3 * @file mod/profile_photo.php
7 use Friendica\Core\Config;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Photo;
14 use Friendica\Model\Profile;
15 use Friendica\Object\Image;
17 function profile_photo_init(App $a)
23 Profile::load($a, $a->user['nickname']);
26 function profile_photo_post(App $a)
29 notice(L10n::t('Permission denied.') . EOL);
33 check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
35 if (!empty($_POST['cropfinal']) && $_POST['cropfinal'] == 1) {
37 // unless proven otherwise
38 $is_default_profile = 1;
40 if ($_REQUEST['profile']) {
41 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']),
45 if (DBM::is_result($r) && (!intval($r[0]['is-default']))) {
46 $is_default_profile = 0;
52 // phase 2 - we have finished cropping
55 notice(L10n::t('Image uploaded but image cropping failed.') . EOL);
59 $image_id = $a->argv[1];
61 if (substr($image_id, -2, 1) == '-') {
62 $scale = substr($image_id, -1, 1);
63 $image_id = substr($image_id, 0, -2);
67 $srcX = $_POST['xstart'];
68 $srcY = $_POST['ystart'];
69 $srcW = $_POST['xfinal'] - $srcX;
70 $srcH = $_POST['yfinal'] - $srcY;
72 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1", dbesc($image_id),
73 dbesc(local_user()), intval($scale));
75 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
76 if (DBM::is_result($r)) {
80 $Image = new Image($base_image['data'], $base_image['type']);
81 if ($Image->isValid()) {
82 $Image->crop(175, $srcX, $srcY, $srcW, $srcH);
84 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
85 L10n::t('Profile Photos'), 4, $is_default_profile);
88 notice(L10n::t('Image size reduction [%s] failed.', "175") . EOL);
91 $Image->scaleDown(80);
93 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
94 L10n::t('Profile Photos'), 5, $is_default_profile);
97 notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
100 $Image->scaleDown(48);
102 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
103 L10n::t('Profile Photos'), 6, $is_default_profile);
106 notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
109 // If setting for the default profile, unset the profile photo flag from any other photos I own
111 if ($is_default_profile) {
112 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
113 dbesc($base_image['resource-id']), intval(local_user())
116 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
117 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
118 dbesc(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
119 intval($_REQUEST['profile']), intval(local_user())
123 Contact::updateSelfFromUserID(local_user(), true);
125 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
126 // Update global directory in background
127 if ($url && strlen(Config::get('system', 'directory'))) {
128 Worker::add(PRIORITY_LOW, "Directory", $url);
131 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
133 notice(L10n::t('Unable to process image') . EOL);
138 return; // NOTREACHED
141 $src = $_FILES['userfile']['tmp_name'];
142 $filename = basename($_FILES['userfile']['name']);
143 $filesize = intval($_FILES['userfile']['size']);
144 $filetype = $_FILES['userfile']['type'];
145 if ($filetype == "") {
146 $filetype = Image::guessType($filename);
149 $maximagesize = Config::get('system', 'maximagesize');
151 if (($maximagesize) && ($filesize > $maximagesize)) {
152 notice(L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize)) . EOL);
157 $imagedata = @file_get_contents($src);
158 $ph = new Image($imagedata, $filetype);
160 if (!$ph->isValid()) {
161 notice(L10n::t('Unable to process image.') . EOL);
169 $imagecrop = profile_photo_crop_ui_head($a, $ph);
170 goaway(System::baseUrl() . '/profile_photo/use/' . $imagecrop['hash']);
173 function profile_photo_content(App $a)
177 notice(L10n::t('Permission denied.') . EOL);
183 if ($a->argc == 2 && $a->argv[1] === 'new') {
189 if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
190 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
192 $resource_id = $a->argv[2];
193 //die(":".local_user());
194 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
198 if (!DBM::is_result($r)) {
199 notice(L10n::t('Permission denied.') . EOL);
204 foreach ($r as $rr) {
205 if ($rr['scale'] == 5) {
210 // set an already uloaded photo as profile photo
211 // if photo is in 'Profile Photos', change it in db
212 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
213 $r = q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
215 $r = q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
219 Contact::updateSelfFromUserID(local_user(), true);
221 // Update global directory in background
222 $url = $_SESSION['my_url'];
223 if ($url && strlen(Config::get('system', 'directory'))) {
224 Worker::add(PRIORITY_LOW, "Directory", $url);
227 goaway(System::baseUrl() . '/profile/' . $a->user['nickname']);
228 return; // NOTREACHED
230 $ph = new Image($r[0]['data'], $r[0]['type']);
231 $imagecrop = profile_photo_crop_ui_head($a, $ph);
232 // go ahead as we have jus uploaded a new photo to crop
235 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
239 if (empty($imagecrop)) {
240 $tpl = get_markup_template('profile_photo.tpl');
242 $o = replace_macros($tpl,
244 '$user' => $a->user['nickname'],
245 '$lbl_upfile' => L10n::t('Upload File:'),
246 '$lbl_profiles' => L10n::t('Select a profile:'),
247 '$title' => L10n::t('Upload Profile Photo'),
248 '$submit' => L10n::t('Upload'),
249 '$profiles' => $profiles,
250 '$form_security_token' => get_form_security_token("profile_photo"),
251 '$select' => sprintf('%s %s', L10n::t('or'),
252 ($newuser) ? '<a href="' . System::baseUrl() . '">' . L10n::t('skip this step') . '</a>' : '<a href="' . System::baseUrl() . '/photos/' . $a->user['nickname'] . '">' . L10n::t('select a photo from your photo albums') . '</a>')
257 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
258 $tpl = get_markup_template("cropbody.tpl");
259 $o = replace_macros($tpl,
261 '$filename' => $filename,
262 '$profile' => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
263 '$resource' => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
264 '$image_url' => System::baseUrl() . '/photo/' . $filename,
265 '$title' => L10n::t('Crop Image'),
266 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
267 '$form_security_token' => get_form_security_token("profile_photo"),
268 '$done' => L10n::t('Done Editing')
273 return; // NOTREACHED
276 function profile_photo_crop_ui_head(App $a, Image $image)
278 $max_length = Config::get('system', 'max_image_length');
280 $max_length = MAX_IMAGE_LENGTH;
282 if ($max_length > 0) {
283 $image->scaleDown($max_length);
286 $width = $image->getWidth();
287 $height = $image->getHeight();
289 if ($width < 175 || $height < 175) {
290 $image->scaleUp(200);
291 $width = $image->getWidth();
292 $height = $image->getHeight();
295 $hash = Photo::newResource();
301 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
304 info(L10n::t('Image uploaded successfully.') . EOL);
306 notice(L10n::t('Image upload failed.') . EOL);
309 if ($width > 640 || $height > 640) {
310 $image->scaleDown(640);
311 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
314 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
320 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), []);
321 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), []);
325 'resolution' => $smallest,
326 'ext' => $image->getExt(),