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
39 public static function getFormatsMap()
42 'image/jpeg' => 'JPG',
51 * Return file extension for mime type
52 * @param string $mimetype
55 public static function getExtensionByMimeType(string $mimetype): string
59 $imagetype = IMAGETYPE_PNG;
63 $imagetype = IMAGETYPE_GIF;
67 $imagetype = IMAGETYPE_JPEG;
71 return image_type_to_extension($imagetype);
75 * Returns supported image mimetypes and corresponding file extensions
79 public static function supportedTypes()
84 if (class_exists('Imagick')) {
85 // Imagick::queryFormats won't help us a lot there...
86 // At least, not yet, other parts of friendica uses this array
91 } elseif (imagetypes() & IMG_PNG) {
101 * Fetch image mimetype from the image data or guessing from the file name
103 * @param string $image_data Image data
104 * @param string $filename File name (for guessing the type via the extension)
105 * @param string $mime default mime type
110 public static function getMimeTypeByData(string $image_data, string $filename = '', string $mime = '')
112 if (substr($mime, 0, 6) == 'image/') {
113 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
117 $image = @getimagesizefromstring($image_data);
118 if (!empty($image['mime'])) {
119 Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mime, 'mime' => $image['mime']]);
120 return $image['mime'];
123 return self::guessTypeByExtension($filename);
127 * Fetch image mimetype from the image data or guessing from the file name
129 * @param string $sourcefile Source file of the image
130 * @param string $filename File name (for guessing the type via the extension)
131 * @param string $mime default mime type
136 public static function getMimeTypeBySource(string $sourcefile, string $filename = '', string $mime = '')
138 if (substr($mime, 0, 6) == 'image/') {
139 Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
143 $image = @getimagesize($sourcefile);
144 if (!empty($image['mime'])) {
145 Logger::info('Mime type detected via file', ['filename' => $filename, 'default' => $mime, 'image' => $image]);
146 return $image['mime'];
149 return self::guessTypeByExtension($filename);
153 * Guess image mimetype from the filename
155 * @param string $filename Image filename
160 public static function guessTypeByExtension(string $filename)
162 $ext = pathinfo(parse_url($filename, PHP_URL_PATH), PATHINFO_EXTENSION);
163 $types = self::supportedTypes();
164 $type = 'image/jpeg';
165 foreach ($types as $m => $e) {
171 Logger::info('Mime type guessed via extension', ['filename' => $filename, 'type' => $type]);
178 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
180 public static function getInfoFromURLCached($url)
188 $cacheKey = 'getInfoFromURL:' . sha1($url);
190 $data = DI::cache()->get($cacheKey);
192 if (empty($data) || !is_array($data)) {
193 $data = self::getInfoFromURL($url);
195 DI::cache()->set($cacheKey, $data);
204 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
206 public static function getInfoFromURL($url)
214 if (Network::isLocalLink($url) && ($data = Photo::getResourceData($url))) {
215 $photo = Photo::selectFirst([], ['resource-id' => $data['guid'], 'scale' => $data['scale']]);
216 if (!empty($photo)) {
217 $img_str = Photo::getImageDataForPhoto($photo);
219 // @todo Possibly add a check for locally stored files
222 if (empty($img_str)) {
223 $img_str = DI::httpClient()->fetch($url, HttpClientAccept::IMAGE, 4);
230 $filesize = strlen($img_str);
233 $data = @getimagesizefromstring($img_str);
234 } catch (\Exception $e) {
239 $data['size'] = $filesize;
246 * @param integer $width
247 * @param integer $height
248 * @param integer $max
251 public static function getScalingDimensions($width, $height, $max)
253 if ((!$width) || (!$height)) {
254 return ['width' => 0, 'height' => 0];
257 if ($width > $max && $height > $max) {
258 // very tall image (greater than 16:9)
259 // constrain the width - let the height float.
261 if ((($height * 9) / 16) > $width) {
263 $dest_height = intval(($height * $max) / $width);
264 } elseif ($width > $height) {
265 // else constrain both dimensions
267 $dest_height = intval(($height * $max) / $width);
269 $dest_width = intval(($width * $max) / $height);
275 $dest_height = intval(($height * $max) / $width);
277 if ($height > $max) {
278 // very tall image (greater than 16:9)
279 // but width is OK - don't do anything
281 if ((($height * 9) / 16) > $width) {
282 $dest_width = $width;
283 $dest_height = $height;
285 $dest_width = intval(($width * $max) / $height);
289 $dest_width = $width;
290 $dest_height = $height;
295 return ['width' => $dest_width, 'height' => $dest_height];