3 namespace Friendica\Module\Settings\Profile\Photo;
5 use Friendica\App\Arguments;
6 use Friendica\Core\Renderer;
7 use Friendica\Core\Session;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Photo;
11 use Friendica\Module\BaseSettingsModule;
12 use Friendica\Network\HTTPException;
13 use Friendica\Object\Image;
14 use Friendica\Util\Images;
15 use Friendica\Util\Strings;
17 class Index extends BaseSettingsModule
19 public static function post(array $parameters = [])
21 if (!Session::isAuthenticated()) {
25 self::checkFormSecurityTokenRedirectOnError('/settings/profile/photo', 'settings_profile_photo');
27 if (empty($_FILES['userfile'])) {
28 notice(DI::l10n()->t('Missing uploaded image.'));
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);
40 $maximagesize = DI::config()->get('system', 'maximagesize', 0);
42 if ($maximagesize && $filesize > $maximagesize) {
43 notice(DI::l10n()->t('Image exceeds size limit of %s', Strings::formatBytes($maximagesize)));
48 $imagedata = @file_get_contents($src);
49 $Image = new Image($imagedata, $filetype);
51 if (!$Image->isValid()) {
52 notice(DI::l10n()->t('Unable to process image.'));
60 $max_length = DI::config()->get('system', 'max_image_length', 0);
61 if ($max_length > 0) {
62 $Image->scaleDown($max_length);
65 $width = $Image->getWidth();
66 $height = $Image->getHeight();
68 if ($width < 175 || $height < 175) {
70 $width = $Image->getWidth();
71 $height = $Image->getHeight();
74 $resource_id = Photo::newResource();
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.'));
81 notice(DI::l10n()->t('Image upload failed.'));
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'));
91 DI::baseUrl()->redirect('settings/profile/photo/crop/' . $resource_id);
94 public static function content(array $parameters = [])
96 if (!Session::isAuthenticated()) {
97 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
104 $newuser = $args->get($args->getArgc() - 1) === 'new';
106 $contact = Contact::selectFirst(['avatar'], ['uid' => local_user(), 'self' => true]);
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',
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>'