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 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
25 // phase 2 - we have finished cropping
28 notice( t('Image uploaded but image cropping failed.') . EOL );
32 $image_id = $a->argv[1];
34 if(substr($image_id,-2,1) == '-') {
35 $scale = substr($image_id,-1,1);
36 $image_id = substr($image_id,0,-2);
40 $srcX = $_POST['xstart'];
41 $srcY = $_POST['ystart'];
42 $srcW = $_POST['xfinal'] - $srcX;
43 $srcH = $_POST['yfinal'] - $srcY;
45 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
54 $im = new Photo($base_image['data']);
56 $im->cropImage(175,$srcX,$srcY,$srcW,$srcH);
58 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, 1);
61 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
65 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, 1);
68 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
72 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, 1);
75 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
77 // Unset the profile photo flag from any other photos I own
79 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
80 dbesc($base_image['resource-id']),
84 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
85 dbesc(datetime_convert()),
89 // Update global directory in background
90 $url = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
91 if($url && strlen(get_config('system','directory_submit_url')))
92 proc_run('php',"include/directory.php","$url");
94 require_once('include/profile_update.php');
98 notice( t('Unable to process image') . EOL);
101 goaway($a->get_baseurl() . '/profiles');
102 return; // NOTREACHED
105 $src = $_FILES['userfile']['tmp_name'];
106 $filename = basename($_FILES['userfile']['name']);
107 $filesize = intval($_FILES['userfile']['size']);
109 $maximagesize = get_config('system','maximagesize');
111 if(($maximagesize) && ($filesize > $maximagesize)) {
112 notice( sprintf(t('Image exceeds size limit of %d'), $maximagesize) . EOL);
117 $imagedata = @file_get_contents($src);
118 $ph = new Photo($imagedata);
120 if(! $ph->is_valid()) {
121 notice( t('Unable to process image.') . EOL );
127 return profile_photo_crop_ui_head($a, $ph);
132 if(! function_exists('profile_photo_content')) {
133 function profile_photo_content(&$a) {
136 notice( t('Permission denied.') . EOL );
142 if($a->argc == 2 && $a->argv[1] === 'new')
145 if( $a->argv[1]=='use'){
147 notice( t('Permission denied.') . EOL );
151 $resource_id = $a->argv[2];
152 //die(":".local_user());
153 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
154 intval(local_user()),
158 notice( t('Permission denied.') . EOL );
163 if($rr['scale'] == 5)
167 // set an already uloaded photo as profile photo
168 // if photo is in 'Profile Photos', change it in db
169 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
170 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
171 intval(local_user()));
173 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
174 intval(local_user()),
178 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
179 dbesc(datetime_convert()),
183 // Update global directory in background
184 $url = $_SESSION['my_url'];
185 if($url && strlen(get_config('system','directory_submit_url')))
186 proc_run('php',"include/directory.php","$url");
188 goaway($a->get_baseurl() . '/profiles');
189 return; // NOTREACHED
191 $ph = new Photo($r[0]['data']);
192 profile_photo_crop_ui_head($a, $ph);
193 // go ahead as we have jus uploaded a new photo to crop
196 if(! x($a->config,'imagecrop')) {
198 $tpl = get_markup_template('profile_photo.tpl');
200 $o .= replace_macros($tpl,array(
201 '$user' => $a->user['nickname'],
202 '$lbl_upfile' => t('Upload File:'),
203 '$title' => t('Upload Profile Photo'),
204 '$submit' => t('Upload'),
205 '$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>')
211 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
212 $resolution = $a->config['imagecrop_resolution'];
213 $tpl = get_markup_template("cropbody.tpl");
214 $o .= replace_macros($tpl,array(
215 '$filename' => $filename,
216 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
217 '$image_url' => $a->get_baseurl() . '/photo/' . $filename,
218 '$title' => t('Crop Image'),
219 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
220 '$done' => t('Done Editing')
225 return; // NOTREACHED
229 if(! function_exists('_crop_ui_head')) {
230 function profile_photo_crop_ui_head(&$a, $ph){
231 $width = $ph->getWidth();
232 $height = $ph->getHeight();
234 if($width < 175 || $height < 175) {
235 $ph->scaleImageUp(200);
236 $width = $ph->getWidth();
237 $height = $ph->getHeight();
240 $hash = photo_new_resource();
245 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
248 info( t('Image uploaded successfully.') . EOL );
250 notice( t('Image upload failed.') . EOL );
252 if($width > 640 || $height > 640) {
253 $ph->scaleImage(640);
254 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
257 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
262 $a->config['imagecrop'] = $hash;
263 $a->config['imagecrop_resolution'] = $smallest;
264 $a->page['htmlhead'] .= get_markup_template("crophead.tpl");