3a70ced2cc39075a63196f0c5c5f8d7ff31f7000
[atutor.git] / docs / mods / _standard / social / profile_picture.php
1 <?php\r
2 /****************************************************************/\r
3 /* ATutor                                                                                                               */\r
4 /****************************************************************/\r
5 /* Copyright (c) 2002-2009                                                                              */\r
6 /* Inclusive Design Institute                                   */\r
7 /* http://atutor.ca                                                                                             */\r
8 /*                                                              */\r
9 /* This program is free software. You can redistribute it and/or*/\r
10 /* modify it under the terms of the GNU General Public License  */\r
11 /* as published by the Free Software Foundation.                                */\r
12 /****************************************************************/\r
13 // $Id$\r
14 $_user_location = 'public';\r
15 define('AT_INCLUDE_PATH', '../../../include/');\r
16 require (AT_INCLUDE_PATH.'vitals.inc.php');\r
17 $member_id = $_SESSION['member_id'];\r
18 $_custom_css = $_base_path . AT_SOCIAL_BASENAME . 'module.css'; // use a custom stylesheet\r
19 \r
20 function resize_image($src, $dest, $src_h, $src_w, $dest_h, $dest_w, $type, $src_x=0, $src_y=0) {\r
21         $thumbnail_img = imagecreatetruecolor($dest_w, $dest_h);\r
22 \r
23         if ($type == 'gif') {\r
24                 $source = imagecreatefromgif($src);\r
25         } else if ($type == 'jpg') {\r
26                 $source = imagecreatefromjpeg($src);\r
27         } else {\r
28                 $source = imagecreatefrompng($src);\r
29         }\r
30         \r
31         if ($src_x > 0 || $src_y > 0){\r
32                 imagecopyresized($thumbnail_img, $source, 0, 0, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h);\r
33         } else {\r
34                 imagecopyresampled($thumbnail_img, $source, $src_x, $src_y, 0, 0, $dest_w, $dest_h, $src_w, $src_h);\r
35         }\r
36 \r
37         if ($type == 'gif') {\r
38                 imagegif($thumbnail_img, $dest);\r
39         } else if ($type == 'jpg') {\r
40                 imagejpeg($thumbnail_img, $dest, 75);\r
41         } else {\r
42                 imagepng($thumbnail_img, $dest, 7);\r
43         }\r
44 }\r
45 \r
46 // check if GD is installed\r
47 if (!extension_loaded('gd')) {\r
48         require(AT_INCLUDE_PATH.'header.inc.php');\r
49         $msg->printInfos('FEATURE_NOT_AVAILABLE');\r
50         require(AT_INCLUDE_PATH.'footer.inc.php');\r
51         exit;\r
52 }\r
53 \r
54 // check if folder exists, if not, create it\r
55 if (!is_dir(AT_CONTENT_DIR.'/profile_pictures/profile')) {\r
56         mkdir(AT_CONTENT_DIR.'/profile_pictures/profile');\r
57 }\r
58 \r
59 $gd_info = gd_info();\r
60 $supported_images = array();\r
61 if ($gd_info['GIF Create Support']) {\r
62         $supported_images[] = 'gif';\r
63 }\r
64 if ($gd_info['JPG Support'] || $gd_info['JPEG Support']) {\r
65         $supported_images[] = 'jpg';\r
66 }\r
67 if ($gd_info['PNG Support']) {\r
68         $supported_images[] = 'png';\r
69 }\r
70 \r
71 if (!$supported_images) {\r
72         require(AT_INCLUDE_PATH.'header.inc.php');\r
73         $msg->printInfos('FEATURE_NOT_AVAILABLE');\r
74         require(AT_INCLUDE_PATH.'footer.inc.php');\r
75         exit;\r
76 }\r
77 \r
78 if (isset($_POST['cancel'])) {\r
79         $msg->addFeedback('CANCELLED');\r
80         header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
81         exit;\r
82 } else if (isset($_POST['submit'])) {\r
83         if (isset($_POST['delete']) && !$_FILES['file']['size']) {\r
84                 profile_image_delete($member_id);\r
85 \r
86                 $msg->addFeedback('PROFILE_UPDATED');\r
87 \r
88                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
89                 exit;\r
90         } else if ($_FILES['file']['error'] == UPLOAD_ERR_FORM_SIZE) {\r
91                 $msg->addError(array('FILE_MAX_SIZE', $_config['prof_pic_max_file_size'] . ' ' . _AT('bytes')));\r
92                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
93                 exit;\r
94         } else if (!$_FILES['file']['size']) {\r
95                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
96                 exit;\r
97         }\r
98 \r
99         // check if this is a supported file type\r
100         $filename   = $stripslashes($_FILES['file']['name']);\r
101         $path_parts = pathinfo($filename);\r
102         $extension  = strtolower($path_parts['extension']);\r
103         $image_attributes = getimagesize($_FILES['file']['tmp_name']);\r
104 \r
105         if ($extension == 'jpeg') {\r
106                 $extension = 'jpg';\r
107         }\r
108 \r
109         if (!in_array($extension, $supported_images)) {\r
110                 $msg->addError(array('FILE_ILLEGAL', $extension));\r
111                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
112                 exit;\r
113         } else if ($image_attributes[2] > IMAGETYPE_PNG) {\r
114                 $msg->addError(array('FILE_ILLEGAL', $extension));\r
115                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
116                 exit;\r
117         }\r
118 \r
119         // make sure under max file size\r
120         if ($_FILES['file']['size'] > $_config['prof_pic_max_file_size']) {\r
121                 $msg->addError('FILE_MAX_SIZE');\r
122                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
123                 exit;\r
124         }\r
125 \r
126         // delete the old images (if any)\r
127         profile_image_delete($member_id);\r
128 \r
129         $new_filename   = $member_id . '.' . $extension;\r
130         $original_img  = AT_CONTENT_DIR.'profile_pictures/originals/'. $new_filename;\r
131         $profile_img   = AT_CONTENT_DIR.'profile_pictures/profile/'. $new_filename;\r
132         $thumbnail_img = AT_CONTENT_DIR.'profile_pictures/thumbs/'. $new_filename;\r
133 \r
134         // save original\r
135         if (!move_uploaded_file($_FILES['file']['tmp_name'], $original_img)) {\r
136                 $msg->addError('CANNOT_OVERWRITE_FILE');\r
137                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
138                 exit;\r
139         }\r
140 \r
141         // resize the original and save it at $thumbnail_file\r
142         $width  = $image_attributes[0];\r
143         $height = $image_attributes[1];\r
144 \r
145         $thumbnail_fixed_height = 60; \r
146         $thumbnail_fixed_width = 60; \r
147 \r
148         if ($width > $height && $height > $thumbnail_fixed_height) {\r
149                 $thumbnail_height= $thumbnail_fixed_height;\r
150                 $thumbnail_width = intval($thumbnail_fixed_height * $width / $height);\r
151                 resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);\r
152                 //cropping\r
153                 resize_image($thumbnail_img, $thumbnail_img, $thumbnail_fixed_height, $thumbnail_fixed_width, $thumbnail_fixed_height, $thumbnail_fixed_width, $extension, ($thumbnail_width-$thumbnail_fixed_width)/2);\r
154         } else if ($width <= $height && $width>$thumbnail_fixed_width) {\r
155                 $thumbnail_height = intval($thumbnail_fixed_width * $height / $width);\r
156                 $thumbnail_width  = $thumbnail_fixed_width;\r
157                 resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);\r
158                 //cropping\r
159                 resize_image($thumbnail_img, $thumbnail_img, $thumbnail_fixed_height, $thumbnail_fixed_width, $thumbnail_fixed_height, $thumbnail_fixed_width, $extension, 0, ($thumbnail_height-$thumbnail_fixed_height)/2);\r
160         } else {\r
161                 // no resizing, just copy the image.\r
162                 // it's too small to resize.\r
163                 copy($original_img, $thumbnail_img);\r
164         }\r
165 \r
166         // resize the original and save it to profile\r
167         $profile_fixed_height = 320;\r
168         $profile_fixed_width = 240;\r
169         if ($width > $height && $height>$profile_fixed_height) {\r
170                 $profile_width = intval($profile_fixed_height * $width / $height);\r
171                 $profile_height  = $profile_fixed_height;\r
172                 resize_image($original_img, $profile_img, $height, $width, $profile_height, $profile_width, $extension);\r
173                 //cropping\r
174                 resize_image($profile_img, $profile_img, $profile_fixed_height, $profile_fixed_width, $profile_fixed_height, $profile_fixed_width, $extension, ($profile_width-$profile_fixed_width)/2);\r
175         } else if ($width <= $height && $width > $profile_fixed_width) {\r
176                 $profile_width = $profile_fixed_width;\r
177                 $profile_height = intval($profile_fixed_width * $height / $width);\r
178                 resize_image($original_img, $profile_img, $height, $width, $profile_height, $profile_width, $extension);\r
179                 //cropping\r
180                 resize_image($profile_img, $profile_img, $profile_fixed_height, $profile_fixed_width, $profile_fixed_height, $profile_fixed_width, $extension, 0, ($profile_height-$profile_fixed_height)/2);\r
181         } else {\r
182                 // no resizing, just copy the image.\r
183                 // it's too small to resize.\r
184                 copy($original_img, $profile_img);\r
185         }\r
186 \r
187         $msg->addFeedback('PROFILE_UPDATED');\r
188 \r
189         header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
190         exit;\r
191 }\r
192 \r
193 require(AT_INCLUDE_PATH.'header.inc.php');\r
194 $savant->assign('member_id', $member_id);\r
195 $savant->assign('supported_images', $supported_images);\r
196 $savant->display('social/profile_picture.html.php');\r
197 require(AT_INCLUDE_PATH.'footer.inc.php'); \r
198 ?>