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;
34 * Maps Mime types to Imagick formats
38 public static function getFormatsMap()
41 'image/jpeg' => 'JPG',
50 * Return file extension for mime type
51 * @param string $mimetype
54 public static function getExtensionByMimeType(string $mimetype): string
58 $imagetype = IMAGETYPE_PNG;
62 $imagetype = IMAGETYPE_GIF;
66 $imagetype = IMAGETYPE_JPEG;
70 return image_type_to_extension($imagetype);
74 * Returns supported image mimetypes and corresponding file extensions
78 public static function supportedTypes()
83 if (class_exists('Imagick')) {
84 // Imagick::queryFormats won't help us a lot there...
85 // At least, not yet, other parts of friendica uses this array
90 } elseif (imagetypes() & IMG_PNG) {
100 * Fetch image mimetype from the image data or guessing from the file name
102 * @param string $image_data Image data
103 * @param string $filename File name (for guessing the type via the extension)
104 * @param string $mime default mime type
109 public static function getMimeTypeByData(string $image_data, string $filename = '', string $mime = '')
111 if (substr($mime, 0, 6) == 'image/') {
112 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
116 $image = @getimagesizefromstring($image_data);
117 if (!empty($image['mime'])) {
118 Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mime, 'mime' => $image['mime']]);
119 return $image['mime'];
122 return self::guessTypeByExtension($filename);
126 * Fetch image mimetype from the image data or guessing from the file name
128 * @param string $sourcefile Source file of the image
129 * @param string $filename File name (for guessing the type via the extension)
130 * @param string $mime default mime type
135 public static function getMimeTypeBySource(string $sourcefile, string $filename = '', string $mime = '')
137 if (substr($mime, 0, 6) == 'image/') {
138 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
142 $image = @getimagesize($sourcefile);
143 if (!empty($image['mime'])) {
144 Logger::info('Mime type detected via file', ['filename' => $filename, 'default' => $mime, 'image' => $image]);
145 return $image['mime'];
148 return self::guessTypeByExtension($filename);
152 * Guess image mimetype from the filename
154 * @param string $filename Image filename
159 public static function guessTypeByExtension(string $filename)
161 $ext = pathinfo(parse_url($filename, PHP_URL_PATH), PATHINFO_EXTENSION);
162 $types = self::supportedTypes();
163 $type = 'image/jpeg';
164 foreach ($types as $m => $e) {
170 Logger::info('Mime type guessed via extension', ['filename' => $filename, 'type' => $type]);
177 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
179 public static function getInfoFromURLCached($url)
187 $cacheKey = 'getInfoFromURL:' . sha1($url);
189 $data = DI::cache()->get($cacheKey);
191 if (empty($data) || !is_array($data)) {
192 $data = self::getInfoFromURL($url);
194 DI::cache()->set($cacheKey, $data);
203 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
205 public static function getInfoFromURL($url)
213 if (Network::isLocalLink($url) && ($data = Photo::getResourceData($url))) {
214 $photo = Photo::selectFirst([], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
215 if (!empty($photo)) {
216 $img_str = Photo::getImageDataForPhoto($photo);
218 // @todo Possibly add a check for locally stored files
221 if (empty($img_str)) {
222 $img_str = DI::httpClient()->fetch($url, 4);
229 $filesize = strlen($img_str);
232 $data = @getimagesizefromstring($img_str);
233 } catch (\Exception $e) {
238 $data['size'] = $filesize;
245 * @param integer $width
246 * @param integer $height
247 * @param integer $max
250 public static function getScalingDimensions($width, $height, $max)
252 if ((!$width) || (!$height)) {
253 return ['width' => 0, 'height' => 0];
256 if ($width > $max && $height > $max) {
257 // very tall image (greater than 16:9)
258 // constrain the width - let the height float.
260 if ((($height * 9) / 16) > $width) {
262 $dest_height = intval(($height * $max) / $width);
263 } elseif ($width > $height) {
264 // else constrain both dimensions
266 $dest_height = intval(($height * $max) / $width);
268 $dest_width = intval(($width * $max) / $height);
274 $dest_height = intval(($height * $max) / $width);
276 if ($height > $max) {
277 // very tall image (greater than 16:9)
278 // but width is OK - don't do anything
280 if ((($height * 9) / 16) > $width) {
281 $dest_width = $width;
282 $dest_height = $height;
284 $dest_width = intval(($width * $max) / $height);
288 $dest_width = $width;
289 $dest_height = $height;
294 return ['width' => $dest_width, 'height' => $dest_height];