]> git.mxchange.org Git - friendica.git/blob - mod/profile_photo.php
raising profile photo size to comply with frio sidebar profile photo
[friendica.git] / mod / profile_photo.php
1 <?php
2 /**
3  * @file mod/profile_photo.php
4  */
5
6 use Friendica\App;
7 use Friendica\BaseModule;
8 use Friendica\Core\Config;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Core\Worker;
12 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\Security;
18
19 function profile_photo_init(App $a)
20 {
21         if (!local_user()) {
22                 return;
23         }
24
25         Profile::load($a, $a->user['nickname']);
26 }
27
28 function profile_photo_post(App $a)
29 {
30         if (!local_user()) {
31                 notice(L10n::t('Permission denied.') . EOL);
32                 return;
33         }
34
35         BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
36
37         if (!empty($_POST['cropfinal']) && $_POST['cropfinal'] == 1) {
38
39                 // unless proven otherwise
40                 $is_default_profile = 1;
41
42                 if ($_REQUEST['profile']) {
43                         $r = q("select id, `is-default` from profile where id = %d and uid = %d limit 1", intval($_REQUEST['profile']),
44                                 intval(local_user())
45                         );
46
47                         if (DBA::isResult($r) && (!intval($r[0]['is-default']))) {
48                                 $is_default_profile = 0;
49                         }
50                 }
51
52
53
54                 // phase 2 - we have finished cropping
55
56                 if ($a->argc != 2) {
57                         notice(L10n::t('Image uploaded but image cropping failed.') . EOL);
58                         return;
59                 }
60
61                 $image_id = $a->argv[1];
62
63                 if (substr($image_id, -2, 1) == '-') {
64                         $scale = substr($image_id, -1, 1);
65                         $image_id = substr($image_id, 0, -2);
66                 }
67
68
69                 $srcX = $_POST['xstart'];
70                 $srcY = $_POST['ystart'];
71                 $srcW = $_POST['xfinal'] - $srcX;
72                 $srcH = $_POST['yfinal'] - $srcY;
73
74                 $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `scale` = %d LIMIT 1", DBA::escape($image_id),
75                         DBA::escape(local_user()), intval($scale));
76
77                 $path = 'profile/' . $a->user['nickname'];
78                 if (DBA::isResult($r)) {
79                         $base_image = $r[0];
80
81                         $Image = new Image($base_image['data'], $base_image['type']);
82                         if ($Image->isValid()) {
83                                 $Image->crop(300, $srcX, $srcY, $srcW, $srcH);
84
85                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
86                                                 L10n::t('Profile Photos'), 4, $is_default_profile);
87
88                                 if ($r === false) {
89                                         notice(L10n::t('Image size reduction [%s] failed.', "300") . EOL);
90                                 }
91
92                                 $Image->scaleDown(80);
93
94                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
95                                                 L10n::t('Profile Photos'), 5, $is_default_profile);
96
97                                 if ($r === false) {
98                                         notice(L10n::t('Image size reduction [%s] failed.', "80") . EOL);
99                                 }
100
101                                 $Image->scaleDown(48);
102
103                                 $r = Photo::store($Image, local_user(), 0, $base_image['resource-id'], $base_image['filename'],
104                                                 L10n::t('Profile Photos'), 6, $is_default_profile);
105
106                                 if ($r === false) {
107                                         notice(L10n::t('Image size reduction [%s] failed.', "48") . EOL);
108                                 }
109
110                                 // If setting for the default profile, unset the profile photo flag from any other photos I own
111
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())
115                                         );
116                                 } else {
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())
121                                         );
122                                 }
123
124                                 Contact::updateSelfFromUserID(local_user(), true);
125
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);
130                                 }
131
132                                 Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
133                         } else {
134                                 notice(L10n::t('Unable to process image') . EOL);
135                         }
136                 }
137
138                 $a->internalRedirect($path);
139                 return; // NOTREACHED
140         }
141
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);
148         }
149
150         $maximagesize = Config::get('system', 'maximagesize');
151
152         if (($maximagesize) && ($filesize > $maximagesize)) {
153                 notice(L10n::t('Image exceeds size limit of %s', formatBytes($maximagesize)) . EOL);
154                 @unlink($src);
155                 return;
156         }
157
158         $imagedata = @file_get_contents($src);
159         $ph = new Image($imagedata, $filetype);
160
161         if (!$ph->isValid()) {
162                 notice(L10n::t('Unable to process image.') . EOL);
163                 @unlink($src);
164                 return;
165         }
166
167         $ph->orient($src);
168         @unlink($src);
169
170         $imagecrop = profile_photo_crop_ui_head($a, $ph);
171         $a->internalRedirect('profile_photo/use/' . $imagecrop['hash']);
172 }
173
174 function profile_photo_content(App $a)
175 {
176
177         if (!local_user()) {
178                 notice(L10n::t('Permission denied.') . EOL);
179                 return;
180         }
181
182         $newuser = false;
183
184         if ($a->argc == 2 && $a->argv[1] === 'new') {
185                 $newuser = true;
186         }
187
188         $imagecrop = [];
189
190         if (isset($a->argv[1]) && $a->argv[1] == 'use' && $a->argc >= 3) {
191                 // BaseModule::checkFormSecurityTokenRedirectOnError('/profile_photo', 'profile_photo');
192
193                 $resource_id = $a->argv[2];
194                 //die(":".local_user());
195                 $r = q("SELECT * FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' ORDER BY `scale` ASC", intval(local_user()),
196                         DBA::escape($resource_id)
197                 );
198
199                 if (!DBA::isResult($r)) {
200                         notice(L10n::t('Permission denied.') . EOL);
201                         return;
202                 }
203
204                 $havescale = false;
205                 foreach ($r as $rr) {
206                         if ($rr['scale'] == 5) {
207                                 $havescale = true;
208                         }
209                 }
210
211                 // set an already uloaded photo as profile photo
212                 // if photo is in 'Profile Photos', change it in db
213                 if (($r[0]['album'] == L10n::t('Profile Photos')) && ($havescale)) {
214                         $r = q("UPDATE `photo` SET `profile`=0 WHERE `profile`=1 AND `uid`=%d", intval(local_user()));
215
216                         $r = q("UPDATE `photo` SET `profile`=1 WHERE `uid` = %d AND `resource-id` = '%s'", intval(local_user()),
217                                 DBA::escape($resource_id)
218                         );
219
220                         Contact::updateSelfFromUserID(local_user(), true);
221
222                         // Update global directory in background
223                         $url = $_SESSION['my_url'];
224                         if ($url && strlen(Config::get('system', 'directory'))) {
225                                 Worker::add(PRIORITY_LOW, "Directory", $url);
226                         }
227
228                         $a->internalRedirect('profile/' . $a->user['nickname']);
229                         return; // NOTREACHED
230                 }
231                 $ph = new Image($r[0]['data'], $r[0]['type']);
232                 $imagecrop = profile_photo_crop_ui_head($a, $ph);
233                 // go ahead as we have jus uploaded a new photo to crop
234         }
235
236         $profiles = q("select `id`,`profile-name` as `name`,`is-default` as `default` from profile where uid = %d",
237                 intval(local_user())
238         );
239
240         if (empty($imagecrop)) {
241                 $tpl = get_markup_template('profile_photo.tpl');
242
243                 $o = replace_macros($tpl,
244                         [
245                         '$user' => $a->user['nickname'],
246                         '$lbl_upfile' => L10n::t('Upload File:'),
247                         '$lbl_profiles' => L10n::t('Select a profile:'),
248                         '$title' => L10n::t('Upload Profile Photo'),
249                         '$submit' => L10n::t('Upload'),
250                         '$profiles' => $profiles,
251                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
252                         '$select' => sprintf('%s %s', L10n::t('or'),
253                                 ($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>')
254                 ]);
255
256                 return $o;
257         } else {
258                 $filename = $imagecrop['hash'] . '-' . $imagecrop['resolution'] . '.' . $imagecrop['ext'];
259                 $tpl = get_markup_template("cropbody.tpl");
260                 $o = replace_macros($tpl,
261                         [
262                         '$filename'  => $filename,
263                         '$profile'   => (isset($_REQUEST['profile']) ? intval($_REQUEST['profile']) : 0),
264                         '$resource'  => $imagecrop['hash'] . '-' . $imagecrop['resolution'],
265                         '$image_url' => System::baseUrl() . '/photo/' . $filename,
266                         '$title'     => L10n::t('Crop Image'),
267                         '$desc'      => L10n::t('Please adjust the image cropping for optimum viewing.'),
268                         '$form_security_token' => BaseModule::getFormSecurityToken("profile_photo"),
269                         '$done'      => L10n::t('Done Editing')
270                 ]);
271                 return $o;
272         }
273
274         return; // NOTREACHED
275 }
276
277 function profile_photo_crop_ui_head(App $a, Image $image)
278 {
279         $max_length = Config::get('system', 'max_image_length');
280         if (!$max_length) {
281                 $max_length = MAX_IMAGE_LENGTH;
282         }
283         if ($max_length > 0) {
284                 $image->scaleDown($max_length);
285         }
286
287         $width = $image->getWidth();
288         $height = $image->getHeight();
289
290         if ($width < 175 || $height < 175) {
291                 $image->scaleUp(200);
292                 $width = $image->getWidth();
293                 $height = $image->getHeight();
294         }
295
296         $hash = Photo::newResource();
297
298
299         $smallest = 0;
300         $filename = '';
301
302         $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 0);
303
304         if ($r) {
305                 info(L10n::t('Image uploaded successfully.') . EOL);
306         } else {
307                 notice(L10n::t('Image upload failed.') . EOL);
308         }
309
310         if ($width > 640 || $height > 640) {
311                 $image->scaleDown(640);
312                 $r = Photo::store($image, local_user(), 0, $hash, $filename, L10n::t('Profile Photos'), 1);
313
314                 if ($r === false) {
315                         notice(L10n::t('Image size reduction [%s] failed.', "640") . EOL);
316                 } else {
317                         $smallest = 1;
318                 }
319         }
320
321         $a->page['htmlhead'] .= replace_macros(get_markup_template("crophead.tpl"), []);
322
323         $imagecrop = [
324                 'hash'       => $hash,
325                 'resolution' => $smallest,
326                 'ext'        => $image->getExt(),
327         ];
328
329         return $imagecrop;
330 }