]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
Merge remote-tracking branch 'remotes/upstream/master'
[friendica.git] / include / Photo.php
1 <?php
2
3 if(! class_exists("Photo")) {
4 class Photo {
5
6         private $image;
7         private $width;
8         private $height;
9         private $valid;
10         private $type;
11         private $types;
12
13         /**
14          * supported mimetypes and corresponding file extensions
15          */
16         static function supportedTypes() {
17                 $t = array();
18                 $t['image/jpeg'] ='jpg';
19                 if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
20                 return $t;
21         }
22
23         public function __construct($data, $type="image/jpeg") {
24
25                 $this->types = $this->supportedTypes();
26                 if (!array_key_exists($type,$this->types)){
27                         $type='image/jpeg';
28                 }
29                 $this->valid = false;
30                 $this->type = $type;
31                 $this->image = @imagecreatefromstring($data);
32                 if($this->image !== FALSE) {
33                         $this->width  = imagesx($this->image);
34                         $this->height = imagesy($this->image);
35                         $this->valid  = true;
36                         imagealphablending($this->image, false);
37                         imagesavealpha($this->image, true);
38                 }
39         }
40
41         public function __destruct() {
42                 if($this->image)
43                         imagedestroy($this->image);
44         }
45
46         public function is_valid() {
47                 return $this->valid;
48         }
49
50         public function getWidth() {
51                 return $this->width;
52         }
53
54         public function getHeight() {
55                 return $this->height;
56         }
57
58         public function getImage() {
59                 return $this->image;
60         }
61         
62         public function getType() {
63                 return $this->type;
64         }
65         public function getExt() {
66                 return $this->types[$this->type];
67         }
68
69         public function scaleImage($max) {
70
71                 $width = $this->width;
72                 $height = $this->height;
73
74                 $dest_width = $dest_height = 0;
75
76                 if((! $width)|| (! $height))
77                         return FALSE;
78
79                 if($width > $max && $height > $max) {
80                         if($width > $height) {
81                                 $dest_width = $max;
82                                 $dest_height = intval(( $height * $max ) / $width);
83                         }
84                         else {
85                                 $dest_width = intval(( $width * $max ) / $height);
86                                 $dest_height = $max;
87                         }
88                 }
89                 else {
90                         if( $width > $max ) {
91                                 $dest_width = $max;
92                                 $dest_height = intval(( $height * $max ) / $width);
93                         }
94                         else {
95                                 if( $height > $max ) {
96                                         $dest_width = intval(( $width * $max ) / $height);
97                                         $dest_height = $max;
98                                 }
99                                 else {
100                                         $dest_width = $width;
101                                         $dest_height = $height;
102                                 }
103                         }
104                 }
105
106
107                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
108                 imagealphablending($dest, false);
109                 imagesavealpha($dest, true);
110                 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
111                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
112                 if($this->image)
113                         imagedestroy($this->image);
114                 $this->image = $dest;
115                 $this->width  = imagesx($this->image);
116                 $this->height = imagesy($this->image);
117
118         }
119
120         public function rotate($degrees) {
121                 $this->image  = imagerotate($this->image,$degrees,0);
122                 $this->width  = imagesx($this->image);
123                 $this->height = imagesy($this->image);
124         }       
125
126
127
128         public function scaleImageUp($min) {
129
130                 $width = $this->width;
131                 $height = $this->height;
132
133                 $dest_width = $dest_height = 0;
134
135                 if((! $width)|| (! $height))
136                         return FALSE;
137
138                 if($width < $min && $height < $min) {
139                         if($width > $height) {
140                                 $dest_width = $min;
141                                 $dest_height = intval(( $height * $min ) / $width);
142                         }
143                         else {
144                                 $dest_width = intval(( $width * $min ) / $height);
145                                 $dest_height = $min;
146                         }
147                 }
148                 else {
149                         if( $width < $min ) {
150                                 $dest_width = $min;
151                                 $dest_height = intval(( $height * $min ) / $width);
152                         }
153                         else {
154                                 if( $height < $min ) {
155                                         $dest_width = intval(( $width * $min ) / $height);
156                                         $dest_height = $min;
157                                 }
158                                 else {
159                                         $dest_width = $width;
160                                         $dest_height = $height;
161                                 }
162                         }
163                 }
164
165
166                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
167                 imagealphablending($dest, false);
168                 imagesavealpha($dest, true);
169                 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
170                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
171                 if($this->image)
172                         imagedestroy($this->image);
173                 $this->image = $dest;
174                 $this->width  = imagesx($this->image);
175                 $this->height = imagesy($this->image);
176
177         }
178
179
180
181         public function scaleImageSquare($dim) {
182
183                 $dest = imagecreatetruecolor( $dim, $dim );
184                 imagealphablending($dest, false);
185                 imagesavealpha($dest, true);
186                 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
187                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
188                 if($this->image)
189                         imagedestroy($this->image);
190                 $this->image = $dest;
191                 $this->width  = imagesx($this->image);
192                 $this->height = imagesy($this->image);
193         }
194
195
196         public function cropImage($max,$x,$y,$w,$h) {
197                 $dest = imagecreatetruecolor( $max, $max );
198                 imagealphablending($dest, false);
199                 imagesavealpha($dest, true);
200                 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
201                 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
202                 if($this->image)
203                         imagedestroy($this->image);
204                 $this->image = $dest;
205                 $this->width  = imagesx($this->image);
206                 $this->height = imagesy($this->image);
207         }
208
209         public function saveImage($path) {
210                 switch($this->type){
211                         case "image/png":
212                                 $quality = get_config('system','png_quality');
213                                 if((! $quality) || ($quality > 9))
214                                         $quality = PNG_QUALITY;
215                                 imagepng($this->image, $path, $quality);
216                                 break;
217                         default:
218                                 $quality = get_config('system','jpeg_quality');
219                                 if((! $quality) || ($quality > 100))
220                                         $quality = JPEG_QUALITY;
221                                 imagejpeg($this->image,$path,$quality);
222                 }
223                 
224         }
225
226         public function imageString() {
227                 ob_start();
228                 switch($this->type){
229                         case "image/png":
230                                 $quality = get_config('system','png_quality');
231                                 if((! $quality) || ($quality > 9))
232                                         $quality = PNG_QUALITY;
233                                 imagepng($this->image,NULL, $quality);
234                                 break;
235                         default:
236                                 $quality = get_config('system','jpeg_quality');
237                                 if((! $quality) || ($quality > 100))
238                                         $quality = JPEG_QUALITY;
239
240                                 imagejpeg($this->image,NULL,$quality);
241                 }
242                 $s = ob_get_contents();
243                 ob_end_clean();
244                 return $s;
245         }
246
247
248
249         public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
250
251                 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
252                         dbesc($rid)
253                 );
254                 if(count($r))
255                         $guid = $r[0]['guid'];
256                 else
257                         $guid = get_guid();
258
259                 $r = q("INSERT INTO `photo`
260                         ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
261                         VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
262                         intval($uid),
263                         intval($cid),
264                         dbesc($guid),
265                         dbesc($rid),
266                         dbesc(datetime_convert()),
267                         dbesc(datetime_convert()),
268                         dbesc(basename($filename)),
269                         dbesc($this->type),
270                         dbesc($album),
271                         intval($this->height),
272                         intval($this->width),
273                         dbesc($this->imageString()),
274                         intval($scale),
275                         intval($profile),
276                         dbesc($allow_cid),
277                         dbesc($allow_gid),
278                         dbesc($deny_cid),
279                         dbesc($deny_gid)
280                 );
281                 return $r;
282         }
283
284
285
286
287
288 }}
289
290
291 /**
292  * Guess image mimetype from filename or from Content-Type header
293  * 
294  * @arg $filename string Image filename
295  * @arg $fromcurl boolean Check Content-Type header from curl request
296  */
297 function guess_image_type($filename, $fromcurl=false) {
298     logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
299         $type = null;
300         if ($fromcurl) {
301                 $a = get_app(); 
302                 $headers=array();
303                 $h = explode("\n",$a->get_curl_headers());
304                 foreach ($h as $l) {
305                         list($k,$v) = array_map("trim", explode(":", trim($l), 2));
306                         $headers[$k] = $v;
307                 }
308                 if (array_key_exists('Content-Type', $headers))
309                         $type = $headers['Content-Type'];
310         }
311         if (is_null($type)){
312                 $ext = pathinfo($filename, PATHINFO_EXTENSION);
313                 $types = Photo::supportedTypes();
314                 $type = "image/jpeg";
315                 foreach ($types as $m=>$e){
316                         if ($ext==$e) $type = $m;
317                 }
318
319         }
320     logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
321         return $type;
322         
323 }
324
325 function import_profile_photo($photo,$uid,$cid) {
326
327         $a = get_app();
328
329         $photo_failure = false;
330
331         $filename = basename($photo);
332         $img_str = fetch_url($photo,true);
333         
334         // guess mimetype from headers or filename
335         $type = guess_image_type($photo,true);
336
337         
338         $img = new Photo($img_str, $type);
339         if($img->is_valid()) {
340
341                 $img->scaleImageSquare(175);
342                                         
343                 $hash = photo_new_resource();
344
345                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
346
347                 if($r === false)
348                         $photo_failure = true;
349
350                 $img->scaleImage(80);
351
352                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
353
354                 if($r === false)
355                         $photo_failure = true;
356
357                 $img->scaleImage(48);
358
359                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
360
361                 if($r === false)
362                         $photo_failure = true;
363
364
365
366                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
367                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
368                 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
369         }
370         else
371                 $photo_failure = true;
372
373         if($photo_failure) {
374                 $photo = $a->get_baseurl() . '/images/person-175.jpg';
375                 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
376                 $micro = $a->get_baseurl() . '/images/person-48.jpg';
377         }
378
379         return(array($photo,$thumb,$micro));
380
381 }