3 * @file mod/profile_photo.php
7 use Friendica\BaseModule;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Core\System;
12 use Friendica\Core\Worker;
13 use Friendica\Database\DBA;
14 use Friendica\Model\Contact;
15 use Friendica\Model\Photo;
16 use Friendica\Model\Profile;
17 use Friendica\Object\Image;
18 use Friendica\Util\Strings;
20 function profile_photo_init(App $a)
26 Profile::load($a, $a->user['nickname']);
29 function profile_photo_post(App $a)
32 notice(L10n::t('Permission denied.') . EOL);
36 BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
38 if (!empty($_POST['cropfinal']) && $_POST['cropfinal'] == 1) {
40 // unless proven otherwise
41 $is_default_profile = 1;
43 if ($_REQUEST['profile']) {
44 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']),
48 if (DBA::isResult($r) && (!intval($r[0]['is-default']))) {
49 $is_default_profile = 0;
55 // phase 2 - we have finished cropping
58 notice(L10n::t('Image uploaded but image cropping failed.') . EOL);
62 $image_id = $a->argv[1];
64 if (substr($image_id, -2, 1) == '-') {
65 $scale = substr($image_id, -1, 1);
66 $image_id = substr($image_id, 0, -2);
70 $srcX = $_POST['xstart'];
71 $srcY = $_POST['ystart'];
72 $srcW = $_POST['xfinal'] - $srcX;
73 $srcH = $_POST['yfinal'] - $srcY;
75 $base_image = Photo::selectFirst([], ['resource-id' => $image_id, 'uid' => local_user(), 'scale' => $scale]);
77 $path = 'profile/' . $a->user['nickname'];
78 if (DBA::isResult($base_image)) {
80 $Image = Photo::getImageForPhoto($base_image);
81 if ($Image->isValid()) {
82 $Image->crop(300, $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.', "300") . 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 q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
113 DBA::escape($base_image['resource-id']), intval(local_user())
116 q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
117 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
118 DBA::escape(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 ($path && strlen(Config::get('system', 'directory'))) {
128 Worker::add(PRIORITY_LOW, "Directory", $a->getBaseURL() . '/' . $path);
131 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
133 notice(L10n::t('Unable to process image') . EOL);
137 $a->internalRedirect($path);
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', Strings::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 $a->internalRedirect('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 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
192 $resource_id = $a->argv[2];
193 //die(":".local_user());
195 $r = Photo::select([], ["resource-id" => $resource_id, "uid" => local_user()], ["order" => ["scale"=>false]]);
198 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
199 DBA::escape($resource_id)
203 if (!DBA::isResult($r)) {
204 notice(L10n::t('Permission denied.') . EOL);
209 foreach ($r as $rr) {
210 if ($rr['scale'] == 5) {
215 // set an already uloaded photo as profile photo
216 // if photo is in 'Profile Photos', change it in db
217 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
218 q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
220 q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
221 DBA::escape($resource_id)
224 Contact::updateSelfFromUserID(local_user(), true);
226 // Update global directory in background
227 $url = $_SESSION['my_url'];
228 if ($url && strlen(Config::get('system', 'directory'))) {
229 Worker::add(PRIORITY_LOW, "Directory", $url);
232 $a->internalRedirect('profile/' . $a->user['nickname']);
233 return; // NOTREACHED
235 $ph = Photo::getImageForPhoto($r[0]);
237 $imagecrop = profile_photo_crop_ui_head($a, $ph);
238 // go ahead as we have jus uploaded a new photo to crop
241 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
245 if (empty($imagecrop)) {
246 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
248 $o = Renderer::replaceMacros($tpl,
250 '$user' => $a->user['nickname'],
251 '$lbl_upfile' => L10n::t('Upload File:'),
252 '$lbl_profiles' => L10n::t('Select a profile:'),
253 '$title' => L10n::t('Upload Profile Photo'),
254 '$submit' => L10n::t('Upload'),
255 '$profiles' => $profiles,
256 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
257 '$select' => sprintf('%s %s', L10n::t('or'),
258 ($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>')
263 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
264 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
265 $o = Renderer::replaceMacros($tpl,
267 '$filename' => $filename,
268 '$profile' => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
269 '$resource' => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
270 '$image_url' => System::baseUrl() . '/photo/' . $filename,
271 '$title' => L10n::t('Crop Image'),
272 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
273 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
274 '$done' => L10n::t('Done Editing')
280 function profile_photo_crop_ui_head(App $a, Image $image)
282 $max_length = Config::get('system', 'max_image_length');
284 $max_length = MAX_IMAGE_LENGTH;
286 if ($max_length > 0) {
287 $image->scaleDown($max_length);
290 $width = $image->getWidth();
291 $height = $image->getHeight();
293 if ($width < 175 || $height < 175) {
294 $image->scaleUp(300);
295 $width = $image->getWidth();
296 $height = $image->getHeight();
299 $hash = Photo::newResource();
305 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
308 info(L10n::t('Image uploaded successfully.') . EOL);
310 notice(L10n::t('Image upload failed.') . EOL);
313 if ($width > 640 || $height > 640) {
314 $image->scaleDown(640);
315 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
318 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
324 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
328 'resolution' => $smallest,
329 'ext' => $image->getExt(),