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\Security;
19 use Friendica\Util\Strings;
21 function profile_photo_init(App $a)
27 Profile::load($a, $a->user['nickname']);
30 function profile_photo_post(App $a)
33 notice(L10n::t('Permission denied.') . EOL);
37 BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
39 if (!empty($_POST['cropfinal']) && $_POST['cropfinal'] == 1) {
41 // unless proven otherwise
42 $is_default_profile = 1;
44 if ($_REQUEST['profile']) {
45 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']),
49 if (DBA::isResult($r) && (!intval($r[0]['is-default']))) {
50 $is_default_profile = 0;
56 // phase 2 - we have finished cropping
59 notice(L10n::t('Image uploaded but image cropping failed.') . EOL);
63 $image_id = $a->argv[1];
65 if (substr($image_id, -2, 1) == '-') {
66 $scale = substr($image_id, -1, 1);
67 $image_id = substr($image_id, 0, -2);
71 $srcX = $_POST['xstart'];
72 $srcY = $_POST['ystart'];
73 $srcW = $_POST['xfinal'] - $srcX;
74 $srcH = $_POST['yfinal'] - $srcY;
76 $base_image = Photo::selectFirst([], ['resource-id' => $image_id, 'uid' => local_user(), 'scale' => $scale]);
78 $path = 'profile/' . $a->user['nickname'];
79 if (DBA::isResult($base_image)) {
81 $Image = Photo::getImageForPhoto($base_image);
82 if ($Image->isValid()) {
83 $Image->crop(300, $srcX, $srcY, $srcW, $srcH);
85 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
86 L10n::t('Profile Photos'), 4, $is_default_profile);
89 notice(L10n::t('Image size reduction [%s] failed.', "300") . EOL);
92 $Image->scaleDown(80);
94 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
95 L10n::t('Profile Photos'), 5, $is_default_profile);
98 notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
101 $Image->scaleDown(48);
103 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
104 L10n::t('Profile Photos'), 6, $is_default_profile);
107 notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
110 // If setting for the default profile, unset the profile photo flag from any other photos I own
112 if ($is_default_profile) {
113 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
114 DBA::escape($base_image['resource-id']), intval(local_user())
117 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
118 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
119 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
120 intval($_REQUEST['profile']), intval(local_user())
124 Contact::updateSelfFromUserID(local_user(), true);
126 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
127 // Update global directory in background
128 if ($path && strlen(Config::get('system', 'directory'))) {
129 Worker::add(PRIORITY_LOW, "Directory", $a->getBaseURL() . '/' . $path);
132 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
134 notice(L10n::t('Unable to process image') . EOL);
138 $a->internalRedirect($path);
139 return; // NOTREACHED
142 $src = $_FILES['userfile']['tmp_name'];
143 $filename = basename($_FILES['userfile']['name']);
144 $filesize = intval($_FILES['userfile']['size']);
145 $filetype = $_FILES['userfile']['type'];
146 if ($filetype == "") {
147 $filetype = Image::guessType($filename);
150 $maximagesize = Config::get('system', 'maximagesize');
152 if (($maximagesize) && ($filesize > $maximagesize)) {
153 notice(L10n::t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)) . EOL);
158 $imagedata = @file_get_contents($src);
159 $ph = new Image($imagedata, $filetype);
161 if (!$ph->isValid()) {
162 notice(L10n::t('Unable to process image.') . EOL);
170 $imagecrop = profile_photo_crop_ui_head($a, $ph);
171 $a->internalRedirect('profile_photo/use/' . $imagecrop['hash']);
174 function profile_photo_content(App $a)
178 notice(L10n::t('Permission denied.') . EOL);
184 if ($a->argc == 2 && $a->argv[1] === 'new') {
190 if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
191 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
193 $resource_id = $a->argv[2];
194 //die(":".local_user());
196 $r = Photo::select([], ["resource-id" => $resource_id, "uid" => local_user()], ["order" => ["scale"=>false]]);
199 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
200 DBA::escape($resource_id)
204 if (!DBA::isResult($r)) {
205 notice(L10n::t('Permission denied.') . EOL);
210 foreach ($r as $rr) {
211 if ($rr['scale'] == 5) {
216 // set an already uloaded photo as profile photo
217 // if photo is in 'Profile Photos', change it in db
218 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
219 $r = q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
221 $r = q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
222 DBA::escape($resource_id)
225 Contact::updateSelfFromUserID(local_user(), true);
227 // Update global directory in background
228 $url = $_SESSION['my_url'];
229 if ($url && strlen(Config::get('system', 'directory'))) {
230 Worker::add(PRIORITY_LOW, "Directory", $url);
233 $a->internalRedirect('profile/' . $a->user['nickname']);
234 return; // NOTREACHED
236 $ph = Photo::getImageForPhoto($r[0]);
238 $imagecrop = profile_photo_crop_ui_head($a, $ph);
239 // go ahead as we have jus uploaded a new photo to crop
242 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
246 if (empty($imagecrop)) {
247 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
249 $o = Renderer::replaceMacros($tpl,
251 '$user' => $a->user['nickname'],
252 '$lbl_upfile' => L10n::t('Upload File:'),
253 '$lbl_profiles' => L10n::t('Select a profile:'),
254 '$title' => L10n::t('Upload Profile Photo'),
255 '$submit' => L10n::t('Upload'),
256 '$profiles' => $profiles,
257 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
258 '$select' => sprintf('%s %s', L10n::t('or'),
259 ($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>')
264 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
265 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
266 $o = Renderer::replaceMacros($tpl,
268 '$filename' => $filename,
269 '$profile' => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
270 '$resource' => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
271 '$image_url' => System::baseUrl() . '/photo/' . $filename,
272 '$title' => L10n::t('Crop Image'),
273 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
274 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
275 '$done' => L10n::t('Done Editing')
280 return; // NOTREACHED
283 function profile_photo_crop_ui_head(App $a, Image $image)
285 $max_length = Config::get('system', 'max_image_length');
287 $max_length = MAX_IMAGE_LENGTH;
289 if ($max_length > 0) {
290 $image->scaleDown($max_length);
293 $width = $image->getWidth();
294 $height = $image->getHeight();
296 if ($width < 175 || $height < 175) {
297 $image->scaleUp(300);
298 $width = $image->getWidth();
299 $height = $image->getHeight();
302 $hash = Photo::newResource();
308 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
311 info(L10n::t('Image uploaded successfully.') . EOL);
313 notice(L10n::t('Image upload failed.') . EOL);
316 if ($width > 640 || $height > 640) {
317 $image->scaleDown(640);
318 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
321 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
327 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
331 'resolution' => $smallest,
332 'ext' => $image->getExt(),