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 ( t('Image size reduction [175] failed.') . EOL );
65 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, 1);
68 notice( t('Image size reduction [80] failed.') . EOL );
72 $r = $im->store(local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, 1);
75 notice( t('Image size reduction [48] failed.') . 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 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
91 $url = $_SESSION['my_url'];
92 if($url && strlen(get_config('system','directory_submit_url')))
93 //proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",array(),$foo));
94 proc_run($php_path,"include/directory.php","$url");
97 notice( t('Unable to process image') . EOL);
100 goaway($a->get_baseurl() . '/profiles');
101 return; // NOTREACHED
104 $src = $_FILES['userfile']['tmp_name'];
105 $filename = basename($_FILES['userfile']['name']);
106 $filesize = intval($_FILES['userfile']['size']);
108 $maximagesize = get_config('system','maximagesize');
110 if(($maximagesize) && ($filesize > $maximagesize)) {
111 notice( t('Image exceeds size limit of ') . $maximagesize . EOL);
116 $imagedata = @file_get_contents($src);
117 $ph = new Photo($imagedata);
119 if(! $ph->is_valid()) {
120 notice( t('Unable to process image.') . EOL );
126 return profile_photo_crop_ui_head($a, $ph);
131 if(! function_exists('profile_photo_content')) {
132 function profile_photo_content(&$a) {
135 notice( t('Permission denied.') . EOL );
139 if( $a->argv[1]=='use'){
141 notice( t('Permission denied.') . EOL );
145 $resource_id = $a->argv[2];
146 //die(":".local_user());
147 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
148 intval(local_user()),
152 notice( t('Permission denied.') . EOL );
155 // set an already uloaded photo as profile photo
156 // if photo is in 'Profile Photos', change it in db
157 if ($r[0]['album']== t('Profile Photos')){
158 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
159 intval(local_user()));
161 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
162 intval(local_user()),
166 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d LIMIT 1",
167 dbesc(datetime_convert()),
171 // Update global directory in background
172 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
173 $url = $_SESSION['my_url'];
174 if($url && strlen(get_config('system','directory_submit_url')))
175 //proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",array(),$foo));
176 proc_run($php_path,"include/directory.php","$url");
178 goaway($a->get_baseurl() . '/profiles');
179 return; // NOTREACHED
181 $ph = new Photo($r[0]['data']);
182 profile_photo_crop_ui_head($a, $ph);
183 // go ahead as we have jus uploaded a new photo to crop
186 if(! x($a->config,'imagecrop')) {
188 $tpl = load_view_file('view/profile_photo.tpl');
190 $o .= replace_macros($tpl,array(
191 '$user' => $a->user['nickname']
197 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
198 $resolution = $a->config['imagecrop_resolution'];
199 $tpl = load_view_file("view/cropbody.tpl");
200 $o .= replace_macros($tpl,array(
201 '$filename' => $filename,
202 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
203 '$image_url' => $a->get_baseurl() . '/photo/' . $filename
209 return; // NOTREACHED
213 if(! function_exists('_crop_ui_head')) {
214 function profile_photo_crop_ui_head(&$a, $ph){
215 $width = $ph->getWidth();
216 $height = $ph->getHeight();
218 if($width < 175 || $height < 175) {
219 $ph->scaleImageUp(200);
220 $width = $ph->getWidth();
221 $height = $ph->getHeight();
224 $hash = photo_new_resource();
229 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
232 notice( t('Image uploaded successfully.') . EOL );
234 notice( t('Image upload failed.') . EOL );
236 if($width > 640 || $height > 640) {
237 $ph->scaleImage(640);
238 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
241 notice( t('Image size reduction [640] failed.') . EOL );
246 $a->config['imagecrop'] = $hash;
247 $a->config['imagecrop_resolution'] = $smallest;
248 $a->page['htmlhead'] .= load_view_file("view/crophead.tpl");