3 * @file mod/profile_photo.php
6 use Friendica\Core\Config;
7 use Friendica\Core\System;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBM;
10 use Friendica\Model\Photo;
11 use Friendica\Object\Image;
13 function profile_photo_init(App $a) {
19 profile_load($a,$a->user['nickname']);
22 function profile_photo_post(App $a) {
25 notice ( t('Permission denied.') . EOL );
29 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
31 if((x($_POST,'cropfinal')) && ($_POST['cropfinal'] == 1)) {
33 // unless proven otherwise
34 $is_default_profile = 1;
36 if($_REQUEST['profile']) {
37 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1",
38 intval($_REQUEST['profile']),
41 if (DBM::is_result($r) && (! intval($r[0]['is-default'])))
42 $is_default_profile = 0;
47 // phase 2 - we have finished cropping
50 notice( t('Image uploaded but image cropping failed.') . EOL );
54 $image_id = $a->argv[1];
56 if(substr($image_id,-2,1) == '-') {
57 $scale = substr($image_id,-1,1);
58 $image_id = substr($image_id,0,-2);
62 $srcX = $_POST['xstart'];
63 $srcY = $_POST['ystart'];
64 $srcW = $_POST['xfinal'] - $srcX;
65 $srcH = $_POST['yfinal'] - $srcY;
67 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1",
72 if (DBM::is_result($r)) {
76 $Image = new Image($base_image['data'], $base_image['type']);
77 if ($Image->isValid()) {
78 $Image->crop(175,$srcX,$srcY,$srcW,$srcH);
80 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 4, $is_default_profile);
83 notice ( sprintf(t('Image size reduction [%s] failed.'),"175") . EOL );
86 $Image->scaleDown(80);
88 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 5, $is_default_profile);
91 notice( sprintf(t('Image size reduction [%s] failed.'),"80") . EOL );
94 $Image->scaleDown(48);
96 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'],$base_image['filename'], t('Profile Photos'), 6, $is_default_profile);
99 notice( sprintf(t('Image size reduction [%s] failed.'),"48") . EOL );
102 // If setting for the default profile, unset the profile photo flag from any other photos I own
104 if($is_default_profile) {
105 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
106 dbesc($base_image['resource-id']),
110 $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `self` AND `uid` = %d",
111 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
112 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
113 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-6.' . $Image->getExt()),
117 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
118 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
119 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
120 intval($_REQUEST['profile']),
125 // we'll set the updated profile-photo timestamp even if it isn't the default profile,
126 // so that browsers will do a cache update unconditionally
128 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
129 dbesc(datetime_convert()),
133 info( t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
134 // Update global directory in background
135 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
136 if ($url && strlen(Config::get('system','directory'))) {
137 Worker::add(PRIORITY_LOW, "Directory", $url);
140 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
142 notice( t('Unable to process image') . EOL);
146 goaway(System::baseUrl() . '/profiles');
147 return; // NOTREACHED
150 $src = $_FILES['userfile']['tmp_name'];
151 $filename = basename($_FILES['userfile']['name']);
152 $filesize = intval($_FILES['userfile']['size']);
153 $filetype = $_FILES['userfile']['type'];
154 if ($filetype == "") {
155 $filetype = Image::guessType($filename);
158 $maximagesize = Config::get('system', 'maximagesize');
160 if (($maximagesize) && ($filesize > $maximagesize)) {
161 notice(sprintf(t('Image exceeds size limit of %s'), formatBytes($maximagesize)) . EOL);
166 $imagedata = @file_get_contents($src);
167 $ph = new Image($imagedata, $filetype);
169 if (! $ph->isValid()) {
170 notice(t('Unable to process image.') . EOL);
177 return profile_photo_crop_ui_head($a, $ph);
181 function profile_photo_content(App $a) {
183 if (! local_user()) {
184 notice( t('Permission denied.') . EOL );
190 if($a->argc == 2 && $a->argv[1] === 'new')
193 if( $a->argv[1]=='use'){
195 notice( t('Permission denied.') . EOL );
199 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
201 $resource_id = $a->argv[2];
202 //die(":".local_user());
203 $r=q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC",
204 intval(local_user()),
207 if (!DBM::is_result($r)){
208 notice( t('Permission denied.') . EOL );
212 foreach ($r as $rr) {
213 if($rr['scale'] == 5)
217 // set an already uloaded photo as profile photo
218 // if photo is in 'Profile Photos', change it in db
219 if (($r[0]['album']== t('Profile Photos')) && ($havescale)){
220 $r=q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d",
221 intval(local_user()));
223 $r=q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'",
224 intval(local_user()),
228 $r = q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
229 dbesc(datetime_convert()),
233 // Update global directory in background
234 $url = $_SESSION['my_url'];
235 if ($url && strlen(Config::get('system','directory'))) {
236 Worker::add(PRIORITY_LOW, "Directory", $url);
239 goaway(System::baseUrl() . '/profiles');
240 return; // NOTREACHED
242 $ph = new Image($r[0]['data'], $r[0]['type']);
243 profile_photo_crop_ui_head($a, $ph);
244 // go ahead as we have jus uploaded a new photo to crop
247 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
252 if(! x($a->config,'imagecrop')) {
254 $tpl = get_markup_template('profile_photo.tpl');
256 $o .= replace_macros($tpl,array(
257 '$user' => $a->user['nickname'],
258 '$lbl_upfile' => t('Upload File:'),
259 '$lbl_profiles' => t('Select a profile:'),
260 '$title' => t('Upload Profile Photo'),
261 '$submit' => t('Upload'),
262 '$profiles' => $profiles,
263 '$form_security_token' => get_form_security_token("profile_photo"),
264 '$select' => sprintf('%s %s', t('or'), ($newuser) ? '<a href="' . System::baseUrl() . '">' . t('skip this step') . '</a>' : '<a href="'. System::baseUrl() . '/photos/' . $a->user['nickname'] . '">' . t('select a photo from your photo albums') . '</a>')
270 $filename = $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'] . '.'.$a->config['imagecrop_ext'];
271 $resolution = $a->config['imagecrop_resolution'];
272 $tpl = get_markup_template("cropbody.tpl");
273 $o .= replace_macros($tpl,array(
274 '$filename' => $filename,
275 '$profile' => intval($_REQUEST['profile']),
276 '$resource' => $a->config['imagecrop'] . '-' . $a->config['imagecrop_resolution'],
277 '$image_url' => System::baseUrl() . '/photo/' . $filename,
278 '$title' => t('Crop Image'),
279 '$desc' => t('Please adjust the image cropping for optimum viewing.'),
280 '$form_security_token' => get_form_security_token("profile_photo"),
281 '$done' => t('Done Editing')
286 return; // NOTREACHED
290 function profile_photo_crop_ui_head(App $a, Image $Image) {
291 $max_length = Config::get('system','max_image_length');
293 $max_length = MAX_IMAGE_LENGTH;
295 if ($max_length > 0) {
296 $Image->scaleDown($max_length);
299 $width = $Image->getWidth();
300 $height = $Image->getHeight();
302 if ($width < 175 || $height < 175) {
303 $Image->scaleUp(200);
304 $width = $Image->getWidth();
305 $height = $Image->getHeight();
308 $hash = photo_new_resource();
313 $r = Photo::store($Image, local_user(), 0 , $hash, $filename, t('Profile Photos'), 0 );
316 info( t('Image uploaded successfully.') . EOL );
318 notice( t('Image upload failed.') . EOL );
321 if ($width > 640 || $height > 640) {
322 $Image->scaleDown(640);
323 $r = Photo::store($Image, local_user(), 0 , $hash, $filename, t('Profile Photos'), 1 );
326 notice( sprintf(t('Image size reduction [%s] failed.'),"640") . EOL );
332 $a->config['imagecrop'] = $hash;
333 $a->config['imagecrop_resolution'] = $smallest;
334 $a->config['imagecrop_ext'] = $Image->getExt();
335 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), array());
336 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), array());