3 * @copyright Copyright (C) 2010-2023, 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;
26 use Friendica\Model\Contact;
27 use Friendica\Model\Photo;
28 use Friendica\Module\BaseSettings;
29 use Friendica\Network\HTTPException;
30 use Friendica\Object\Image;
31 use Friendica\Util\Images;
32 use Friendica\Util\Strings;
34 class Index extends BaseSettings
36 protected function post(array $request = [])
38 if (!DI::userSession()->isAuthenticated()) {
42 self::checkFormSecurityTokenRedirectOnError('/settings/profile/photo', 'settings_profile_photo');
44 if (empty($_FILES['userfile'])) {
45 DI::sysmsg()->addNotice(DI::l10n()->t('Missing uploaded image.'));
49 $src = $_FILES['userfile']['tmp_name'];
50 $filename = basename($_FILES['userfile']['name']);
51 $filesize = intval($_FILES['userfile']['size']);
52 $filetype = $_FILES['userfile']['type'];
54 $filetype = Images::getMimeTypeBySource($src, $filename, $filetype);
56 $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize', 0));
58 if ($maximagesize && $filesize > $maximagesize) {
59 DI::sysmsg()->addNotice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
64 $imagedata = @file_get_contents($src);
65 $Image = new Image($imagedata, $filetype);
67 if (!$Image->isValid()) {
68 DI::sysmsg()->addNotice(DI::l10n()->t('Unable to process image.'));
76 $max_length = DI::config()->get('system', 'max_image_length', 0);
77 if ($max_length > 0) {
78 $Image->scaleDown($max_length);
81 $width = $Image->getWidth();
82 $height = $Image->getHeight();
84 if ($width < 175 || $height < 175) {
86 $width = $Image->getWidth();
87 $height = $Image->getHeight();
90 $resource_id = Photo::newResource();
94 if (!Photo::store($Image, DI::userSession()->getLocalUserId(), 0, $resource_id, $filename, DI::l10n()->t(Photo::PROFILE_PHOTOS), 0, Photo::USER_AVATAR)) {
95 DI::sysmsg()->addNotice(DI::l10n()->t('Image upload failed.'));
98 if ($width > 640 || $height > 640) {
99 $Image->scaleDown(640);
100 if (!Photo::store($Image, DI::userSession()->getLocalUserId(), 0, $resource_id, $filename, DI::l10n()->t(Photo::PROFILE_PHOTOS), 1, Photo::USER_AVATAR)) {
101 DI::sysmsg()->addNotice(DI::l10n()->t('Image size reduction [%s] failed.', '640'));
105 DI::baseUrl()->redirect('settings/profile/photo/crop/' . $resource_id);
108 protected function content(array $request = []): string
110 if (!DI::userSession()->isAuthenticated()) {
111 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
118 $newuser = $args->get($args->getArgc() - 1) === 'new';
120 $contact = Contact::selectFirst(['avatar'], ['uid' => DI::userSession()->getLocalUserId(), 'self' => true]);
122 $tpl = Renderer::getMarkupTemplate('settings/profile/photo/index.tpl');
123 $o = Renderer::replaceMacros($tpl, [
124 '$title' => DI::l10n()->t('Profile Picture Settings'),
125 '$current_picture' => DI::l10n()->t('Current Profile Picture'),
126 '$upload_picture' => DI::l10n()->t('Upload Profile Picture'),
127 '$lbl_upfile' => DI::l10n()->t('Upload Picture:'),
128 '$submit' => DI::l10n()->t('Upload'),
129 '$avatar' => $contact['avatar'],
130 '$form_security_token' => self::getFormSecurityToken('settings_profile_photo'),
131 '$select' => sprintf('%s %s',
134 '<a href="' . DI::baseUrl() . '">' . DI::l10n()->t('skip this step') . '</a>'
135 : '<a href="' . DI::baseUrl() . '/profile/' . DI::app()->getLoggedInUserNickname() . '/photos">'
136 . DI::l10n()->t('select a photo from your photo albums') . '</a>'