3 * @copyright Copyright (C) 2020, Friendica
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\App\Arguments;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\Session;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Photo;
30 use Friendica\Module\BaseSettings;
31 use Friendica\Network\HTTPException;
32 use Friendica\Object\Image;
33 use Friendica\Util\Images;
34 use Friendica\Util\Strings;
36 class Index extends BaseSettings
38 public static function post(array $parameters = [])
40 if (!Session::isAuthenticated()) {
44 self::checkFormSecurityTokenRedirectOnError('/settings/profile/photo', 'settings_profile_photo');
46 if (empty($_FILES['userfile'])) {
47 notice(DI::l10n()->t('Missing uploaded image.'));
51 $src = $_FILES['userfile']['tmp_name'];
52 $filename = basename($_FILES['userfile']['name']);
53 $filesize = intval($_FILES['userfile']['size']);
54 $filetype = $_FILES['userfile']['type'];
55 if ($filetype == '') {
56 $filetype = Images::guessType($filename);
59 $maximagesize = DI::config()->get('system', 'maximagesize', 0);
61 if ($maximagesize && $filesize > $maximagesize) {
62 notice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
67 $imagedata = @file_get_contents($src);
68 $Image = new Image($imagedata, $filetype);
70 if (!$Image->isValid()) {
71 notice(DI::l10n()->t('Unable to process image.'));
79 $max_length = DI::config()->get('system', 'max_image_length', 0);
80 if ($max_length > 0) {
81 $Image->scaleDown($max_length);
84 $width = $Image->getWidth();
85 $height = $Image->getHeight();
87 if ($width < 175 || $height < 175) {
89 $width = $Image->getWidth();
90 $height = $Image->getHeight();
93 $resource_id = Photo::newResource();
97 if (Photo::store($Image, local_user(), 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 0)) {
98 info(DI::l10n()->t('Image uploaded successfully.'));
100 notice(DI::l10n()->t('Image upload failed.'));
103 if ($width > 640 || $height > 640) {
104 $Image->scaleDown(640);
105 if (!Photo::store($Image, local_user(), 0, $resource_id, $filename, DI::l10n()->t('Profile Photos'), 1)) {
106 notice(DI::l10n()->t('Image size reduction [%s] failed.', '640'));
110 DI::baseUrl()->redirect('settings/profile/photo/crop/' . $resource_id);
113 public static function content(array $parameters = [])
115 if (!Session::isAuthenticated()) {
116 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
123 $newuser = $args->get($args->getArgc() - 1) === 'new';
125 $contact = Contact::selectFirst(['avatar'], ['uid' => local_user(), 'self' => true]);
127 $tpl = Renderer::getMarkupTemplate('settings/profile/photo/index.tpl');
128 $o = Renderer::replaceMacros($tpl, [
129 '$title' => DI::l10n()->t('Profile Picture Settings'),
130 '$current_picture' => DI::l10n()->t('Current Profile Picture'),
131 '$upload_picture' => DI::l10n()->t('Upload Profile Picture'),
132 '$lbl_upfile' => DI::l10n()->t('Upload Picture:'),
133 '$submit' => DI::l10n()->t('Upload'),
134 '$avatar' => $contact['avatar'],
135 '$form_security_token' => self::getFormSecurityToken('settings_profile_photo'),
136 '$select' => sprintf('%s %s',
139 '<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('skip this step') . '</a>'
140 : '<a href="' . DI::baseUrl() . '/photos/' . DI::app()->user['nickname'] . '">'
141 . DI::l10n()->t('select a photo from your photo albums') . '</a>'