]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
exif_read_data may not be available and isn't a requirement - check and silently...
[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         public function flip($horiz = true, $vert = false) {
127                 $w = imagesx($this->image);
128                 $h = imagesy($this->image);
129                 $flipped = imagecreate($w, $h);
130                 if($horiz) {
131                         for ($x = 0; $x < $w; $x++) {
132                                 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
133                         }
134                 }
135                 if($vert) {
136                         for ($y = 0; $y < $h; $y++) {
137                                 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
138                         }
139                 }
140                 $this->image = $flipped;
141         }
142
143         public function orient($filename) {
144                 // based off comment on http://php.net/manual/en/function.imagerotate.php
145
146                 if(! function_exists('exif_read_data'))
147                         return;
148
149                 $exif = exif_read_data($filename);
150                 $ort = $exif['Orientation'];
151
152                 switch($ort)
153                 {
154                         case 1: // nothing
155                         break;
156
157                 case 2: // horizontal flip
158                     $this->flip();
159                     break;
160                                
161                 case 3: // 180 rotate left
162                 $this->rotate(180);
163                         break;
164                    
165                 case 4: // vertical flip
166                 $this->flip(false, true);
167                         break;
168                
169                 case 5: // vertical flip + 90 rotate right
170                 $this->flip(false, true);
171                 $this->rotate(-90);
172                         break;
173                
174                 case 6: // 90 rotate right
175                     $this->rotate(-90);
176                         break;
177                
178                 case 7: // horizontal flip + 90 rotate right
179                     $this->flip();   
180                     $this->rotate(-90);
181                         break;
182                
183                 case 8:    // 90 rotate left
184                     $this->rotate(90);
185                         break;
186             }
187         }
188
189
190
191         public function scaleImageUp($min) {
192
193                 $width = $this->width;
194                 $height = $this->height;
195
196                 $dest_width = $dest_height = 0;
197
198                 if((! $width)|| (! $height))
199                         return FALSE;
200
201                 if($width < $min && $height < $min) {
202                         if($width > $height) {
203                                 $dest_width = $min;
204                                 $dest_height = intval(( $height * $min ) / $width);
205                         }
206                         else {
207                                 $dest_width = intval(( $width * $min ) / $height);
208                                 $dest_height = $min;
209                         }
210                 }
211                 else {
212                         if( $width < $min ) {
213                                 $dest_width = $min;
214                                 $dest_height = intval(( $height * $min ) / $width);
215                         }
216                         else {
217                                 if( $height < $min ) {
218                                         $dest_width = intval(( $width * $min ) / $height);
219                                         $dest_height = $min;
220                                 }
221                                 else {
222                                         $dest_width = $width;
223                                         $dest_height = $height;
224                                 }
225                         }
226                 }
227
228
229                 $dest = imagecreatetruecolor( $dest_width, $dest_height );
230                 imagealphablending($dest, false);
231                 imagesavealpha($dest, true);
232                 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
233                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
234                 if($this->image)
235                         imagedestroy($this->image);
236                 $this->image = $dest;
237                 $this->width  = imagesx($this->image);
238                 $this->height = imagesy($this->image);
239
240         }
241
242
243
244         public function scaleImageSquare($dim) {
245
246                 $dest = imagecreatetruecolor( $dim, $dim );
247                 imagealphablending($dest, false);
248                 imagesavealpha($dest, true);
249                 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
250                 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
251                 if($this->image)
252                         imagedestroy($this->image);
253                 $this->image = $dest;
254                 $this->width  = imagesx($this->image);
255                 $this->height = imagesy($this->image);
256         }
257
258
259         public function cropImage($max,$x,$y,$w,$h) {
260                 $dest = imagecreatetruecolor( $max, $max );
261                 imagealphablending($dest, false);
262                 imagesavealpha($dest, true);
263                 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
264                 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
265                 if($this->image)
266                         imagedestroy($this->image);
267                 $this->image = $dest;
268                 $this->width  = imagesx($this->image);
269                 $this->height = imagesy($this->image);
270         }
271
272         public function saveImage($path) {
273                 switch($this->type){
274                         case "image/png":
275                                 $quality = get_config('system','png_quality');
276                                 if((! $quality) || ($quality > 9))
277                                         $quality = PNG_QUALITY;
278                                 imagepng($this->image, $path, $quality);
279                                 break;
280                         default:
281                                 $quality = get_config('system','jpeg_quality');
282                                 if((! $quality) || ($quality > 100))
283                                         $quality = JPEG_QUALITY;
284                                 imagejpeg($this->image,$path,$quality);
285                 }
286                 
287         }
288
289         public function imageString() {
290                 ob_start();
291                 switch($this->type){
292                         case "image/png":
293                                 $quality = get_config('system','png_quality');
294                                 if((! $quality) || ($quality > 9))
295                                         $quality = PNG_QUALITY;
296                                 imagepng($this->image,NULL, $quality);
297                                 break;
298                         default:
299                                 $quality = get_config('system','jpeg_quality');
300                                 if((! $quality) || ($quality > 100))
301                                         $quality = JPEG_QUALITY;
302
303                                 imagejpeg($this->image,NULL,$quality);
304                 }
305                 $s = ob_get_contents();
306                 ob_end_clean();
307                 return $s;
308         }
309
310
311
312         public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
313
314                 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
315                         dbesc($rid)
316                 );
317                 if(count($r))
318                         $guid = $r[0]['guid'];
319                 else
320                         $guid = get_guid();
321
322                 $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
323                                 dbesc($rid),
324                                 intval($uid),
325                                 intval($cid),
326                                 intval($scale)
327                 );
328                 if(count($x)) {
329                         $r = q("UPDATE `photo`
330                                 set `uid` = %d, 
331                                 `contact-id` = %d, 
332                                 `guid` = '%s', 
333                                 `resource-id` = '%s', 
334                                 `created` = '%s',
335                                 `edited` = '%s', 
336                                 `filename` = '%s', 
337                                 `type` = '%s', 
338                                 `album` = '%s', 
339                                 `height` = %d, 
340                                 `width` = %d, 
341                                 `data` = '%s', 
342                                 `scale` = %d, 
343                                 `profile` = %d, 
344                                 `allow_cid` = '%s', 
345                                 `allow_gid` = '%s', 
346                                 `deny_cid` = '%s',
347                                 `deny_gid` = '%s' 
348                                 where id = %d limit 1",
349
350                                 intval($uid),
351                                 intval($cid),
352                                 dbesc($guid),
353                                 dbesc($rid),
354                                 dbesc(datetime_convert()),
355                                 dbesc(datetime_convert()),
356                                 dbesc(basename($filename)),
357                                 dbesc($this->type),
358                                 dbesc($album),
359                                 intval($this->height),
360                                 intval($this->width),
361                                 dbesc($this->imageString()),
362                                 intval($scale),
363                                 intval($profile),
364                                 dbesc($allow_cid),
365                                 dbesc($allow_gid),
366                                 dbesc($deny_cid),
367                                 dbesc($deny_gid),
368                                 intval($x[0]['id'])
369                         );
370                 }
371                 else {
372                         $r = q("INSERT INTO `photo`
373                                 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
374                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
375                                 intval($uid),
376                                 intval($cid),
377                                 dbesc($guid),
378                                 dbesc($rid),
379                                 dbesc(datetime_convert()),
380                                 dbesc(datetime_convert()),
381                                 dbesc(basename($filename)),
382                                 dbesc($this->type),
383                                 dbesc($album),
384                                 intval($this->height),
385                                 intval($this->width),
386                                 dbesc($this->imageString()),
387                                 intval($scale),
388                                 intval($profile),
389                                 dbesc($allow_cid),
390                                 dbesc($allow_gid),
391                                 dbesc($deny_cid),
392                                 dbesc($deny_gid)
393                         );
394                 }
395                 return $r;
396         }
397 }}
398
399
400 /**
401  * Guess image mimetype from filename or from Content-Type header
402  * 
403  * @arg $filename string Image filename
404  * @arg $fromcurl boolean Check Content-Type header from curl request
405  */
406 function guess_image_type($filename, $fromcurl=false) {
407     logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
408         $type = null;
409         if ($fromcurl) {
410                 $a = get_app(); 
411                 $headers=array();
412                 $h = explode("\n",$a->get_curl_headers());
413                 foreach ($h as $l) {
414                         list($k,$v) = array_map("trim", explode(":", trim($l), 2));
415                         $headers[$k] = $v;
416                 }
417                 if (array_key_exists('Content-Type', $headers))
418                         $type = $headers['Content-Type'];
419         }
420         if (is_null($type)){
421                 $ext = pathinfo($filename, PATHINFO_EXTENSION);
422                 $types = Photo::supportedTypes();
423                 $type = "image/jpeg";
424                 foreach ($types as $m=>$e){
425                         if ($ext==$e) $type = $m;
426                 }
427
428         }
429     logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
430         return $type;
431         
432 }
433
434 function import_profile_photo($photo,$uid,$cid) {
435
436         $a = get_app();
437
438         $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
439                 intval($uid),
440                 intval($cid)
441         );
442         if(count($r)) {
443                 $hash = $r[0]['resource-id'];
444         }
445         else {
446                 $hash = photo_new_resource();
447         }
448                 
449         $photo_failure = false;
450
451         $filename = basename($photo);
452         $img_str = fetch_url($photo,true);
453         
454         // guess mimetype from headers or filename
455         $type = guess_image_type($photo,true);
456
457         
458         $img = new Photo($img_str, $type);
459         if($img->is_valid()) {
460
461                 $img->scaleImageSquare(175);
462                                         
463                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
464
465                 if($r === false)
466                         $photo_failure = true;
467
468                 $img->scaleImage(80);
469
470                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
471
472                 if($r === false)
473                         $photo_failure = true;
474
475                 $img->scaleImage(48);
476
477                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
478
479                 if($r === false)
480                         $photo_failure = true;
481
482                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
483                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
484                 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
485         }
486         else
487                 $photo_failure = true;
488
489         if($photo_failure) {
490                 $photo = $a->get_baseurl() . '/images/person-175.jpg';
491                 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
492                 $micro = $a->get_baseurl() . '/images/person-48.jpg';
493         }
494
495         return(array($photo,$thumb,$micro));
496
497 }