]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Profile/Photo/Crop.php
old boot.php functions replaced in src/module (3)
[friendica.git] / src / Module / Settings / Profile / Photo / Crop.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings\Profile\Photo;
23
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Session;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
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;
33
34 class Crop extends BaseSettings
35 {
36         protected function post(array $request = [])
37         {
38                 if (!Session::isAuthenticated()) {
39                         return;
40                 }
41
42                 $photo_prefix = $this->parameters['guid'];
43                 $resource_id = $photo_prefix;
44                 $scale = 0;
45                 if (substr($photo_prefix, -2, 1) == '-') {
46                         list($resource_id, $scale) = explode('-', $photo_prefix);
47                 }
48
49                 self::checkFormSecurityTokenRedirectOnError('settings/profile/photo/crop/' . $photo_prefix, 'settings_profile_photo_crop');
50
51                 $action = $_POST['action'] ?? 'crop';
52
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);
58
59                 $path = 'profile/' . DI::app()->getLoggedInUserNickname();
60
61                 $base_image = Photo::selectFirst([], ['resource-id' => $resource_id, 'uid' => Session::getLocalUser(), 'scale' => $scale]);
62                 if (DBA::isResult($base_image)) {
63                         $Image = Photo::getImageForPhoto($base_image);
64                         if (empty($Image)) {
65                                 throw new HTTPException\InternalServerErrorException();
66                         }
67
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' => Session::getLocalUser()]);
71
72                                 // Normalizing expected square crop parameters
73                                 $selectionW = $selectionH = min($selectionW, $selectionH);
74
75                                 $imageIsSquare = $Image->getWidth() === $Image->getHeight();
76                                 $selectionIsFullImage = $selectionX === 0 && $selectionY === 0 && $selectionW === $Image->getWidth() && $selectionH === $Image->getHeight();
77
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());
82                                         $action = 'crop';
83                                 }
84
85                                 // Selective crop if it was asked and the selection isn't the full image
86                                 if ($action == 'crop'
87                                         && !($imageIsSquare && !$selectionIsFullImage)
88                                 ) {
89                                         $Image->crop(300, $selectionX, $selectionY, $selectionW, $selectionH);
90                                         $resource_id = Photo::newResource();
91                                 } else {
92                                         $Image->scaleDown(300);
93                                 }
94
95                                 $condition = ['resource-id' => $resource_id, 'uid' => Session::getLocalUser(), 'contact-id' => 0];
96
97                                 $r = Photo::store(
98                                         $Image,
99                                         Session::getLocalUser(),
100                                         0,
101                                         $resource_id,
102                                         $base_image['filename'],
103                                         DI::l10n()->t(Photo::PROFILE_PHOTOS),
104                                         4,
105                                         Photo::USER_AVATAR
106                                 );
107                                 if ($r === false) {
108                                         DI::sysmsg()->addNotice(DI::l10n()->t('Image size reduction [%s] failed.', '300'));
109                                 } else {
110                                         Photo::update(['profile' => true], array_merge($condition, ['scale' => 4]));
111                                 }
112
113                                 $Image->scaleDown(80);
114
115                                 $r = Photo::store(
116                                         $Image,
117                                         Session::getLocalUser(),
118                                         0,
119                                         $resource_id,
120                                         $base_image['filename'],
121                                         DI::l10n()->t(Photo::PROFILE_PHOTOS),
122                                         5,
123                                         Photo::USER_AVATAR
124                                 );
125                                 if ($r === false) {
126                                         DI::sysmsg()->addNotice(DI::l10n()->t('Image size reduction [%s] failed.', '80'));
127                                 } else {
128                                         Photo::update(['profile' => true], array_merge($condition, ['scale' => 5]));
129                                 }
130
131                                 $Image->scaleDown(48);
132
133                                 $r = Photo::store(
134                                         $Image,
135                                         Session::getLocalUser(),
136                                         0,
137                                         $resource_id,
138                                         $base_image['filename'],
139                                         DI::l10n()->t(Photo::PROFILE_PHOTOS),
140                                         6,
141                                         Photo::USER_AVATAR
142                                 );
143                                 if ($r === false) {
144                                         DI::sysmsg()->addNotice(DI::l10n()->t('Image size reduction [%s] failed.', '48'));
145                                 } else {
146                                         Photo::update(['profile' => true], array_merge($condition, ['scale' => 6]));
147                                 }
148
149                                 Contact::updateSelfFromUserID(Session::getLocalUser(), true);
150
151                                 DI::sysmsg()->addInfo(DI::l10n()->t('Shift-reload the page or clear browser cache if the new photo does not display immediately.'));
152
153                                 // Update global directory in background
154                                 Profile::publishUpdate(Session::getLocalUser());
155                         } else {
156                                 DI::sysmsg()->addNotice(DI::l10n()->t('Unable to process image'));
157                         }
158                 }
159
160                 DI::baseUrl()->redirect($path);
161         }
162
163         protected function content(array $request = []): string
164         {
165                 if (!Session::isAuthenticated()) {
166                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
167                 }
168
169                 parent::content();
170
171                 $resource_id = $this->parameters['guid'];
172
173                 $photos = Photo::selectToArray([], ['resource-id' => $resource_id, 'uid' => Session::getLocalUser()], ['order' => ['scale' => false]]);
174                 if (!DBA::isResult($photos)) {
175                         throw new HTTPException\NotFoundException(DI::l10n()->t('Photo not found.'));
176                 }
177
178                 $havescale = false;
179                 $smallest = 0;
180                 foreach ($photos as $photo) {
181                         $smallest = $photo['scale'] == 1 ? 1 : $smallest;
182                         $havescale = $havescale || $photo['scale'] == 5;
183                 }
184
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' => Session::getLocalUser()]);
189
190                         Photo::update(['profile' => true], ['resource-id' => $resource_id, 'uid' => Session::getLocalUser()]);
191
192                         Contact::updateSelfFromUserID(Session::getLocalUser(), true);
193
194                         // Update global directory in background
195                         Profile::publishUpdate(Session::getLocalUser());
196
197                         DI::sysmsg()->addInfo(DI::l10n()->t('Profile picture successfully updated.'));
198
199                         DI::baseUrl()->redirect('profile/' . DI::app()->getLoggedInUserNickname());
200                 }
201
202                 $Image = Photo::getImageForPhoto($photos[0]);
203                 if (empty($Image)) {
204                         throw new HTTPException\InternalServerErrorException();
205                 }
206
207                 $imagecrop = [
208                         'resource-id' => $resource_id,
209                         'scale'       => $smallest,
210                         'ext'         => $Image->getExt(),
211                 ];
212
213                 $isSquare = $Image->getWidth() === $Image->getHeight();
214
215                 DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/profile/photo/crop_head.tpl'), []);
216
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'),
228                 ]);
229
230                 return $o;
231         }
232 }