3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
24 use Friendica\Core\Logger;
26 use Friendica\Model\Photo;
27 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
35 * Maps Mime types to Imagick formats
37 * @return array Format map
39 public static function getFormatsMap()
42 'image/jpeg' => 'JPG',
50 * Return file extension for MIME type
52 * @param string $mimetype MIME type
53 * @return string File extension for MIME type
55 public static function getExtensionByMimeType(string $mimetype): string
59 $imagetype = IMAGETYPE_PNG;
63 $imagetype = IMAGETYPE_GIF;
68 $imagetype = IMAGETYPE_JPEG;
71 default: // Unknown type must be a blob then
76 return image_type_to_extension($imagetype);
80 * Returns supported image mimetypes and corresponding file extensions
84 public static function supportedTypes(): array
87 'image/jpeg' => 'jpg',
91 if (class_exists('Imagick')) {
92 // Imagick::queryFormats won't help us a lot there...
93 // At least, not yet, other parts of friendica uses this array
98 } elseif (imagetypes() & IMG_PNG) {
108 * Fetch image mimetype from the image data or guessing from the file name
110 * @param string $image_data Image data
111 * @param string $filename File name (for guessing the type via the extension)
112 * @param string $default Default MIME type
113 * @return string MIME type
116 public static function getMimeTypeByData(string $image_data, string $filename = '', string $default = ''): string
118 if (substr($default, 0, 6) == 'image/') {
119 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $default]);
123 $image = @getimagesizefromstring($image_data);
124 if (!empty($image['mime'])) {
125 Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $default, 'mime' => $image['mime']]);
126 return $image['mime'];
129 return self::guessTypeByExtension($filename);
133 * Fetch image mimetype from the image data or guessing from the file name
135 * @param string $sourcefile Source file of the image
136 * @param string $filename File name (for guessing the type via the extension)
137 * @param string $default default MIME type
138 * @return string MIME type
141 public static function getMimeTypeBySource(string $sourcefile, string $filename = '', string $default = ''): string
143 if (substr($default, 0, 6) == 'image/') {
144 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $default]);
148 $image = @getimagesize($sourcefile);
149 if (!empty($image['mime'])) {
150 Logger::info('Mime type detected via file', ['filename' => $filename, 'default' => $default, 'image' => $image]);
151 return $image['mime'];
154 return self::guessTypeByExtension($filename);
158 * Guess image MIME type from the filename's extension
160 * @param string $filename Image filename
161 * @return string Guessed MIME type by extension
164 public static function guessTypeByExtension(string $filename): string
166 $ext = pathinfo(parse_url($filename, PHP_URL_PATH), PATHINFO_EXTENSION);
167 $types = self::supportedTypes();
168 $type = 'image/jpeg';
169 foreach ($types as $m => $e) {
175 Logger::info('Mime type guessed via extension', ['filename' => $filename, 'type' => $type]);
180 * Gets info array from given URL, cached data has priority
184 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
186 public static function getInfoFromURLCached(string $url): array
194 $cacheKey = 'getInfoFromURL:' . sha1($url);
196 $data = DI::cache()->get($cacheKey);
198 if (empty($data) || !is_array($data)) {
199 $data = self::getInfoFromURL($url);
201 DI::cache()->set($cacheKey, $data);
208 * Gets info from URL uncached
211 * @return array Info array
212 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
214 public static function getInfoFromURL(string $url): array
222 if (Network::isLocalLink($url) && ($data = Photo::getResourceData($url))) {
223 $photo = Photo::selectFirst([], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
224 if (!empty($photo)) {
225 $img_str = Photo::getImageDataForPhoto($photo);
227 // @todo Possibly add a check for locally stored files
230 if (empty($img_str)) {
231 $img_str = DI::httpClient()->fetch($url, HttpClientAccept::IMAGE, 4);
238 $filesize = strlen($img_str);
241 $data = @getimagesizefromstring($img_str);
242 } catch (\Exception $e) {
247 $data['size'] = $filesize;
250 return is_array($data) ? $data : [];
254 * Returns scaling information
256 * @param integer $width Width
257 * @param integer $height Height
258 * @param integer $max Max width/height
259 * @return array Scaling dimensions
261 public static function getScalingDimensions(int $width, int $height, int $max): array
263 if ((!$width) || (!$height)) {
264 return ['width' => 0, 'height' => 0];
267 if ($width > $max && $height > $max) {
268 // very tall image (greater than 16:9)
269 // constrain the width - let the height float.
271 if ((($height * 9) / 16) > $width) {
273 $dest_height = intval(($height * $max) / $width);
274 } elseif ($width > $height) {
275 // else constrain both dimensions
277 $dest_height = intval(($height * $max) / $width);
279 $dest_width = intval(($width * $max) / $height);
285 $dest_height = intval(($height * $max) / $width);
287 if ($height > $max) {
288 // very tall image (greater than 16:9)
289 // but width is OK - don't do anything
291 if ((($height * 9) / 16) > $width) {
292 $dest_width = $width;
293 $dest_height = $height;
295 $dest_width = intval(($width * $max) / $height);
299 $dest_width = $width;
300 $dest_height = $height;
305 return ['width' => $dest_width, 'height' => $dest_height];