]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/Profile/Photo/Index.php
Merge pull request #8209 from nupplaphil/task/mod_worker
[friendica.git] / src / Module / Settings / Profile / Photo / Index.php
1 <?php
2
3 namespace Friendica\Module\Settings\Profile\Photo;
4
5 use Friendica\App\Arguments;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
8 use Friendica\DI;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Photo;
11 use Friendica\Module\BaseSettings;
12 use Friendica\Network\HTTPException;
13 use Friendica\Object\Image;
14 use Friendica\Util\Images;
15 use Friendica\Util\Strings;
16
17 class Index extends BaseSettings
18 {
19         public static function post(array $parameters = [])
20         {
21                 if (!Session::isAuthenticated()) {
22                         return;
23                 }
24
25                 self::checkFormSecurityTokenRedirectOnError('/settings/profile/photo', 'settings_profile_photo');
26
27                 if (empty($_FILES['userfile'])) {
28                         notice(DI::l10n()->t('Missing uploaded image.'));
29                         return;
30                 }
31
32                 $src = $_FILES['userfile']['tmp_name'];
33                 $filename = basename($_FILES['userfile']['name']);
34                 $filesize = intval($_FILES['userfile']['size']);
35                 $filetype = $_FILES['userfile']['type'];
36                 if ($filetype == '') {
37                         $filetype = Images::guessType($filename);
38                 }
39
40                 $maximagesize = DI::config()->get('system', 'maximagesize', 0);
41
42                 if ($maximagesize && $filesize > $maximagesize) {
43                         notice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
44                         @unlink($src);
45                         return;
46                 }
47
48                 $imagedata = @file_get_contents($src);
49                 $Image = new Image($imagedata, $filetype);
50
51                 if (!$Image->isValid()) {
52                         notice(DI::l10n()->t('Unable to process image.'));
53                         @unlink($src);
54                         return;
55                 }
56
57                 $Image->orient($src);
58                 @unlink($src);
59
60                 $max_length = DI::config()->get('system', 'max_image_length', 0);
61                 if ($max_length > 0) {
62                         $Image->scaleDown($max_length);
63                 }
64
65                 $width = $Image->getWidth();
66                 $height = $Image->getHeight();
67
68                 if ($width < 175 || $height < 175) {
69                         $Image->scaleUp(300);
70                         $width = $Image->getWidth();
71                         $height = $Image->getHeight();
72                 }
73
74                 $resource_id = Photo::newResource();
75
76                 $filename = '';
77
78                 if (Photo::store($Image, local_user(), 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 0)) {
79                         info(DI::l10n()->t('Image uploaded successfully.'));
80                 } else {
81                         notice(DI::l10n()->t('Image upload failed.'));
82                 }
83
84                 if ($width > 640 || $height > 640) {
85                         $Image->scaleDown(640);
86                         if (!Photo::store($Image, local_user(), 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 1)) {
87                                 notice(DI::l10n()->t('Image size reduction [%s] failed.', '640'));
88                         }
89                 }
90
91                 DI::baseUrl()->redirect('settings/profile/photo/crop/' . $resource_id);
92         }
93
94         public static function content(array $parameters = [])
95         {
96                 if (!Session::isAuthenticated()) {
97                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
98                 }
99
100                 parent::content();
101
102                 $args = DI::args();
103
104                 $newuser = $args->get($args->getArgc() - 1) === 'new';
105
106                 $contact = Contact::selectFirst(['avatar'], ['uid' => local_user(), 'self' => true]);
107
108                 $tpl = Renderer::getMarkupTemplate('settings/profile/photo/index.tpl');
109                 $o = Renderer::replaceMacros($tpl, [
110                         '$title'           => DI::l10n()->t('Profile Picture Settings'),
111                         '$current_picture' => DI::l10n()->t('Current Profile Picture'),
112                         '$upload_picture'  => DI::l10n()->t('Upload Profile Picture'),
113                         '$lbl_upfile'      => DI::l10n()->t('Upload Picture:'),
114                         '$submit'          => DI::l10n()->t('Upload'),
115                         '$avatar'          => $contact['avatar'],
116                         '$form_security_token' => self::getFormSecurityToken('settings_profile_photo'),
117                         '$select'          => sprintf('%s %s',
118                                 DI::l10n()->t('or'),
119                                 ($newuser) ?
120                                         '<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('skip this step') . '</a>'
121                                         : '<a href="' . DI::baseUrl() . '/photos/' . DI::app()->user['nickname'] . '">'
122                                                 . DI::l10n()->t('select a photo from your photo albums') . '</a>'
123                         ),
124                 ]);
125
126                 return $o;
127         }
128 }