3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Settings\Profile\Photo;
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Session;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Photo;
30 use Friendica\Model\Profile;
31 use Friendica\Module\BaseSettings;
32 use Friendica\Network\HTTPException;
34 class Crop extends BaseSettings
36 protected function post(array $request = [], array $post = [])
38 if (!Session::isAuthenticated()) {
42 $photo_prefix = $this->parameters['guid'];
43 $resource_id = $photo_prefix;
45 if (substr($photo_prefix, -2, 1) == '-') {
46 list($resource_id, $scale) = explode('-', $photo_prefix);
49 self::checkFormSecurityTokenRedirectOnError('settings/profile/photo/crop/' . $photo_prefix, 'settings_profile_photo_crop');
51 $action = $_POST['action'] ?? 'crop';
53 // Image selection origin is top left
54 $selectionX = intval($_POST['xstart'] ?? 0);
55 $selectionY = intval($_POST['ystart'] ?? 0);
56 $selectionW = intval($_POST['width'] ?? 0);
57 $selectionH = intval($_POST['height'] ?? 0);
59 $path = 'profile/' . DI::app()->getLoggedInUserNickname();
61 $base_image = Photo::selectFirst([], ['resource-id' => $resource_id, 'uid' => local_user(), 'scale' => $scale]);
62 if (DBA::isResult($base_image)) {
63 $Image = Photo::getImageForPhoto($base_image);
65 throw new HTTPException\InternalServerErrorException();
68 if ($Image->isValid()) {
69 // If setting for the default profile, unset the profile photo flag from any other photos I own
70 DBA::update('photo', ['profile' => 0], ['uid' => local_user()]);
72 // Normalizing expected square crop parameters
73 $selectionW = $selectionH = min($selectionW, $selectionH);
75 $imageIsSquare = $Image->getWidth() === $Image->getHeight();
76 $selectionIsFullImage = $selectionX === 0 && $selectionY === 0 && $selectionW === $Image->getWidth() && $selectionH === $Image->getHeight();
78 // Bypassed UI with a rectangle image, we force a square cropped image
79 if (!$imageIsSquare && $action == 'skip') {
80 $selectionX = $selectionY = 0;
81 $selectionW = $selectionH = min($Image->getWidth(), $Image->getHeight());
85 // Selective crop if it was asked and the selection isn't the full image
87 && !($imageIsSquare && !$selectionIsFullImage)
89 $Image->crop(300, $selectionX, $selectionY, $selectionW, $selectionH);
90 $resource_id = Photo::newResource();
92 $Image->scaleDown(300);
95 $condition = ['resource-id' => $resource_id, 'uid' => local_user(), 'contact-id' => 0];
102 $base_image['filename'],
103 DI::l10n()->t(Photo::PROFILE_PHOTOS),
108 notice(DI::l10n()->t('Image size reduction [%s] failed.', '300'));
110 Photo::update(['profile' => true], array_merge($condition, ['scale' => 4]));
113 $Image->scaleDown(80);
120 $base_image['filename'],
121 DI::l10n()->t(Photo::PROFILE_PHOTOS),
126 notice(DI::l10n()->t('Image size reduction [%s] failed.', '80'));
128 Photo::update(['profile' => true], array_merge($condition, ['scale' => 5]));
131 $Image->scaleDown(48);
138 $base_image['filename'],
139 DI::l10n()->t(Photo::PROFILE_PHOTOS),
144 notice(DI::l10n()->t('Image size reduction [%s] failed.', '48'));
146 Photo::update(['profile' => true], array_merge($condition, ['scale' => 6]));
149 Contact::updateSelfFromUserID(local_user(), true);
151 info(DI::l10n()->t('Shift-reload the page or clear browser cache if the new photo does not display immediately.'));
153 // Update global directory in background
154 Profile::publishUpdate(local_user());
156 notice(DI::l10n()->t('Unable to process image'));
160 DI::baseUrl()->redirect($path);
163 protected function content(array $request = []): string
165 if (!Session::isAuthenticated()) {
166 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
171 $resource_id = $this->parameters['guid'];
173 $photos = Photo::selectToArray([], ['resource-id' => $resource_id, 'uid' => local_user()], ['order' => ['scale' => false]]);
174 if (!DBA::isResult($photos)) {
175 throw new HTTPException\NotFoundException(DI::l10n()->t('Photo not found.'));
180 foreach ($photos as $photo) {
181 $smallest = $photo['scale'] == 1 ? 1 : $smallest;
182 $havescale = $havescale || $photo['scale'] == 5;
185 // set an already uloaded photo as profile photo
186 // if photo is in 'Profile Photos', change it in db
187 if ($photos[0]['photo-type'] == Photo::USER_AVATAR && $havescale) {
188 Photo::update(['profile' => false], ['uid' => local_user()]);
190 Photo::update(['profile' => true], ['resource-id' => $resource_id, 'uid' => local_user()]);
192 Contact::updateSelfFromUserID(local_user(), true);
194 // Update global directory in background
195 Profile::publishUpdate(local_user());
197 info(DI::l10n()->t('Profile picture successfully updated.'));
199 DI::baseUrl()->redirect('profile/' . DI::app()->getLoggedInUserNickname());
202 $Image = Photo::getImageForPhoto($photos[0]);
204 throw new HTTPException\InternalServerErrorException();
208 'resource-id' => $resource_id,
209 'scale' => $smallest,
210 'ext' => $Image->getExt(),
213 $isSquare = $Image->getWidth() === $Image->getHeight();
215 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/profile/photo/crop_head.tpl'), []);
217 $filename = $imagecrop['resource-id'] . '-' . $imagecrop['scale'] . '.' . $imagecrop['ext'];
218 $tpl = Renderer::getMarkupTemplate('settings/profile/photo/crop.tpl');
219 $o = Renderer::replaceMacros($tpl, [
220 '$filename' => $filename,
221 '$resource' => $imagecrop['resource-id'] . '-' . $imagecrop['scale'],
222 '$image_url' => DI::baseUrl() . '/photo/' . $filename,
223 '$title' => DI::l10n()->t('Crop Image'),
224 '$desc' => DI::l10n()->t('Please adjust the image cropping for optimum viewing.'),
225 '$form_security_token' => self::getFormSecurityToken('settings_profile_photo_crop'),
226 '$skip' => $isSquare ? DI::l10n()->t('Use Image As Is') : '',
227 '$crop' => DI::l10n()->t('Crop Image'),