3 require_once("Photo.php");
5 function profile_photo_init(&$a) {
11 profile_load($a,$a->user['nickname']);
16 function profile_photo_post(&$a) {
19 notice ( t('Permission denied.') . EOL );
23 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
25 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
27 // phase 2 - we have finished cropping
30 notice( t('Image uploaded but image cropping failed.') . EOL );
34 $image_id = $a->argv[1];
36 if(substr($image_id,-2,1) == '-') {
37 $scale = substr($image_id,-1,1);
38 $image_id = substr($image_id,0,-2);
42 $srcX = $_POST['xstart'];
43 $srcY = $_POST['ystart'];
44 $srcW = $_POST['xfinal'] - $srcX;
45 $srcH = $_POST['yfinal'] - $srcY;
47 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
56 $im = new Photo($base_image['data'], $base_image['type']);
58 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
60 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, 1);
63 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
67 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, 1);
70 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
74 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, 1);
77 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
79 // Unset the profile photo flag from any other photos I own
81 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
82 dbesc($base_image['resource-id']),
86 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
87 dbesc(datetime_convert()),
91 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
92 // Update global directory in background
93 $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
94 if($url && strlen(get_config('system','directory_submit_url')))
95 proc_run('php',"include/directory.php","$url");
97 require_once('include/profile_update.php');
101 notice( t('Unable to process image') . EOL);
104 goaway($a->get_baseurl() . '/profiles');
105 return; // NOTREACHED
108 $src = $_FILES['userfile']['tmp_name'];
109 $filename = basename($_FILES['userfile']['name']);
110 $filesize = intval($_FILES['userfile']['size']);
111 $filetype = $_FILES['userfile']['type'];
112 if ($filetype=="") $filetype=guess_image_type($filename);
114 $maximagesize = get_config('system','maximagesize');
116 if(($maximagesize) && ($filesize > $maximagesize)) {
117 notice( sprintf(t('Image exceeds size limit of %d'), $maximagesize) . EOL);
122 $imagedata = @file_get_contents($src);
123 $ph = new Photo($imagedata, $filetype);
125 if(! $ph->is_valid()) {
126 notice( t('Unable to process image.') . EOL );
132 return profile_photo_crop_ui_head($a, $ph);
137 if(! function_exists('profile_photo_content')) {
138 function profile_photo_content(&$a) {
141 notice( t('Permission denied.') . EOL );
147 if($a->argc == 2 && $a->argv[1] === 'new')
150 if( $a->argv[1]=='use'){
152 notice( t('Permission denied.') . EOL );
156 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
158 $resource_id = $a->argv[2];
159 //die(":".local_user());
160 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
161 intval(local_user()),
165 notice( t('Permission denied.') . EOL );
170 if($rr['scale'] == 5)
174 // set an already uloaded photo as profile photo
175 // if photo is in 'Profile Photos', change it in db
176 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
177 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
178 intval(local_user()));
180 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
181 intval(local_user()),
185 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
186 dbesc(datetime_convert()),
190 // Update global directory in background
191 $url = $_SESSION['my_url'];
192 if($url && strlen(get_config('system','directory_submit_url')))
193 proc_run('php',"include/directory.php","$url");
195 goaway($a->get_baseurl() . '/profiles');
196 return; // NOTREACHED
198 $ph = new Photo($r[0]['data'], $r[0]['type']);
199 profile_photo_crop_ui_head($a, $ph);
200 // go ahead as we have jus uploaded a new photo to crop
203 if(! x($a->config,'imagecrop')) {
205 $tpl = get_markup_template('profile_photo.tpl');
207 $o .= replace_macros($tpl,array(
208 '$user' => $a->user['nickname'],
209 '$lbl_upfile' => t('Upload File:'),
210 '$title' => t('Upload Profile Photo'),
211 '$submit' => t('Upload'),
212 '$form_security_token' => get_form_security_token("profile_photo"),
213 '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . $a->get_baseurl() . '">' . t('skip this step') . '</a>' : '<a href="'. $a->get_baseurl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
219 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
220 $resolution = $a->config['imagecrop_resolution'];
221 $tpl = get_markup_template("cropbody.tpl");
222 $o .= replace_macros($tpl,array(
223 '$filename' => $filename,
224 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
225 '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
226 '$title' => t('Crop Image'),
227 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
228 '$form_security_token' => get_form_security_token("profile_photo"),
229 '$done' => t('Done Editing')
234 return; // NOTREACHED
238 if(! function_exists('_crop_ui_head')) {
239 function profile_photo_crop_ui_head(&$a, $ph){
240 $width = $ph->getWidth();
241 $height = $ph->getHeight();
243 if($width < 175 || $height < 175) {
244 $ph->scaleImageUp(200);
245 $width = $ph->getWidth();
246 $height = $ph->getHeight();
249 $hash = photo_new_resource();
254 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
257 info( t('Image uploaded successfully.') . EOL );
259 notice( t('Image upload failed.') . EOL );
261 if($width > 640 || $height > 640) {
262 $ph->scaleImage(640);
263 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
266 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
271 $a->config['imagecrop'] = $hash;
272 $a->config['imagecrop_resolution'] = $smallest;
273 $a->config['imagecrop_ext'] = $ph->getExt();
274 $a->page['htmlhead'] .= get_markup_template("crophead.tpl");