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 $data = DI::cache()->get($url);
189 if (empty($data) || !is_array($data)) {
190 $data = self::getInfoFromURL($url);
192 DI::cache()->set($url, $data);
201 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
203 public static function getInfoFromURL($url)
211 if (Network::isLocalLink($url) && ($data = Photo::getResourceData($url))) {
212 $photo = Photo::selectFirst([], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
213 if (!empty($photo)) {
214 $img_str = Photo::getImageDataForPhoto($photo);
216 // @todo Possibly add a check for locally stored files
219 if (empty($img_str)) {
220 $img_str = DI::httpClient()->fetch($url, 4);
227 $filesize = strlen($img_str);
230 $data = @getimagesizefromstring($img_str);
231 } catch (\Exception $e) {
236 $data['size'] = $filesize;
243 * @param integer $width
244 * @param integer $height
245 * @param integer $max
248 public static function getScalingDimensions($width, $height, $max)
250 if ((!$width) || (!$height)) {
251 return ['width' => 0, 'height' => 0];
254 if ($width > $max && $height > $max) {
255 // very tall image (greater than 16:9)
256 // constrain the width - let the height float.
258 if ((($height * 9) / 16) > $width) {
260 $dest_height = intval(($height * $max) / $width);
261 } elseif ($width > $height) {
262 // else constrain both dimensions
264 $dest_height = intval(($height * $max) / $width);
266 $dest_width = intval(($width * $max) / $height);
272 $dest_height = intval(($height * $max) / $width);
274 if ($height > $max) {
275 // very tall image (greater than 16:9)
276 // but width is OK - don't do anything
278 if ((($height * 9) / 16) > $width) {
279 $dest_width = $width;
280 $dest_height = $height;
282 $dest_width = intval(($width * $max) / $height);
286 $dest_width = $width;
287 $dest_height = $height;
292 return ['width' => $dest_width, 'height' => $dest_height];