X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FImages.php;h=aed72f065b7c8c0810efa22c0aaa938ca6c7c46c;hb=911ed3d6ba98149ed6d400d554e7a0e10bd1c273;hp=595640698780e9deafa23507d742db464437a0bd;hpb=84bfc37bf1fcf343015d9569266d66646b9550c1;p=friendica.git diff --git a/src/Util/Images.php b/src/Util/Images.php index 5956406987..aed72f065b 100644 --- a/src/Util/Images.php +++ b/src/Util/Images.php @@ -1,6 +1,6 @@ $filename, 'mime' => $mime]); - return $mime; + if (substr($default, 0, 6) == 'image/') { + Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $default]); + return $default; } $image = @getimagesize($sourcefile); if (!empty($image['mime'])) { - Logger::info('Mime type detected via file', ['filename' => $filename, 'default' => $mime, 'image' => $image]); + Logger::info('Mime type detected via file', ['filename' => $filename, 'default' => $default, 'image' => $image]); return $image['mime']; } @@ -179,7 +180,7 @@ class Images /** * Gets info array from given URL, cached data has priority * - * @param string $url URL + * @param string $url * @return array Info * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ @@ -201,12 +202,13 @@ class Images DI::cache()->set($cacheKey, $data); } - return $data; + return $data ?? []; } /** * Gets info from URL uncached - * @param string $url URL + * + * @param string $url * @return array Info array * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ @@ -227,7 +229,12 @@ class Images } if (empty($img_str)) { - $img_str = DI::httpClient()->fetch($url, HttpClientAccept::IMAGE, 4); + try { + $img_str = DI::httpClient()->fetch($url, HttpClientAccept::IMAGE, 4); + } catch (\Exception $exception) { + Logger::notice('Image is invalid', ['url' => $url, 'exception' => $exception]); + return []; + } } if (!$img_str) { @@ -243,10 +250,16 @@ class Images } if ($data) { + $image = new Image($img_str); + + if ($image->isValid()) { + $data['blurhash'] = $image->getBlurHash(); + } + $data['size'] = $filesize; } - return $data; + return is_array($data) ? $data : []; } /** @@ -303,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]'; + } }