3 * @file mod/profile_photo.php
7 use Friendica\BaseModule;
8 use Friendica\Core\Config;
9 use Friendica\Core\Renderer;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
13 use Friendica\Model\Contact;
14 use Friendica\Model\Photo;
15 use Friendica\Model\Profile;
16 use Friendica\Object\Image;
17 use Friendica\Util\Strings;
19 function profile_photo_init(App $a)
25 Profile::load($a, $a->user['nickname']);
28 function profile_photo_post(App $a)
31 notice(DI::l10n()->t('Permission denied.') . EOL);
35 BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
37 if (!empty($_POST['cropfinal']) && $_POST['cropfinal'] == 1) {
39 // unless proven otherwise
40 $is_default_profile = 1;
42 if ($_REQUEST['profile']) {
43 $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']),
47 if (DBA::isResult($r) && (!intval($r[0]['is-default']))) {
48 $is_default_profile = 0;
54 // phase 2 - we have finished cropping
57 notice(DI::l10n()->t('Image uploaded but image cropping failed.') . EOL);
61 $image_id = $a->argv[1];
63 if (substr($image_id, -2, 1) == '-') {
64 $scale = substr($image_id, -1, 1);
65 $image_id = substr($image_id, 0, -2);
69 $srcX = $_POST['xstart'];
70 $srcY = $_POST['ystart'];
71 $srcW = $_POST['xfinal'] - $srcX;
72 $srcH = $_POST['yfinal'] - $srcY;
74 $base_image = Photo::selectFirst([], ['resource-id' => $image_id, 'uid' => local_user(), 'scale' => $scale]);
76 $path = 'profile/' . $a->user['nickname'];
77 if (DBA::isResult($base_image)) {
79 $Image = Photo::getImageForPhoto($base_image);
80 if ($Image->isValid()) {
81 $Image->crop(300, $srcX, $srcY, $srcW, $srcH);
83 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
84 DI::l10n()->t('Profile Photos'), 4, $is_default_profile);
87 notice(DI::l10n()->t('Image size reduction [%s] failed.', "300") . EOL);
90 $Image->scaleDown(80);
92 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
93 DI::l10n()->t('Profile Photos'), 5, $is_default_profile);
96 notice(DI::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 DI::l10n()->t('Profile Photos'), 6, $is_default_profile);
105 notice(DI::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 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 q("update profile set photo = '%s', thumb = '%s' where id = %d and uid = %d",
116 DBA::escape(DI::baseUrl() . '/photo/' . $base_image['resource-id'] . '-4.' . $Image->getExt()),
117 DBA::escape(DI::baseUrl() . '/photo/' . $base_image['resource-id'] . '-5.' . $Image->getExt()),
118 intval($_REQUEST['profile']), intval(local_user())
122 Contact::updateSelfFromUserID(local_user(), true);
124 info(DI::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 ($path && strlen(Config::get('system', 'directory'))) {
127 Worker::add(PRIORITY_LOW, "Directory", DI::baseUrl()->get() . '/' . $path);
130 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
132 notice(DI::l10n()->t('Unable to process image') . EOL);
136 DI::baseUrl()->redirect($path);
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(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)) . EOL);
156 $imagedata = @file_get_contents($src);
157 $ph = new Image($imagedata, $filetype);
159 if (!$ph->isValid()) {
160 notice(DI::l10n()->t('Unable to process image.') . EOL);
168 $imagecrop = profile_photo_crop_ui_head($ph);
169 DI::baseUrl()->redirect('profile_photo/use/' . $imagecrop['hash']);
172 function profile_photo_content(App $a)
176 notice(DI::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 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
191 $resource_id = $a->argv[2];
192 //die(":".local_user());
194 $r = Photo::selectToArray([], ["resource-id" => $resource_id, "uid" => local_user()], ["order" => ["scale" => false]]);
195 if (!DBA::isResult($r)) {
196 notice(DI::l10n()->t('Permission denied.') . EOL);
201 foreach ($r as $rr) {
202 if ($rr['scale'] == 5) {
207 // set an already uloaded photo as profile photo
208 // if photo is in 'Profile Photos', change it in db
209 if (($r[0]['album'] == DI::l10n()->t('Profile Photos')) && ($havescale)) {
210 q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
212 q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
213 DBA::escape($resource_id)
216 Contact::updateSelfFromUserID(local_user(), true);
218 // Update global directory in background
219 $url = $_SESSION['my_url'];
220 if ($url && strlen(Config::get('system', 'directory'))) {
221 Worker::add(PRIORITY_LOW, "Directory", $url);
224 DI::baseUrl()->redirect('profile/' . $a->user['nickname']);
225 return; // NOTREACHED
227 $ph = Photo::getImageForPhoto($r[0]);
229 $imagecrop = profile_photo_crop_ui_head($ph);
230 // go ahead as we have jus uploaded a new photo to crop
233 $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
237 if (empty($imagecrop)) {
238 $tpl = Renderer::getMarkupTemplate('profile_photo.tpl');
240 $o = Renderer::replaceMacros($tpl,
242 '$user' => $a->user['nickname'],
243 '$lbl_upfile' => DI::l10n()->t('Upload File:'),
244 '$lbl_profiles' => DI::l10n()->t('Select a profile:'),
245 '$title' => DI::l10n()->t('Upload Profile Photo'),
246 '$submit' => DI::l10n()->t('Upload'),
247 '$profiles' => $profiles,
248 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
249 '$select' => sprintf('%s %s', DI::l10n()->t('or'),
250 ($newuser) ? '<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('skip this step') . '</a>' : '<a href="' . DI::baseUrl() . '/photos/' . $a->user['nickname'] . '">' . DI::l10n()->t('select a photo from your photo albums') . '</a>')
255 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
256 $tpl = Renderer::getMarkupTemplate("cropbody.tpl");
257 $o = Renderer::replaceMacros($tpl,
259 '$filename' => $filename,
260 '$profile' => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
261 '$resource' => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
262 '$image_url' => DI::baseUrl() . '/photo/' . $filename,
263 '$title' => DI::l10n()->t('Crop Image'),
264 '$desc' => DI::l10n()->t('Please adjust the image cropping for optimum viewing.'),
265 '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
266 '$done' => DI::l10n()->t('Done Editing')
272 function profile_photo_crop_ui_head(Image $image)
274 $max_length = Config::get('system', 'max_image_length');
276 $max_length = MAX_IMAGE_LENGTH;
278 if ($max_length > 0) {
279 $image->scaleDown($max_length);
282 $width = $image->getWidth();
283 $height = $image->getHeight();
285 if ($width < 175 || $height < 175) {
286 $image->scaleUp(300);
287 $width = $image->getWidth();
288 $height = $image->getHeight();
291 $hash = Photo::newResource();
297 $r = Photo::store($image, local_user(), 0, $hash, $filename, DI::l10n()->t('Profile Photos'), 0);
300 info(DI::l10n()->t('Image uploaded successfully.') . EOL);
302 notice(DI::l10n()->t('Image upload failed.') . EOL);
305 if ($width > 640 || $height > 640) {
306 $image->scaleDown(640);
307 $r = Photo::store($image, local_user(), 0, $hash, $filename, DI::l10n()->t('Profile Photos'), 1);
310 notice(DI::l10n()->t('Image size reduction [%s] failed.', "640") . EOL);
316 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate("crophead.tpl"), []);
320 'resolution' => $smallest,
321 'ext' => $image->getExt(),