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