]> git.mxchange.org Git - friendica.git/blob - src/Util/Images.php
1b52b91a133e62b359cc3cecd0e8ddcde2034565
[friendica.git] / src / Util / Images.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\DI;
27
28 /**
29  * Image utilities
30  */
31 class Images
32 {
33         /**
34          * Maps Mime types to Imagick formats
35          *
36          * @return array
37          */
38         public static function getFormatsMap()
39         {
40                 $m = [
41                         'image/jpeg' => 'JPG',
42                         'image/png' => 'PNG',
43                         'image/gif' => 'GIF'
44                 ];
45
46                 return $m;
47         }
48
49         /**
50          * Returns supported image mimetypes and corresponding file extensions
51          *
52          * @return array
53          */
54         public static function supportedTypes()
55         {
56                 $types = [
57                         'image/jpeg' => 'jpg'
58                 ];
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
62                         $types += [
63                                 'image/png' => 'png',
64                                 'image/gif' => 'gif'
65                         ];
66                 } elseif (imagetypes() & IMG_PNG) {
67                         $types += [
68                                 'image/png' => 'png'
69                         ];
70                 }
71
72                 return $types;
73         }
74
75         /**
76          * Fetch image mimetype from the image data or guessing from the file name
77          *
78          * @param string $image_data Image data
79          * @param string $filename   File name (for guessing the type via the extension)
80          * @param string $mimeType   possible mime type
81          *
82          * @return string
83          * @throws \Exception
84          */
85         public static function getMimeTypeByData(string $image_data, string $filename = '', string $mimeType = '')
86         {
87                 if (substr($mimeType, 0, 6) == 'image/') {
88                         Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mimeType]);
89                         return $mimeType;
90                 }
91
92                 $image = @getimagesizefromstring($image_data);
93                 if (!empty($image['mime'])) {
94                         Logger::info('Mime type detected via data', ['filename' => $filename, 'default' => $mimeType, 'mime' => $image['mime']]);
95                         return $image['mime'];
96                 }
97
98                 return self::guessTypeByExtension($filename);
99         }
100
101         /**
102          * Fetch image mimetype from the image data or guessing from the file name
103          *
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
107          *
108          * @return string
109          * @throws \Exception
110          */
111         public static function getMimeTypeBySource(string $sourcefile, string $filename = '', string $mime = '')
112         {
113                 if (substr($mime, 0, 6) == 'image/') {
114                         Logger::info('Using default mime type', ['filename' => $filename, 'mime' => $mime]);
115                         return $mime;
116                 }
117
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'];
122                 }
123
124                 return self::guessTypeByExtension($filename);
125         }
126
127         /**
128          * Guess image mimetype from the filename
129          *
130          * @param string $filename   Image filename
131          *
132          * @return string
133          * @throws \Exception
134          */
135         public static function guessTypeByExtension(string $filename)
136         {
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) {
141                         if ($ext == $e) {
142                                 $type = $m;
143                         }
144                 }
145
146                 Logger::info('Mime type guessed via extension', ['filename' => $filename, 'type' => $type]);
147                 return $type;
148         }
149
150         /**
151          * @param string $url
152          * @return array
153          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
154          */
155         public static function getInfoFromURLCached($url)
156         {
157                 $data = [];
158
159                 if (empty($url)) {
160                         return $data;
161                 }
162
163                 $data = DI::cache()->get($url);
164
165                 if (empty($data) || !is_array($data)) {
166                         $data = self::getInfoFromURL($url);
167
168                         DI::cache()->set($url, $data);
169                 }
170
171                 return $data;
172         }
173
174         /**
175          * @param string $url
176          * @return array
177          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
178          */
179         public static function getInfoFromURL($url)
180         {
181                 $data = [];
182
183                 if (empty($url)) {
184                         return $data;
185                 }
186
187                 $img_str = DI::httpRequest()->fetch($url, true, 4);
188
189                 if (!$img_str) {
190                         return [];
191                 }
192
193                 $filesize = strlen($img_str);
194
195                 try {
196                         if (function_exists("getimagesizefromstring")) {
197                                 $data = @getimagesizefromstring($img_str);
198                         } else {
199                                 $tempfile = tempnam(get_temppath(), "cache");
200
201                                 $stamp1 = microtime(true);
202                                 file_put_contents($tempfile, $img_str);
203                                 DI::profiler()->saveTimestamp($stamp1, "file");
204
205                                 $data = getimagesize($tempfile);
206                                 unlink($tempfile);
207                         }
208                 } catch (\Exception $e) {
209                         return [];
210                 }
211
212                 if ($data) {
213                         $data['size'] = $filesize;
214                 }
215
216                 return $data;
217         }
218
219         /**
220          * @param integer $width
221          * @param integer $height
222          * @param integer $max
223          * @return array
224          */
225         public static function getScalingDimensions($width, $height, $max)
226         {
227                 if ((!$width) || (!$height)) {
228                         return ['width' => 0, 'height' => 0];
229                 }
230
231                 if ($width > $max && $height > $max) {
232                         // very tall image (greater than 16:9)
233                         // constrain the width - let the height float.
234
235                         if ((($height * 9) / 16) > $width) {
236                                 $dest_width = $max;
237                                 $dest_height = intval(($height * $max) / $width);
238                         } elseif ($width > $height) {
239                                 // else constrain both dimensions
240                                 $dest_width = $max;
241                                 $dest_height = intval(($height * $max) / $width);
242                         } else {
243                                 $dest_width = intval(($width * $max) / $height);
244                                 $dest_height = $max;
245                         }
246                 } else {
247                         if ($width > $max) {
248                                 $dest_width = $max;
249                                 $dest_height = intval(($height * $max) / $width);
250                         } else {
251                                 if ($height > $max) {
252                                         // very tall image (greater than 16:9)
253                                         // but width is OK - don't do anything
254
255                                         if ((($height * 9) / 16) > $width) {
256                                                 $dest_width = $width;
257                                                 $dest_height = $height;
258                                         } else {
259                                                 $dest_width = intval(($width * $max) / $height);
260                                                 $dest_height = $max;
261                                         }
262                                 } else {
263                                         $dest_width = $width;
264                                         $dest_height = $height;
265                                 }
266                         }
267                 }
268
269                 return ['width' => $dest_width, 'height' => $dest_height];
270         }
271 }