From 7d91cc73de054c645df61f64ef72ff8aca9cfb4d Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 May 2023 20:44:58 +0000 Subject: [PATCH 1/7] Unifiy the creation of photo links --- mod/photos.php | 4 +--- src/Model/Post/Media.php | 10 +++------- src/Module/Media/Photo/Upload.php | 2 +- src/Module/Profile/Photos.php | 4 +--- src/Util/Images.php | 28 ++++++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/mod/photos.php b/mod/photos.php index 3d6a1d4051..18118bcd96 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -375,9 +375,7 @@ function photos_post(App $a) $arr['visible'] = 0; $arr['origin'] = 1; - $arr['body'] = '[url=' . DI::baseUrl() . '/photos/' . $user['nickname'] . '/image/' . $photo['resource-id'] . ']' - . '[img]' . DI::baseUrl() . '/photo/' . $photo['resource-id'] . '-' . $photo['scale'] . '.'. $ext . '[/img]' - . '[/url]'; + $arr['body'] = Images::getImageUrl($photo['resource-id'], $user['nickname'], $photo['scale'], $ext, ''); $item_id = Item::insert($arr); } diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index cc5b16d1e7..5e867d26cf 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -489,7 +489,7 @@ class Media if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) { foreach ($pictures as $picture) { if (self::isLinkToImagePage($picture[1], $picture[2])) { - $body = str_replace($picture[0], '[url=' . str_replace(['-1.', '-2.'], '-0.', $picture[2]) . '][img=' . $picture[2] . ']' . $picture[3] . '[/img][/url]', $body); + $body = str_replace($picture[0], Images::getPictureUrl(str_replace(['-1.', '-2.'], '-0.', $picture[2]), $picture[2], $picture[3]), $body); } } } @@ -497,7 +497,7 @@ class Media if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) { foreach ($pictures as $picture) { if (self::isLinkToImagePage($picture[1], $picture[2])) { - $body = str_replace($picture[0], '[url=' . str_replace(['-1.', '-2.'], '-0.', $picture[2]) . '][img]' . $picture[2] . '[/img][/url]', $body); + $body = str_replace($picture[0], Images::getPictureUrl(str_replace(['-1.', '-2.'], '-0.', $picture[2]), $picture[2], ''), $body); } } } @@ -1005,11 +1005,7 @@ class Media if ($media['type'] == self::IMAGE) { if (!empty($media['preview'])) { - if (!empty($media['description'])) { - $body .= "\n[url=" . $media['url'] . "][img=" . $media['preview'] . ']' . $media['description'] . '[/img][/url]'; - } else { - $body .= "\n[url=" . $media['url'] . "][img]" . $media['preview'] . '[/img][/url]'; - } + $body .= "\n" . Images::getPictureUrl($media['url'], $media['preview'], $media['description']); } else { if (!empty($media['description'])) { $body .= "\n[img=" . $media['url'] . ']' . $media['description'] . '[/img]'; diff --git a/src/Module/Media/Photo/Upload.php b/src/Module/Media/Photo/Upload.php index 09d31971a4..8ea079c6cf 100644 --- a/src/Module/Media/Photo/Upload.php +++ b/src/Module/Media/Photo/Upload.php @@ -180,7 +180,7 @@ class Upload extends \Friendica\BaseModule } $this->logger->info('upload done'); - $this->return(200, "\n\n" . '[url=' . $this->baseUrl . '/photos/' . $owner['nickname'] . '/image/' . $resource_id . '][img=' . $this->baseUrl . "/photo/$resource_id-$preview." . $image->getExt() . "][/img][/url]\n\n"); + $this->return(200, "\n\n" . Images::getImageUrl($resource_id, $owner['nickname'], $preview, $image->getExt(), '') . "\n\n"); } /** diff --git a/src/Module/Profile/Photos.php b/src/Module/Profile/Photos.php index 03937c655e..720efa060b 100644 --- a/src/Module/Profile/Photos.php +++ b/src/Module/Profile/Photos.php @@ -273,9 +273,7 @@ class Photos extends \Friendica\Module\BaseProfile $arr['visible'] = $visible; $arr['origin'] = 1; - $arr['body'] = '[url=' . $this->baseUrl . '/photos/' . $this->owner['nickname'] . '/image/' . $resource_id . ']' - . '[img]' . $this->baseUrl . "/photo/{$resource_id}-{$preview}.".$image->getExt() . '[/img]' - . '[/url]'; + $arr['body'] = Images::getImageUrl($resource_id, $this->owner['nickname'], $preview, $image->getExt(), ''); $item_id = Item::insert($arr); // Update the photo albums cache diff --git a/src/Util/Images.php b/src/Util/Images.php index 90d120081b..08553a1a9c 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -316,4 +316,32 @@ class Images return ['width' => $dest_width, 'height' => $dest_height]; } + + /** + * Get a link to a an image link with a preview + * + * @param string $resource_id + * @param string $nickname + * @param integer $preview + * @param string $ext + * @param string $description + * @return string + */ + public static function getImageUrl(string $resource_id, string $nickname, int $preview, string $ext, string $description): string + { + return '[url=' . DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id . '][img=' . DI::baseUrl() . '/photo/' . $resource_id . '-' . $preview. '.' . $ext . ']' . $description . '[/img][/url]'; + } + + /** + * Get a link to a picture with a preview + * + * @param string $photo + * @param string $preview + * @param string $description + * @return string + */ + public static function getPictureUrl(string $photo, string $preview, string $description): string + { + return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; + } } From c5ca5bfdf84f6fb5e4ba4d8509df25c58aeb516a Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 10 May 2023 07:54:15 +0000 Subject: [PATCH 2/7] Function renamed --- mod/photos.php | 2 +- src/Model/Post/Media.php | 14 +++----------- src/Module/Media/Photo/Upload.php | 3 +-- src/Module/Profile/Photos.php | 2 +- src/Util/Images.php | 30 ++++++++++++++++++------------ 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/mod/photos.php b/mod/photos.php index 18118bcd96..0a8dc472d7 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -375,7 +375,7 @@ function photos_post(App $a) $arr['visible'] = 0; $arr['origin'] = 1; - $arr['body'] = Images::getImageUrl($photo['resource-id'], $user['nickname'], $photo['scale'], $ext, ''); + $arr['body'] = Images::getBBCodeByResource($photo['resource-id'], $user['nickname'], $photo['scale'], $ext); $item_id = Item::insert($arr); } diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index 5e867d26cf..e040f2b113 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -489,7 +489,7 @@ class Media if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) { foreach ($pictures as $picture) { if (self::isLinkToImagePage($picture[1], $picture[2])) { - $body = str_replace($picture[0], Images::getPictureUrl(str_replace(['-1.', '-2.'], '-0.', $picture[2]), $picture[2], $picture[3]), $body); + $body = str_replace($picture[0], Images::getBBCodeByUrl(str_replace(['-1.', '-2.'], '-0.', $picture[2]), $picture[2], $picture[3]), $body); } } } @@ -497,7 +497,7 @@ class Media if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) { foreach ($pictures as $picture) { if (self::isLinkToImagePage($picture[1], $picture[2])) { - $body = str_replace($picture[0], Images::getPictureUrl(str_replace(['-1.', '-2.'], '-0.', $picture[2]), $picture[2], ''), $body); + $body = str_replace($picture[0], Images::getBBCodeByUrl(str_replace(['-1.', '-2.'], '-0.', $picture[2]), $picture[2]), $body); } } } @@ -1004,15 +1004,7 @@ class Media } if ($media['type'] == self::IMAGE) { - if (!empty($media['preview'])) { - $body .= "\n" . Images::getPictureUrl($media['url'], $media['preview'], $media['description']); - } else { - if (!empty($media['description'])) { - $body .= "\n[img=" . $media['url'] . ']' . $media['description'] . '[/img]'; - } else { - $body .= "\n[img]" . $media['url'] . '[/img]'; - } - } + $body .= "\n" . Images::getBBCodeByUrl($media['url'], $media['preview'], $media['description']); } elseif ($media['type'] == self::AUDIO) { $body .= "\n[audio]" . $media['url'] . "[/audio]\n"; } elseif ($media['type'] == self::VIDEO) { diff --git a/src/Module/Media/Photo/Upload.php b/src/Module/Media/Photo/Upload.php index 8ea079c6cf..e53ca35b86 100644 --- a/src/Module/Media/Photo/Upload.php +++ b/src/Module/Media/Photo/Upload.php @@ -34,7 +34,6 @@ use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Object\Image; use Friendica\Util\Images; use Friendica\Util\Profiler; -use Friendica\Util\Strings; use Psr\Log\LoggerInterface; /** @@ -180,7 +179,7 @@ class Upload extends \Friendica\BaseModule } $this->logger->info('upload done'); - $this->return(200, "\n\n" . Images::getImageUrl($resource_id, $owner['nickname'], $preview, $image->getExt(), '') . "\n\n"); + $this->return(200, "\n\n" . Images::getBBCodeByResource($resource_id, $owner['nickname'], $preview, $image->getExt()) . "\n\n"); } /** diff --git a/src/Module/Profile/Photos.php b/src/Module/Profile/Photos.php index 720efa060b..9d8b27592c 100644 --- a/src/Module/Profile/Photos.php +++ b/src/Module/Profile/Photos.php @@ -273,7 +273,7 @@ class Photos extends \Friendica\Module\BaseProfile $arr['visible'] = $visible; $arr['origin'] = 1; - $arr['body'] = Images::getImageUrl($resource_id, $this->owner['nickname'], $preview, $image->getExt(), ''); + $arr['body'] = Images::getBBCodeByResource($resource_id, $this->owner['nickname'], $preview, $image->getExt()); $item_id = Item::insert($arr); // Update the photo albums cache diff --git a/src/Util/Images.php b/src/Util/Images.php index 08553a1a9c..90c2aae9db 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -318,30 +318,36 @@ class Images } /** - * Get a link to a an image link with a preview + * Get a BBCode tag for an local photo page URL with a preview thumbnail and an image description * * @param string $resource_id - * @param string $nickname - * @param integer $preview - * @param string $ext + * @param string $nickname The local user owner of the resource + * @param int $preview Preview image size identifier, either 0, 1 or 2 in decreasing order of size + * @param string $ext Image file extension * @param string $description * @return string */ - public static function getImageUrl(string $resource_id, string $nickname, int $preview, string $ext, string $description): string + public static function getBBCodeByResource(string $resource_id, string $nickname, int $preview, string $ext, string $description = ''): string { - return '[url=' . DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id . '][img=' . DI::baseUrl() . '/photo/' . $resource_id . '-' . $preview. '.' . $ext . ']' . $description . '[/img][/url]'; + return self::getBBCodeByUrl( + DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id, + DI::baseUrl() . '/photo/' . $resource_id . '-' . $preview. '.' . $ext, + $description + ); } /** - * Get a link to a picture with a preview + * Get a BBCode tag for an image URL with a preview thumbnail and an image description * - * @param string $photo - * @param string $preview + * @param string $photo Full image URL + * @param string $preview Preview image URL * @param string $description * @return string - */ - public static function getPictureUrl(string $photo, string $preview, string $description): string + */ public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = ''): string { - return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; + if (!empty($preview)) { + return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; + } + return '[img=' . $photo . ']' . $description . '[/img]'; } } From 3554b615084fd8704ee9b95aa538c47099d5bce5 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 10 May 2023 08:48:21 +0000 Subject: [PATCH 3/7] Fix tests --- src/Util/Images.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/Images.php b/src/Util/Images.php index 90c2aae9db..f468e69c2d 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -343,7 +343,7 @@ class Images * @param string $preview Preview image URL * @param string $description * @return string - */ public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = ''): string + */ public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = null): string { if (!empty($preview)) { return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; From 45dda2c0647a76093fc50b7f245b78d59ef90434 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Wed, 10 May 2023 17:06:16 +0200 Subject: [PATCH 4/7] Update src/Util/Images.php Co-authored-by: Hypolite Petovan --- src/Util/Images.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Util/Images.php b/src/Util/Images.php index f468e69c2d..05b3573dd2 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -343,7 +343,8 @@ class Images * @param string $preview Preview image URL * @param string $description * @return string - */ public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = null): string + */ + public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = ''): string { if (!empty($preview)) { return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; From 992b6eed1d8bc0201ece8b9043ee70d76bc68a3e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 10 May 2023 16:57:08 +0000 Subject: [PATCH 5/7] Fixed test - again --- src/Util/Images.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/Images.php b/src/Util/Images.php index 05b3573dd2..269dbda199 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -344,7 +344,7 @@ class Images * @param string $description * @return string */ - public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = ''): string + public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = null): string { if (!empty($preview)) { return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; From 2b3c1972db987ee2396e528476739f4cec4e8972 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 10 May 2023 17:59:25 +0000 Subject: [PATCH 6/7] Differentiate between no description or an empty description --- src/Module/Media/Photo/Upload.php | 2 +- src/Util/Images.php | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Module/Media/Photo/Upload.php b/src/Module/Media/Photo/Upload.php index e53ca35b86..25f9af9bc0 100644 --- a/src/Module/Media/Photo/Upload.php +++ b/src/Module/Media/Photo/Upload.php @@ -179,7 +179,7 @@ class Upload extends \Friendica\BaseModule } $this->logger->info('upload done'); - $this->return(200, "\n\n" . Images::getBBCodeByResource($resource_id, $owner['nickname'], $preview, $image->getExt()) . "\n\n"); + $this->return(200, "\n\n" . Images::getBBCodeByResource($resource_id, $owner['nickname'], $preview, $image->getExt(), '') . "\n\n"); } /** diff --git a/src/Util/Images.php b/src/Util/Images.php index 269dbda199..7f6d8720ff 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -327,7 +327,7 @@ class Images * @param string $description * @return string */ - public static function getBBCodeByResource(string $resource_id, string $nickname, int $preview, string $ext, string $description = ''): string + public static function getBBCodeByResource(string $resource_id, string $nickname, int $preview, string $ext, string $description = null): string { return self::getBBCodeByUrl( DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id, @@ -347,8 +347,17 @@ class Images public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = null): string { if (!empty($preview)) { - return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; + if (!is_null($description)) { + return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; + } else { + return '[url=' . $photo . '][img]' . $preview . '[/img][/url]'; + } + } + + if (!is_null($description)) { + return '[img=' . $photo . ']' . $description . '[/img]'; + } else { + return '[img]' . $photo . '[/img]'; } - return '[img=' . $photo . ']' . $description . '[/img]'; } } From 10104430313887e825db2f2e61b5bf46ffe95a60 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 May 2023 20:30:45 +0000 Subject: [PATCH 7/7] Simplify image url --- src/Content/Text/HTML.php | 1 - src/Model/Post/Media.php | 2 +- src/Module/Media/Photo/Upload.php | 2 +- src/Util/Images.php | 16 ++++------------ 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/Content/Text/HTML.php b/src/Content/Text/HTML.php index 3f9ef7c3b4..988274f8dc 100644 --- a/src/Content/Text/HTML.php +++ b/src/Content/Text/HTML.php @@ -24,7 +24,6 @@ namespace Friendica\Content\Text; use DOMDocument; use DOMXPath; use Friendica\Protocol\HTTP\MediaType; -use Friendica\Content\Widget\ContactBlock; use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\Core\Search; diff --git a/src/Model/Post/Media.php b/src/Model/Post/Media.php index e040f2b113..a0d55b6e00 100644 --- a/src/Model/Post/Media.php +++ b/src/Model/Post/Media.php @@ -1004,7 +1004,7 @@ class Media } if ($media['type'] == self::IMAGE) { - $body .= "\n" . Images::getBBCodeByUrl($media['url'], $media['preview'], $media['description']); + $body .= "\n" . Images::getBBCodeByUrl($media['url'], $media['preview'], $media['description'] ?? ''); } elseif ($media['type'] == self::AUDIO) { $body .= "\n[audio]" . $media['url'] . "[/audio]\n"; } elseif ($media['type'] == self::VIDEO) { diff --git a/src/Module/Media/Photo/Upload.php b/src/Module/Media/Photo/Upload.php index 25f9af9bc0..e53ca35b86 100644 --- a/src/Module/Media/Photo/Upload.php +++ b/src/Module/Media/Photo/Upload.php @@ -179,7 +179,7 @@ class Upload extends \Friendica\BaseModule } $this->logger->info('upload done'); - $this->return(200, "\n\n" . Images::getBBCodeByResource($resource_id, $owner['nickname'], $preview, $image->getExt(), '') . "\n\n"); + $this->return(200, "\n\n" . Images::getBBCodeByResource($resource_id, $owner['nickname'], $preview, $image->getExt()) . "\n\n"); } /** diff --git a/src/Util/Images.php b/src/Util/Images.php index 7f6d8720ff..aed72f065b 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -327,7 +327,7 @@ class Images * @param string $description * @return string */ - public static function getBBCodeByResource(string $resource_id, string $nickname, int $preview, string $ext, string $description = null): string + public static function getBBCodeByResource(string $resource_id, string $nickname, int $preview, string $ext, string $description = ''): string { return self::getBBCodeByUrl( DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id, @@ -344,20 +344,12 @@ class Images * @param string $description * @return string */ - public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = null): string + public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = ''): string { if (!empty($preview)) { - if (!is_null($description)) { - return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; - } else { - return '[url=' . $photo . '][img]' . $preview . '[/img][/url]'; - } + return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; } - if (!is_null($description)) { - return '[img=' . $photo . ']' . $description . '[/img]'; - } else { - return '[img]' . $photo . '[/img]'; - } + return '[img=' . $photo . ']' . $description . '[/img]'; } }