diff --git a/mod/photos.php b/mod/photos.php index 3d6a1d405..0a8dc472d 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::getBBCodeByResource($photo['resource-id'], $user['nickname'], $photo['scale'], $ext); $item_id = Item::insert($arr); } diff --git a/src/Content/Text/HTML.php b/src/Content/Text/HTML.php index 3f9ef7c3b..988274f8d 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 cc5b16d1e..a0d55b6e0 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::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], '[url=' . str_replace(['-1.', '-2.'], '-0.', $picture[2]) . '][img]' . $picture[2] . '[/img][/url]', $body); + $body = str_replace($picture[0], Images::getBBCodeByUrl(str_replace(['-1.', '-2.'], '-0.', $picture[2]), $picture[2]), $body); } } } @@ -1004,19 +1004,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]'; - } - } 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 09d31971a..e53ca35b8 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" . '[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::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 03937c655..9d8b27592 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::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 90d120081..aed72f065 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -316,4 +316,40 @@ class Images return ['width' => $dest_width, 'height' => $dest_height]; } + + /** + * 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 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 getBBCodeByResource(string $resource_id, string $nickname, int $preview, string $ext, string $description = ''): string + { + return self::getBBCodeByUrl( + DI::baseUrl() . '/photos/' . $nickname . '/image/' . $resource_id, + DI::baseUrl() . '/photo/' . $resource_id . '-' . $preview. '.' . $ext, + $description + ); + } + + /** + * Get a BBCode tag for an image URL with a preview thumbnail and an image description + * + * @param string $photo Full image URL + * @param string $preview Preview image URL + * @param string $description + * @return string + */ + public static function getBBCodeByUrl(string $photo, string $preview = null, string $description = ''): string + { + if (!empty($preview)) { + return '[url=' . $photo . '][img=' . $preview . ']' . $description . '[/img][/url]'; + } + + return '[img=' . $photo . ']' . $description . '[/img]'; + } }