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;
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 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1", DBA::escape($image_id),
76 DBA::escape(local_user()), intval($scale));
78 $path = 'profile/' . $a->user['nickname'];
79 if (DBA::isResult($r)) {
82 $Image = new Image($base_image['data'], $base_image['type']);
83 if ($Image->isValid()) {
84 $Image->crop(300, $srcX, $srcY, $srcW, $srcH);
86 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
87 L10n::t('Profile Photos'), 4, $is_default_profile);
90 notice(L10n::t('Image size reduction [%s] failed.', "300") . EOL);
93 $Image->scaleDown(80);
95 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
96 L10n::t('Profile Photos'), 5, $is_default_profile);
99 notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
102 $Image->scaleDown(48);
104 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
105 L10n::t('Profile Photos'), 6, $is_default_profile);
108 notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
111 // If setting for the default profile, unset the profile photo flag from any other photos I own
113 if ($is_default_profile) {
114 $r = q("UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
115 DBA::escape($base_image['resource-id']), intval(local_user())
118 $r = q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
119 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
120 DBA::escape(System::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
121 intval($_REQUEST['profile']), intval(local_user())
125 Contact::updateSelfFromUserID(local_user(), true);
127 info(L10n::t('Shift-reload the page or clear browser cache if the new photo does not display immediately.') . EOL);
128 // Update global directory in background
129 if ($path && strlen(Config::get('system', 'directory'))) {
130 Worker::add(PRIORITY_LOW, "Directory", $a->getBaseURL() . '/' . $path);
133 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
135 notice(L10n::t('Unable to process image') . EOL);
139 $a->internalRedirect($path);
140 return; // NOTREACHED
143 $src = $_FILES['userfile']['tmp_name'];
144 $filename = basename($_FILES['userfile']['name']);
145 $filesize = intval($_FILES['userfile']['size']);
146 $filetype = $_FILES['userfile']['type'];
147 if ($filetype == "") {
148 $filetype = Image::guessType($filename);
151 $maximagesize = Config::get('system', 'maximagesize');
153 if (($maximagesize) && ($filesize > $maximagesize)) {
154 notice(L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize)) . EOL);
159 $imagedata = @file_get_contents($src);
160 $ph = new Image($imagedata, $filetype);
162 if (!$ph->isValid()) {
163 notice(L10n::t('Unable to process image.') . EOL);
171 $imagecrop = profile_photo_crop_ui_head($a, $ph);
172 $a->internalRedirect('profile_photo/use/' . $imagecrop['hash']);
175 function profile_photo_content(App $a)
179 notice(L10n::t('Permission denied.') . EOL);
185 if ($a->argc == 2 && $a->argv[1] === 'new') {
191 if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
192 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
194 $resource_id = $a->argv[2];
195 //die(":".local_user());
196 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
197 DBA::escape($resource_id)
200 if (!DBA::isResult($r)) {
201 notice(L10n::t('Permission denied.') . EOL);
206 foreach ($r as $rr) {
207 if ($rr['scale'] == 5) {
212 // set an already uloaded photo as profile photo
213 // if photo is in 'Profile Photos', change it in db
214 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
215 $r = q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
217 $r = q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
218 DBA::escape($resource_id)
221 Contact::updateSelfFromUserID(local_user(), true);
223 // Update global directory in background
224 $url = $_SESSION['my_url'];
225 if ($url && strlen(Config::get('system', 'directory'))) {
226 Worker::add(PRIORITY_LOW, "Directory", $url);
229 $a->internalRedirect('profile/' . $a->user['nickname']);
230 return; // NOTREACHED
232 $ph = new Image($r[0]['data'], $r[0]['type']);
233 $imagecrop = profile_photo_crop_ui_head($a, $ph);
234 // go ahead as we have jus uploaded a new photo to crop
237 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
241 if (empty($imagecrop)) {
242 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
244 $o = Renderer::replaceMacros($tpl,
246 '$user' => $a->user['nickname'],
247 '$lbl_upfile' => L10n::t('Upload File:'),
248 '$lbl_profiles' => L10n::t('Select a profile:'),
249 '$title' => L10n::t('Upload Profile Photo'),
250 '$submit' => L10n::t('Upload'),
251 '$profiles' => $profiles,
252 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
253 '$select' => sprintf('%s %s', L10n::t('or'),
254 ($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>')
259 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
260 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
261 $o = Renderer::replaceMacros($tpl,
263 '$filename' => $filename,
264 '$profile' => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
265 '$resource' => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
266 '$image_url' => System::baseUrl() . '/photo/' . $filename,
267 '$title' => L10n::t('Crop Image'),
268 '$desc' => L10n::t('Please adjust the image cropping for optimum viewing.'),
269 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
270 '$done' => L10n::t('Done Editing')
275 return; // NOTREACHED
278 function profile_photo_crop_ui_head(App $a, Image $image)
280 $max_length = Config::get('system', 'max_image_length');
282 $max_length = MAX_IMAGE_LENGTH;
284 if ($max_length > 0) {
285 $image->scaleDown($max_length);
288 $width = $image->getWidth();
289 $height = $image->getHeight();
291 if ($width < 175 || $height < 175) {
292 $image->scaleUp(300);
293 $width = $image->getWidth();
294 $height = $image->getHeight();
297 $hash = Photo::newResource();
303 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
306 info(L10n::t('Image uploaded successfully.') . EOL);
308 notice(L10n::t('Image upload failed.') . EOL);
311 if ($width > 640 || $height > 640) {
312 $image->scaleDown(640);
313 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
316 notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
322 $a->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
326 'resolution' => $smallest,
327 'ext' => $image->getExt(),