3 namespace Friendica\Util;
5 use Friendica\Core\Logger;
6 use Friendica\Core\System;
16 * Maps Mime types to Imagick formats
20 public static function getFormatsMap()
23 'image/jpeg' => 'JPG',
32 * Returns supported image mimetypes and corresponding file extensions
36 public static function supportedTypes()
41 if (class_exists('Imagick')) {
42 // Imagick::queryFormats won't help us a lot there...
43 // At least, not yet, other parts of friendica uses this array
48 } elseif (imagetypes() & IMG_PNG) {
58 * Guess image mimetype from filename or from Content-Type header
60 * @param string $filename Image filename
61 * @param boolean $fromcurl Check Content-Type header from curl request
62 * @param string $header passed headers to take into account
67 public static function guessType($filename, $fromcurl = false, $header = '')
69 Logger::info('Image: guessType: ' . $filename . ($fromcurl ? ' from curl headers' : ''));
73 $h = explode("\n", $header);
75 $data = array_map("trim", explode(":", trim($l), 2));
76 if (count($data) > 1) {
82 if (array_key_exists('Content-Type', $headers)) {
83 $type = $headers['Content-Type'];
88 // Guessing from extension? Isn't that... dangerous?
89 if (class_exists('Imagick') && file_exists($filename) && is_readable($filename)) {
91 * Well, this not much better,
92 * but at least it comes from the data inside the image,
93 * we won't be tricked by a manipulated extension
95 $image = new Imagick($filename);
96 $type = $image->getImageMimeType();
98 $ext = pathinfo($filename, PATHINFO_EXTENSION);
99 $types = self::supportedTypes();
100 $type = 'image/jpeg';
101 foreach ($types as $m => $e) {
109 Logger::info('Image: guessType: type=' . $type);
117 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
119 public static function getInfoFromURLCached($url)
127 $data = DI::cache()->get($url);
129 if (empty($data) || !is_array($data)) {
130 $data = self::getInfoFromURL($url);
132 DI::cache()->set($url, $data);
141 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
143 public static function getInfoFromURL($url)
151 $img_str = Network::fetchUrl($url, true, 4);
157 $filesize = strlen($img_str);
160 if (function_exists("getimagesizefromstring")) {
161 $data = @getimagesizefromstring($img_str);
163 $tempfile = tempnam(get_temppath(), "cache");
165 $stamp1 = microtime(true);
166 file_put_contents($tempfile, $img_str);
167 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
169 $data = getimagesize($tempfile);
172 } catch (\Exception $e) {
177 $data['size'] = $filesize;
184 * @param integer $width
185 * @param integer $height
186 * @param integer $max
189 public static function getScalingDimensions($width, $height, $max)
191 if ((!$width) || (!$height)) {
192 return ['width' => 0, 'height' => 0];
195 if ($width > $max && $height > $max) {
196 // very tall image (greater than 16:9)
197 // constrain the width - let the height float.
199 if ((($height * 9) / 16) > $width) {
201 $dest_height = intval(($height * $max) / $width);
202 } elseif ($width > $height) {
203 // else constrain both dimensions
205 $dest_height = intval(($height * $max) / $width);
207 $dest_width = intval(($width * $max) / $height);
213 $dest_height = intval(($height * $max) / $width);
215 if ($height > $max) {
216 // very tall image (greater than 16:9)
217 // but width is OK - don't do anything
219 if ((($height * 9) / 16) > $width) {
220 $dest_width = $width;
221 $dest_height = $height;
223 $dest_width = intval(($width * $max) / $height);
227 $dest_width = $width;
228 $dest_height = $height;
233 return ['width' => $dest_width, 'height' => $dest_height];