remove old readme
[atutor.git] / themes / simplified-desktop / social / profile_picture.html.php
1 <?php\r
2 /************************************************************************/\r
3 /* ATutor                                                                                                                               */\r
4 /************************************************************************/\r
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/\r
6 /* Adaptive Technology Resource Centre / University of Toronto                  */\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: profile_picture.html.php 9418 2010-03-03 16:39:24Z greg $\r
14 if (!defined('AT_INCLUDE_PATH')) { exit; }\r
15 \r
16 function resize_image($src, $dest, $src_h, $src_w, $dest_h, $dest_w, $type, $src_x=0, $src_y=0) {\r
17         $thumbnail_img = imagecreatetruecolor($dest_w, $dest_h);\r
18 \r
19         if ($type == 'gif') {\r
20                 $source = imagecreatefromgif($src);\r
21         } else if ($type == 'jpg') {\r
22                 $source = imagecreatefromjpeg($src);\r
23         } else {\r
24                 $source = imagecreatefrompng($src);\r
25         }\r
26         \r
27         if ($src_x > 0 || $src_y > 0){\r
28                 imagecopyresized($thumbnail_img, $source, 0, 0, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h);\r
29         } else {\r
30                 imagecopyresampled($thumbnail_img, $source, $src_x, $src_y, 0, 0, $dest_w, $dest_h, $src_w, $src_h);\r
31         }\r
32 \r
33         if ($type == 'gif') {\r
34                 imagegif($thumbnail_img, $dest);\r
35         } else if ($type == 'jpg') {\r
36                 imagejpeg($thumbnail_img, $dest, 75);\r
37         } else {\r
38                 imagepng($thumbnail_img, $dest, 7);\r
39         }\r
40 }\r
41 \r
42 // check if GD is installed\r
43 if (!extension_loaded('gd')) {\r
44         require(AT_INCLUDE_PATH.'header.inc.php');\r
45         $msg->printInfos('FEATURE_NOT_AVAILABLE');\r
46         require(AT_INCLUDE_PATH.'footer.inc.php');\r
47         exit;\r
48 }\r
49 \r
50 // check if folder exists, if not, create it\r
51 if (!is_dir(AT_CONTENT_DIR.'/profile_pictures/profile')) {\r
52         mkdir(AT_CONTENT_DIR.'/profile_pictures/profile');\r
53 }\r
54 \r
55 $gd_info = gd_info();\r
56 $supported_images = array();\r
57 if ($gd_info['GIF Create Support']) {\r
58         $supported_images[] = 'gif';\r
59 }\r
60 if ($gd_info['JPG Support']) {\r
61         $supported_images[] = 'jpg';\r
62 }\r
63 if ($gd_info['PNG Support']) {\r
64         $supported_images[] = 'png';\r
65 }\r
66 \r
67 if (!$supported_images) {\r
68         require(AT_INCLUDE_PATH.'header.inc.php');\r
69         $msg->printInfos('FEATURE_NOT_AVAILABLE');\r
70         require(AT_INCLUDE_PATH.'footer.inc.php');\r
71         exit;\r
72 }\r
73 \r
74 if (isset($_POST['cancel'])) {\r
75         $msg->addFeedback('CANCELLED');\r
76         header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
77         exit;\r
78 } else if (isset($_POST['submit'])) {\r
79         if (isset($_POST['delete']) && !$_FILES['file']['size']) {\r
80                 profile_image_delete($member_id);\r
81 \r
82                 $msg->addFeedback('PROFILE_UPDATED');\r
83 \r
84                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
85                 exit;\r
86         } else if ($_FILES['file']['error'] == UPLOAD_ERR_FORM_SIZE) {\r
87                 $msg->addError(array('FILE_MAX_SIZE', $_config['prof_pic_max_file_size'] . ' ' . _AT('bytes')));\r
88                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
89                 exit;\r
90         } else if (!$_FILES['file']['size']) {\r
91                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
92                 exit;\r
93         }\r
94 \r
95         // check if this is a supported file type\r
96         $filename   = $stripslashes($_FILES['file']['name']);\r
97         $path_parts = pathinfo($filename);\r
98         $extension  = strtolower($path_parts['extension']);\r
99         $image_attributes = getimagesize($_FILES['file']['tmp_name']);\r
100 \r
101         if ($extension == 'jpeg') {\r
102                 $extension = 'jpg';\r
103         }\r
104 \r
105         if (!in_array($extension, $supported_images)) {\r
106                 $msg->addError(array('FILE_ILLEGAL', $extension));\r
107                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
108                 exit;\r
109         } else if ($image_attributes[2] > IMAGETYPE_PNG) {\r
110                 $msg->addError(array('FILE_ILLEGAL', $extension));\r
111                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
112                 exit;\r
113         }\r
114 \r
115         // make sure under max file size\r
116         if ($_FILES['file']['size'] > $_config['prof_pic_max_file_size']) {\r
117                 $msg->addError('FILE_MAX_SIZE');\r
118                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
119                 exit;\r
120         }\r
121 \r
122         // delete the old images (if any)\r
123         profile_image_delete($member_id);\r
124 \r
125         $new_filename   = $member_id . '.' . $extension;\r
126         $original_img  = AT_CONTENT_DIR.'profile_pictures/originals/'. $new_filename;\r
127         $profile_img   = AT_CONTENT_DIR.'profile_pictures/profile/'. $new_filename;\r
128         $thumbnail_img = AT_CONTENT_DIR.'profile_pictures/thumbs/'. $new_filename;\r
129 \r
130         // save original\r
131         if (!move_uploaded_file($_FILES['file']['tmp_name'], $original_img)) {\r
132                 $msg->addError('CANNOT_OVERWRITE_FILE');\r
133                 header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
134                 exit;\r
135         }\r
136 \r
137         // resize the original and save it at $thumbnail_file\r
138         $width  = $image_attributes[0];\r
139         $height = $image_attributes[1];\r
140 \r
141         $thumbnail_fixed_height = 60; \r
142         $thumbnail_fixed_width = 60; \r
143 \r
144         if ($width > $height && $height > $thumbnail_fixed_height) {\r
145                 $thumbnail_height= $thumbnail_fixed_height;\r
146                 $thumbnail_width = intval($thumbnail_fixed_height * $width / $height);\r
147                 resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);\r
148                 //cropping\r
149                 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
150         } else if ($width <= $height && $width>$thumbnail_fixed_width) {\r
151                 $thumbnail_height = intval($thumbnail_fixed_width * $height / $width);\r
152                 $thumbnail_width  = $thumbnail_fixed_width;\r
153                 resize_image($original_img, $thumbnail_img, $height, $width, $thumbnail_height, $thumbnail_width, $extension);\r
154                 //cropping\r
155                 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
156         } else {\r
157                 // no resizing, just copy the image.\r
158                 // it's too small to resize.\r
159                 copy($original_img, $thumbnail_img);\r
160         }\r
161 \r
162         // resize the original and save it to profile\r
163         $profile_fixed_height = 320;\r
164         $profile_fixed_width = 240;\r
165         if ($width > $height && $height>$profile_fixed_height) {\r
166                 $profile_width = intval($profile_fixed_height * $width / $height);\r
167                 $profile_height  = $profile_fixed_height;\r
168                 resize_image($original_img, $profile_img, $height, $width, $profile_height, $profile_width, $extension);\r
169                 //cropping\r
170                 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
171         } else if ($width <= $height && $width > $profile_fixed_width) {\r
172                 $profile_width = $profile_fixed_width;\r
173                 $profile_height = intval($profile_fixed_width * $height / $width);\r
174                 resize_image($original_img, $profile_img, $height, $width, $profile_height, $profile_width, $extension);\r
175                 //cropping\r
176                 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
177         } else {\r
178                 // no resizing, just copy the image.\r
179                 // it's too small to resize.\r
180                 copy($original_img, $profile_img);\r
181         }\r
182 \r
183         $msg->addFeedback('PROFILE_UPDATED');\r
184 \r
185         header('Location: '.$_SERVER['PHP_SELF'].'?member_id='.$member_id);\r
186         exit;\r
187 }\r
188 \r
189 require(AT_INCLUDE_PATH.'header.inc.php');\r
190 \r
191 ?>\r
192 <div class="social-wrapper">\r
193 <?php include("lib/profile_menu.inc.php")  ?>\r
194 <br />\r
195 <form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>?member_id=<?php echo $member_id; ?>" name="form">\r
196 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $_config['prof_pic_max_file_size']; ?>" />\r
197 <div class="input-form">\r
198 <?php if (profile_image_exists($member_id)): ?>\r
199         <div class="row">\r
200                 <a href="get_profile_img.php?id=<?php echo $member_id.SEP.'size=o'; ?>"><img src="get_profile_img.php?id=<?php echo $member_id; ?>" alt="" /></a>\r
201                 <input type="checkbox" name="delete" value="1" id="del"/><label for="del"><?php echo _AT('delete'); ?></label>\r
202         </div>\r
203 <?php endif; ?>\r
204         <div class="row">\r
205                 <h3><label for="upload_picture"><?php echo _AT('upload_new_picture'); ?></label></h3>\r
206                 <input type="file" name="file" id="upload_picture"/> (<?php echo implode(', ', $supported_images); ?>)</div>\r
207 \r
208         <div class="row buttons">\r
209                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" />\r
210                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />\r
211         </div>\r
212 </div>\r
213 </form>\r
214 <div style="clear:both;"></div>\r
215 </div>\r
216 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>