From 5bff6f38d7b36544fc3d09d80909af0a6d9b358d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 4 Dec 2022 23:31:23 +0000 Subject: [PATCH] Issue 12327: Convert avatars to static --- src/Model/Contact.php | 26 ++++++++++++++++++++++---- src/Module/NoScrape.php | 1 - src/Module/Photo.php | 6 ++++++ src/Object/Api/Mastodon/Account.php | 4 ++-- src/Object/Image.php | 4 ++-- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 34ce8e684e..3bed67214c 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -2057,9 +2057,10 @@ class Contact * @param integer $cid contact id * @param string $size One of the Proxy::SIZE_* constants * @param string $updated Contact update date + * @param bool $static If "true" a parameter is added to convert the header to a static one * @return string avatar link */ - public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string + public static function getAvatarUrlForId(int $cid, string $size = '', string $updated = '', string $guid = '', bool $static = false): string { // We have to fetch the "updated" variable when it wasn't provided // The parameter can be provided to improve performance @@ -2089,7 +2090,15 @@ class Contact $url .= Proxy::PIXEL_LARGE . '/'; break; } - return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : ''); + $query_params = []; + if ($updated) { + $query_params['ts'] = strtotime($updated); + } + if ($static) { + $query_params['static'] = true; + } + + return $url . ($guid ?: $cid) . (!empty($query_params) ? '?' . http_build_query($query_params) : ''); } /** @@ -2114,9 +2123,10 @@ class Contact * @param integer $cid contact id * @param string $size One of the Proxy::SIZE_* constants * @param string $updated Contact update date + * @param bool $static If "true" a parameter is added to convert the header to a static one * @return string header link */ - public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = ''): string + public static function getHeaderUrlForId(int $cid, string $size = '', string $updated = '', string $guid = '', bool $static = false): string { // We have to fetch the "updated" variable when it wasn't provided // The parameter can be provided to improve performance @@ -2147,7 +2157,15 @@ class Contact break; } - return $url . ($guid ?: $cid) . ($updated ? '?ts=' . strtotime($updated) : ''); + $query_params = []; + if ($updated) { + $query_params['ts'] = strtotime($updated); + } + if ($static) { + $query_params['static'] = true; + } + + return $url . ($guid ?: $cid) . (!empty($query_params) ? '?' . http_build_query($query_params) : ''); } /** diff --git a/src/Module/NoScrape.php b/src/Module/NoScrape.php index 718c95c107..8e5850ac0b 100644 --- a/src/Module/NoScrape.php +++ b/src/Module/NoScrape.php @@ -23,7 +23,6 @@ namespace Friendica\Module; use Friendica\BaseModule; use Friendica\Core\System; -use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\User; diff --git a/src/Module/Photo.php b/src/Module/Photo.php index a4ef6ab414..78e403e6c7 100644 --- a/src/Module/Photo.php +++ b/src/Module/Photo.php @@ -182,6 +182,12 @@ class Photo extends BaseModule throw new HTTPException\InternalServerErrorException($error); } + if (!empty($request['static'])) { + $img = new Image($imgdata, $photo['type']); + $img->toStatic(); + $imgdata = $img->asString(); + } + // if customsize is set and image is not a gif, resize it if ($photo['type'] !== 'image/gif' && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) { $img = new Image($imgdata, $photo['type']); diff --git a/src/Object/Api/Mastodon/Account.php b/src/Object/Api/Mastodon/Account.php index 3369a18c00..afc739e1c6 100644 --- a/src/Object/Api/Mastodon/Account.php +++ b/src/Object/Api/Mastodon/Account.php @@ -109,9 +109,9 @@ class Account extends BaseDataTransferObject $this->note = BBCode::convertForUriId($account['uri-id'], $account['about'], BBCode::EXTERNAL); $this->url = $account['url']; $this->avatar = Contact::getAvatarUrlForId($account['id'] ?? 0 ?: $account['pid'], Proxy::SIZE_SMALL, $account['updated'], $account['guid'] ?? ''); - $this->avatar_static = $this->avatar; + $this->avatar_static = Contact::getAvatarUrlForId($account['id'] ?? 0 ?: $account['pid'], Proxy::SIZE_SMALL, $account['updated'], $account['guid'] ?? '', true); $this->header = Contact::getHeaderUrlForId($account['id'] ?? 0 ?: $account['pid'], '', $account['updated'], $account['guid'] ?? ''); - $this->header_static = $this->header; + $this->header_static = Contact::getHeaderUrlForId($account['id'] ?? 0 ?: $account['pid'], '', $account['updated'], $account['guid'] ?? '', true); $this->followers_count = $account['ap-followers_count'] ?? $account['diaspora-interacted_count'] ?? 0; $this->following_count = $account['ap-following_count'] ?? $account['diaspora-interacting_count'] ?? 0; $this->statuses_count = $account['ap-statuses_count'] ?? $account['diaspora-post_count'] ?? 0; diff --git a/src/Object/Image.php b/src/Object/Image.php index 87401304db..f49aa7fdd2 100644 --- a/src/Object/Image.php +++ b/src/Object/Image.php @@ -57,7 +57,7 @@ class Image */ public function __construct(string $data, string $type = null) { - $this->imagick = class_exists('Imagick'); + $this->imagick = class_exists('Imagick') && !class_exists('GDImage'); $this->types = Images::supportedTypes(); if (!array_key_exists($type, $this->types)) { $type = 'image/jpeg'; @@ -751,7 +751,7 @@ class Image $row = []; for ($x = 0; $x < $width; ++$x) { $index = imagecolorat($this->image, $x, $y); - $colors = imagecolorsforindex($this->image, $index); + $colors = @imagecolorsforindex($this->image, $index); $row[] = [$colors['red'], $colors['green'], $colors['blue']]; }