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\" &",
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 );
127 $width = $ph->getWidth();
128 $height = $ph->getHeight();
130 if($width < 175 || $height < 175) {
131 $ph->scaleImageUp(200);
132 $width = $ph->getWidth();
133 $height = $ph->getHeight();
136 $hash = photo_new_resource();
141 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
144 notice( t('Image uploaded successfully.') . EOL );
146 notice( t('Image upload failed.') . EOL );
148 if($width > 640 || $height > 640) {
149 $ph->scaleImage(640);
150 $r = $ph->store(local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
153 notice( t('Image size reduction [640] failed.') . EOL );
158 $a->config['imagecrop'] = $hash;
159 $a->config['imagecrop_resolution'] = $smallest;
160 $a->page['htmlhead'] .= load_view_file("view/crophead.tpl");
165 if(! function_exists('profile_photo_content')) {
166 function profile_photo_content(&$a) {
169 notice( t('Permission denied.') . EOL );
173 if(! x($a->config,'imagecrop')) {
175 $tpl = load_view_file('view/profile_photo.tpl');
177 $o .= replace_macros($tpl,array(
184 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.jpg';
185 $resolution = $a->config['imagecrop_resolution'];
186 $tpl = load_view_file("view/cropbody.tpl");
187 $o .= replace_macros($tpl,array(
188 '$filename' => $filename,
189 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
190 '$image_url' => $a->get_baseurl() . '/photo/' . $filename
196 return; // NOTREACHED