]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
Merge remote branch '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                 $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
260                                 dbesc($rid),
261                                 intval($uid),
262                                 intval($cid),
263                                 intval($scale)
264                 );
265                 if(count($x)) {
266                         $r = q("UPDATE `photo`
267                                 set `uid` = %d, 
268                                 `contact-id` = %d, 
269                                 `guid` = '%s', 
270                                 `resource-id` = '%s', 
271                                 `created` = '%s',
272                                 `edited` = '%s', 
273                                 `filename` = '%s', 
274                                 `type` = '%s', 
275                                 `album` = '%s', 
276                                 `height` = %d, 
277                                 `width` = %d, 
278                                 `data` = '%s', 
279                                 `scale` = %d, 
280                                 `profile` = %d, 
281                                 `allow_cid` = '%s', 
282                                 `allow_gid` = '%s', 
283                                 `deny_cid` = '%s',
284                                 `deny_gid` = '%s' 
285                                 where id = %d limit 1",
286
287                                 intval($uid),
288                                 intval($cid),
289                                 dbesc($guid),
290                                 dbesc($rid),
291                                 dbesc(datetime_convert()),
292                                 dbesc(datetime_convert()),
293                                 dbesc(basename($filename)),
294                                 dbesc($this->type),
295                                 dbesc($album),
296                                 intval($this->height),
297                                 intval($this->width),
298                                 dbesc($this->imageString()),
299                                 intval($scale),
300                                 intval($profile),
301                                 dbesc($allow_cid),
302                                 dbesc($allow_gid),
303                                 dbesc($deny_cid),
304                                 dbesc($deny_gid),
305                                 intval($x[0]['id'])
306                         );
307                 }
308                 else {
309                         $r = q("INSERT INTO `photo`
310                                 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
311                                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
312                                 intval($uid),
313                                 intval($cid),
314                                 dbesc($guid),
315                                 dbesc($rid),
316                                 dbesc(datetime_convert()),
317                                 dbesc(datetime_convert()),
318                                 dbesc(basename($filename)),
319                                 dbesc($this->type),
320                                 dbesc($album),
321                                 intval($this->height),
322                                 intval($this->width),
323                                 dbesc($this->imageString()),
324                                 intval($scale),
325                                 intval($profile),
326                                 dbesc($allow_cid),
327                                 dbesc($allow_gid),
328                                 dbesc($deny_cid),
329                                 dbesc($deny_gid)
330                         );
331                 }
332                 return $r;
333         }
334 }}
335
336
337 /**
338  * Guess image mimetype from filename or from Content-Type header
339  * 
340  * @arg $filename string Image filename
341  * @arg $fromcurl boolean Check Content-Type header from curl request
342  */
343 function guess_image_type($filename, $fromcurl=false) {
344     logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
345         $type = null;
346         if ($fromcurl) {
347                 $a = get_app(); 
348                 $headers=array();
349                 $h = explode("\n",$a->get_curl_headers());
350                 foreach ($h as $l) {
351                         list($k,$v) = array_map("trim", explode(":", trim($l), 2));
352                         $headers[$k] = $v;
353                 }
354                 if (array_key_exists('Content-Type', $headers))
355                         $type = $headers['Content-Type'];
356         }
357         if (is_null($type)){
358                 $ext = pathinfo($filename, PATHINFO_EXTENSION);
359                 $types = Photo::supportedTypes();
360                 $type = "image/jpeg";
361                 foreach ($types as $m=>$e){
362                         if ($ext==$e) $type = $m;
363                 }
364
365         }
366     logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
367         return $type;
368         
369 }
370
371 function import_profile_photo($photo,$uid,$cid) {
372
373         $a = get_app();
374
375         $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
376                 intval($uid),
377                 intval($cid)
378         );
379         if(count($r)) {
380                 $hash = $r[0]['resource-id'];
381         }
382         else {
383                 $hash = photo_new_resource();
384         }
385                 
386         $photo_failure = false;
387
388         $filename = basename($photo);
389         $img_str = fetch_url($photo,true);
390         
391         // guess mimetype from headers or filename
392         $type = guess_image_type($photo,true);
393
394         
395         $img = new Photo($img_str, $type);
396         if($img->is_valid()) {
397
398                 $img->scaleImageSquare(175);
399                                         
400                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
401
402                 if($r === false)
403                         $photo_failure = true;
404
405                 $img->scaleImage(80);
406
407                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
408
409                 if($r === false)
410                         $photo_failure = true;
411
412                 $img->scaleImage(48);
413
414                 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
415
416                 if($r === false)
417                         $photo_failure = true;
418
419                 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
420                 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
421                 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
422         }
423         else
424                 $photo_failure = true;
425
426         if($photo_failure) {
427                 $photo = $a->get_baseurl() . '/images/person-175.jpg';
428                 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
429                 $micro = $a->get_baseurl() . '/images/person-48.jpg';
430         }
431
432         return(array($photo,$thumb,$micro));
433
434 }