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\DBA;
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 (DBA::isResult($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", DBA::escape($image_id),
73 DBA::escape(local_user()), intval($scale));
75 $url = System::baseUrl() . '/profile/' . $a->user['nickname'];
76 if (DBA::isResult($r)) {
79 $Image = new Image($base_image['data'], $base_image['type']);
80 if ($Image->isValid()) {
81 $Image->crop(175, $srcX, $srcY, $srcW, $srcH);
83 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
84 L10n::t('Profile Photos'), 4, $is_default_profile);
87 notice(L10n::t('Image size reduction [%s] failed.', "175") . EOL);
90 $Image->scaleDown(80);
92 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
93 L10n::t('Profile Photos'), 5, $is_default_profile);
96 notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
99 $Image->scaleDown(48);
101 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
102 L10n::t('Profile Photos'), 6, $is_default_profile);
105 notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
108 // If setting for the default profile, unset the profile photo flag from any other photos I own
110 if ($is_default_profile) {
111 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
112 DBA::escape($base_image['resource-id']), intval(local_user())
115 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
116 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
117 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
118 intval($_REQUEST['profile']), intval(local_user())
122 Contact::updateSelfFromUserID(local_user(), true);
124 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
125 // Update global directory in background
126 if ($url && strlen(Config::get('system', 'directory'))) {
127 Worker::add(PRIORITY_LOW, "Directory", $url);
130 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
132 notice(L10n::t('Unable to process image') . EOL);
137 return; // NOTREACHED
140 $src = $_FILES['userfile']['tmp_name'];
141 $filename = basename($_FILES['userfile']['name']);
142 $filesize = intval($_FILES['userfile']['size']);
143 $filetype = $_FILES['userfile']['type'];
144 if ($filetype == "") {
145 $filetype = Image::guessType($filename);
148 $maximagesize = Config::get('system', 'maximagesize');
150 if (($maximagesize) && ($filesize > $maximagesize)) {
151 notice(L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize)) . EOL);
156 $imagedata = @file_get_contents($src);
157 $ph = new Image($imagedata, $filetype);
159 if (!$ph->isValid()) {
160 notice(L10n::t('Unable to process image.') . EOL);
168 $imagecrop = profile_photo_crop_ui_head($a, $ph);
169 goaway(System::baseUrl() . '/profile_photo/use/' . $imagecrop['hash']);
172 function profile_photo_content(App $a)
176 notice(L10n::t('Permission denied.') . EOL);
182 if ($a->argc == 2 && $a->argv[1] === 'new') {
188 if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
189 // check_form_security_token_redirectOnErr('/profile_photo', 'profile_photo');
191 $resource_id = $a->argv[2];
192 //die(":".local_user());
193 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
194 DBA::escape($resource_id)
197 if (!DBA::isResult($r)) {
198 notice(L10n::t('Permission denied.') . EOL);
203 foreach ($r as $rr) {
204 if ($rr['scale'] == 5) {
209 // set an already uloaded photo as profile photo
210 // if photo is in 'Profile Photos', change it in db
211 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
212 $r = q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
214 $r = q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
215 DBA::escape($resource_id)
218 Contact::updateSelfFromUserID(local_user(), true);
220 // Update global directory in background
221 $url = $_SESSION['my_url'];
222 if ($url && strlen(Config::get('system', 'directory'))) {
223 Worker::add(PRIORITY_LOW, "Directory", $url);
226 goaway(System::baseUrl() . '/profile/' . $a->user['nickname']);
227 return; // NOTREACHED
229 $ph = new Image($r[0]['data'], $r[0]['type']);
230 $imagecrop = profile_photo_crop_ui_head($a, $ph);
231 // go ahead as we have jus uploaded a new photo to crop
234 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
238 if (empty($imagecrop)) {
239 $tpl = get_markup_template('profile_photo.tpl');
241 $o = replace_macros($tpl,
243 '$user' => $a->user['nickname'],
244 '$lbl_upfile' => L10n::t('Upload File:'),
245 '$lbl_profiles' => L10n::t('Select a profile:'),
246 '$title' => L10n::t('Upload Profile Photo'),
247 '$submit' => L10n::t('Upload'),
248 '$profiles' => $profiles,
249 '$form_security_token' => get_form_security_token("profile_photo"),
250 '$select' => sprintf('%s %s', L10n::t('or'),
251 ($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>')
256 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
257 $tpl = get_markup_template("cropbody.tpl");
258 $o = replace_macros($tpl,
260 '$filename' => $filename,
261 '$profile' => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
262 '$resource' => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
263 '$image_url' => System::baseUrl() . '/photo/' . $filename,
264 '$title' => L10n::t('Crop Image'),
265 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
266 '$form_security_token' => get_form_security_token("profile_photo"),
267 '$done' => L10n::t('Done Editing')
272 return; // NOTREACHED
275 function profile_photo_crop_ui_head(App $a, Image $image)
277 $max_length = Config::get('system', 'max_image_length');
279 $max_length = MAX_IMAGE_LENGTH;
281 if ($max_length > 0) {
282 $image->scaleDown($max_length);
285 $width = $image->getWidth();
286 $height = $image->getHeight();
288 if ($width < 175 || $height < 175) {
289 $image->scaleUp(200);
290 $width = $image->getWidth();
291 $height = $image->getHeight();
294 $hash = Photo::newResource();
300 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
303 info(L10n::t('Image uploaded successfully.') . EOL);
305 notice(L10n::t('Image upload failed.') . EOL);
308 if ($width > 640 || $height > 640) {
309 $image->scaleDown(640);
310 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
313 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
319 $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), []);
320 $a->page['end'] .= replace_macros(get_markup_template("cropend.tpl"), []);
324 'resolution' => $smallest,
325 'ext' => $image->getExt(),