3 * @copyright Copyright (C) 2020, Friendica
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;
25 use Friendica\Core\System;
27 use Friendica\Network\HTTPRequest;
35 * Maps Mime types to Imagick formats
39 public static function getFormatsMap()
42 'image/jpeg' => 'JPG',
51 * Returns supported image mimetypes and corresponding file extensions
55 public static function supportedTypes()
60 if (class_exists('Imagick')) {
61 // Imagick::queryFormats won't help us a lot there...
62 // At least, not yet, other parts of friendica uses this array
67 } elseif (imagetypes() & IMG_PNG) {
77 * Fetch image mimetype from the image data or guessing from the file name
79 * @param string $image_data Image data
80 * @param string $filename File name (for guessing the type via the extension)
81 * @param string $mime default mime type
86 public static function getMimeTypeByData(string $image_data, string $filename = '', string $mime = '')
88 if (substr($mime, 0, 6) == 'image/') {
89 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
93 $image = @getimagesizefromstring($image_data);
94 if (!empty($image['mime'])) {
95 Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mime, 'mime' => $image['mime']]);
96 return $image['mime'];
99 return self::guessTypeByExtension($filename);
103 * Fetch image mimetype from the image data or guessing from the file name
105 * @param string $sourcefile Source file of the image
106 * @param string $filename File name (for guessing the type via the extension)
107 * @param string $mime default mime type
112 public static function getMimeTypeBySource(string $sourcefile, string $filename = '', string $mime = '')
114 if (substr($mime, 0, 6) == 'image/') {
115 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
119 $image = @getimagesize($sourcefile);
120 if (!empty($image['mime'])) {
121 Logger::info('Mime type detected via file', ['filename' => $filename, 'default' => $mime, 'image' => $image]);
122 return $image['mime'];
125 return self::guessTypeByExtension($filename);
129 * Guess image mimetype from the filename
131 * @param string $filename Image filename
136 public static function guessTypeByExtension(string $filename)
138 $ext = pathinfo(parse_url($filename, PHP_URL_PATH), PATHINFO_EXTENSION);
139 $types = self::supportedTypes();
140 $type = 'image/jpeg';
141 foreach ($types as $m => $e) {
147 Logger::info('Mime type guessed via extension', ['filename' => $filename, 'type' => $type]);
154 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
156 public static function getInfoFromURLCached($url)
164 $data = DI::cache()->get($url);
166 if (empty($data) || !is_array($data)) {
167 $data = self::getInfoFromURL($url);
169 DI::cache()->set($url, $data);
178 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
180 public static function getInfoFromURL($url)
188 $img_str = HTTPRequest::fetchUrl($url, true, 4);
194 $filesize = strlen($img_str);
197 if (function_exists("getimagesizefromstring")) {
198 $data = @getimagesizefromstring($img_str);
200 $tempfile = tempnam(get_temppath(), "cache");
202 $stamp1 = microtime(true);
203 file_put_contents($tempfile, $img_str);
204 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
206 $data = getimagesize($tempfile);
209 } catch (\Exception $e) {
214 $data['size'] = $filesize;
221 * @param integer $width
222 * @param integer $height
223 * @param integer $max
226 public static function getScalingDimensions($width, $height, $max)
228 if ((!$width) || (!$height)) {
229 return ['width' => 0, 'height' => 0];
232 if ($width > $max && $height > $max) {
233 // very tall image (greater than 16:9)
234 // constrain the width - let the height float.
236 if ((($height * 9) / 16) > $width) {
238 $dest_height = intval(($height * $max) / $width);
239 } elseif ($width > $height) {
240 // else constrain both dimensions
242 $dest_height = intval(($height * $max) / $width);
244 $dest_width = intval(($width * $max) / $height);
250 $dest_height = intval(($height * $max) / $width);
252 if ($height > $max) {
253 // very tall image (greater than 16:9)
254 // but width is OK - don't do anything
256 if ((($height * 9) / 16) > $width) {
257 $dest_width = $width;
258 $dest_height = $height;
260 $dest_width = intval(($width * $max) / $height);
264 $dest_width = $width;
265 $dest_height = $height;
270 return ['width' => $dest_width, 'height' => $dest_height];