3 * @copyright Copyright (C) 2010-2021, 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;
25 use Friendica\Core\System;
34 * Maps Mime types to Imagick formats
38 public static function getFormatsMap()
41 'image/jpeg' => 'JPG',
50 * Returns supported image mimetypes and corresponding file extensions
54 public static function supportedTypes()
59 if (class_exists('Imagick')) {
60 // Imagick::queryFormats won't help us a lot there...
61 // At least, not yet, other parts of friendica uses this array
66 } elseif (imagetypes() & IMG_PNG) {
76 * Fetch image mimetype from the image data or guessing from the file name
78 * @param string $image_data Image data
79 * @param string $filename File name (for guessing the type via the extension)
80 * @param string $mime default mime type
85 public static function getMimeTypeByData(string $image_data, string $filename = '', string $mime = '')
87 if (substr($mime, 0, 6) == 'image/') {
88 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
92 $image = @getimagesizefromstring($image_data);
93 if (!empty($image['mime'])) {
94 Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mime, 'mime' => $image['mime']]);
95 return $image['mime'];
98 return self::guessTypeByExtension($filename);
102 * Fetch image mimetype from the image data or guessing from the file name
104 * @param string $sourcefile Source file of the image
105 * @param string $filename File name (for guessing the type via the extension)
106 * @param string $mime default mime type
111 public static function getMimeTypeBySource(string $sourcefile, string $filename = '', string $mime = '')
113 if (substr($mime, 0, 6) == 'image/') {
114 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
118 $image = @getimagesize($sourcefile);
119 if (!empty($image['mime'])) {
120 Logger::info('Mime type detected via file', ['filename' => $filename, 'default' => $mime, 'image' => $image]);
121 return $image['mime'];
124 return self::guessTypeByExtension($filename);
128 * Guess image mimetype from the filename
130 * @param string $filename Image filename
135 public static function guessTypeByExtension(string $filename)
137 $ext = pathinfo(parse_url($filename, PHP_URL_PATH), PATHINFO_EXTENSION);
138 $types = self::supportedTypes();
139 $type = 'image/jpeg';
140 foreach ($types as $m => $e) {
146 Logger::info('Mime type guessed via extension', ['filename' => $filename, 'type' => $type]);
153 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
155 public static function getInfoFromURLCached($url)
163 $data = DI::cache()->get($url);
165 if (empty($data) || !is_array($data)) {
166 $data = self::getInfoFromURL($url);
168 DI::cache()->set($url, $data);
177 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
179 public static function getInfoFromURL($url)
187 $img_str = DI::httpRequest()->fetch($url, 4);
193 $filesize = strlen($img_str);
196 if (function_exists("getimagesizefromstring")) {
197 $data = @getimagesizefromstring($img_str);
199 $tempfile = tempnam(get_temppath(), "cache");
201 $stamp1 = microtime(true);
202 file_put_contents($tempfile, $img_str);
203 DI::profiler()->saveTimestamp($stamp1, "file");
205 $data = getimagesize($tempfile);
208 } catch (\Exception $e) {
213 $data['size'] = $filesize;
220 * @param integer $width
221 * @param integer $height
222 * @param integer $max
225 public static function getScalingDimensions($width, $height, $max)
227 if ((!$width) || (!$height)) {
228 return ['width' => 0, 'height' => 0];
231 if ($width > $max && $height > $max) {
232 // very tall image (greater than 16:9)
233 // constrain the width - let the height float.
235 if ((($height * 9) / 16) > $width) {
237 $dest_height = intval(($height * $max) / $width);
238 } elseif ($width > $height) {
239 // else constrain both dimensions
241 $dest_height = intval(($height * $max) / $width);
243 $dest_width = intval(($width * $max) / $height);
249 $dest_height = intval(($height * $max) / $width);
251 if ($height > $max) {
252 // very tall image (greater than 16:9)
253 // but width is OK - don't do anything
255 if ((($height * 9) / 16) > $width) {
256 $dest_width = $width;
257 $dest_height = $height;
259 $dest_width = intval(($width * $max) / $height);
263 $dest_width = $width;
264 $dest_height = $height;
269 return ['width' => $dest_width, 'height' => $dest_height];