3 if(! class_exists("Photo")) {
9 * Put back gd stuff, not everybody have Imagick
19 * supported mimetypes and corresponding file extensions
21 static function supportedTypes() {
22 if(class_exists('Imagick')) {
24 * Imagick::queryFormats won't help us a lot there...
25 * At least, not yet, other parts of friendica uses this array
28 'image/jpeg' => 'jpg',
34 $t['image/jpeg'] ='jpg';
35 if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
41 public function __construct($data, $type=null) {
42 $this->imagick = class_exists('Imagick');
43 $this->types = $this->supportedTypes();
44 if (!array_key_exists($type,$this->types)){
49 if($this->is_imagick()) {
50 $this->image = new Imagick();
51 $this->image->readImageBlob($data);
54 * Setup the image to the format it will be saved to
56 $map = $this->get_FormatsMap();
57 $format = $map[$type];
58 $this->image->setFormat($format);
60 // Always coalesce, if it is not a multi-frame image it won't hurt anyway
61 $this->image = $this->image->coalesceImages();
64 * setup the compression here, so we'll do it only once
66 switch($this->getType()){
68 $quality = get_config('system','png_quality');
69 if((! $quality) || ($quality > 9))
70 $quality = PNG_QUALITY;
72 * From http://www.imagemagick.org/script/command-line-options.php#quality:
74 * 'For the MNG and PNG image formats, the quality value sets
75 * the zlib compression level (quality / 10) and filter-type (quality % 10).
76 * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
77 * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
79 $quality = $quality * 10;
80 $this->image->setCompressionQuality($quality);
83 $quality = get_config('system','jpeg_quality');
84 if((! $quality) || ($quality > 100))
85 $quality = JPEG_QUALITY;
86 $this->image->setCompressionQuality($quality);
90 $this->image = @imagecreatefromstring($data);
91 if($this->image !== FALSE) {
92 $this->width = imagesx($this->image);
93 $this->height = imagesy($this->image);
95 imagealphablending($this->image, false);
96 imagesavealpha($this->image, true);
101 public function __destruct() {
103 if($this->is_imagick()) {
104 $this->image->clear();
105 $this->image->destroy();
108 imagedestroy($this->image);
112 public function is_imagick() {
113 return $this->imagick;
117 * Maps Mime types to Imagick formats
119 public function get_FormatsMap() {
121 'image/jpeg' => 'JPG',
122 'image/png' => 'PNG',
128 public function is_valid() {
129 if($this->is_imagick())
130 return ($this->image !== FALSE);
134 public function getWidth() {
135 if(!$this->is_valid())
138 if($this->is_imagick())
139 return $this->image->getImageWidth();
143 public function getHeight() {
144 if(!$this->is_valid())
147 if($this->is_imagick())
148 return $this->image->getImageHeight();
149 return $this->height;
152 public function getImage() {
153 if(!$this->is_valid())
156 if($this->is_imagick()) {
158 $this->image = $this->image->deconstructImages();
164 public function getType() {
165 if(!$this->is_valid())
171 public function getExt() {
172 if(!$this->is_valid())
175 return $this->types[$this->getType()];
178 public function scaleImage($max) {
179 if(!$this->is_valid())
182 if($this->is_imagick()) {
184 * If it is not animated, there will be only one iteration here,
185 * so don't bother checking
187 // Don't forget to go back to the first frame
188 $this->image->setFirstIterator();
190 $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true);
191 } while ($this->image->nextImage());
195 $width = $this->width;
196 $height = $this->height;
198 $dest_width = $dest_height = 0;
200 if((! $width)|| (! $height))
203 if($width > $max && $height > $max) {
204 if($width > $height) {
206 $dest_height = intval(( $height * $max ) / $width);
209 $dest_width = intval(( $width * $max ) / $height);
214 if( $width > $max ) {
216 $dest_height = intval(( $height * $max ) / $width);
219 if( $height > $max ) {
220 $dest_width = intval(( $width * $max ) / $height);
224 $dest_width = $width;
225 $dest_height = $height;
231 $dest = imagecreatetruecolor( $dest_width, $dest_height );
232 imagealphablending($dest, false);
233 imagesavealpha($dest, true);
234 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
235 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
237 imagedestroy($this->image);
238 $this->image = $dest;
239 $this->width = imagesx($this->image);
240 $this->height = imagesy($this->image);
243 public function rotate($degrees) {
244 if(!$this->is_valid())
247 if($this->is_imagick()) {
248 $this->image->setFirstIterator();
250 $this->image->rotateImage(new ImagickPixel(), $degrees);
251 } while ($this->image->nextImage());
255 $this->image = imagerotate($this->image,$degrees,0);
256 $this->width = imagesx($this->image);
257 $this->height = imagesy($this->image);
260 public function flip($horiz = true, $vert = false) {
261 if(!$this->is_valid())
264 if($this->is_imagick()) {
265 $this->image->setFirstIterator();
267 if($horiz) $this->image->flipImage();
268 if($vert) $this->image->flopImage();
269 } while ($this->image->nextImage());
273 $w = imagesx($this->image);
274 $h = imagesy($this->image);
275 $flipped = imagecreate($w, $h);
277 for ($x = 0; $x < $w; $x++) {
278 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
282 for ($y = 0; $y < $h; $y++) {
283 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
286 $this->image = $flipped;
289 public function orient($filename) {
290 // based off comment on http://php.net/manual/en/function.imagerotate.php
292 if(!$this->is_valid())
295 if( (! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg') )
298 $exif = exif_read_data($filename);
299 $ort = $exif['Orientation'];
306 case 2: // horizontal flip
310 case 3: // 180 rotate left
314 case 4: // vertical flip
315 $this->flip(false, true);
318 case 5: // vertical flip + 90 rotate right
319 $this->flip(false, true);
323 case 6: // 90 rotate right
327 case 7: // horizontal flip + 90 rotate right
332 case 8: // 90 rotate left
340 public function scaleImageUp($min) {
341 if(!$this->is_valid())
344 if($this->is_imagick())
345 return $this->scaleImage($min);
347 $width = $this->width;
348 $height = $this->height;
350 $dest_width = $dest_height = 0;
352 if((! $width)|| (! $height))
355 if($width < $min && $height < $min) {
356 if($width > $height) {
358 $dest_height = intval(( $height * $min ) / $width);
361 $dest_width = intval(( $width * $min ) / $height);
366 if( $width < $min ) {
368 $dest_height = intval(( $height * $min ) / $width);
371 if( $height < $min ) {
372 $dest_width = intval(( $width * $min ) / $height);
376 $dest_width = $width;
377 $dest_height = $height;
383 $dest = imagecreatetruecolor( $dest_width, $dest_height );
384 imagealphablending($dest, false);
385 imagesavealpha($dest, true);
386 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
387 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
389 imagedestroy($this->image);
390 $this->image = $dest;
391 $this->width = imagesx($this->image);
392 $this->height = imagesy($this->image);
397 public function scaleImageSquare($dim) {
398 if(!$this->is_valid())
401 if($this->is_imagick()) {
402 $this->image->setFirstIterator();
404 $this->image->resizeImage($dim, $dim, imagick::FILTER_LANCZOS, 1, false);
405 } while ($this->image->nextImage());
409 $dest = imagecreatetruecolor( $dim, $dim );
410 imagealphablending($dest, false);
411 imagesavealpha($dest, true);
412 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
413 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
415 imagedestroy($this->image);
416 $this->image = $dest;
417 $this->width = imagesx($this->image);
418 $this->height = imagesy($this->image);
422 public function cropImage($max,$x,$y,$w,$h) {
423 if(!$this->is_valid())
426 if($this->is_imagick()) {
427 $this->image->setFirstIterator();
429 $this->image->cropImage($w, $h, $x, $y);
431 * We need to remove the canva,
432 * or the image is not resized to the crop:
433 * http://php.net/manual/en/imagick.cropimage.php#97232
435 $this->image->setImagePage(0, 0, 0, 0);
436 } while ($this->image->nextImage());
437 return $this->scaleImage($max);
440 $dest = imagecreatetruecolor( $max, $max );
441 imagealphablending($dest, false);
442 imagesavealpha($dest, true);
443 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
444 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
446 imagedestroy($this->image);
447 $this->image = $dest;
448 $this->width = imagesx($this->image);
449 $this->height = imagesy($this->image);
452 public function saveImage($path) {
453 if(!$this->is_valid())
456 $string = $this->imageString();
457 file_put_contents($path, $string);
460 public function imageString() {
461 if(!$this->is_valid())
464 if($this->is_imagick()) {
466 $this->image = $this->image->deconstructImages();
467 $string = $this->image->getImagesBlob();
475 switch($this->getType()){
477 $quality = get_config('system','png_quality');
478 if((! $quality) || ($quality > 9))
479 $quality = PNG_QUALITY;
480 imagepng($this->image,NULL, $quality);
483 $quality = get_config('system','jpeg_quality');
484 if((! $quality) || ($quality > 100))
485 $quality = JPEG_QUALITY;
486 imagejpeg($this->image,NULL,$quality);
488 $string = ob_get_contents();
496 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
498 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
502 $guid = $r[0]['guid'];
506 $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
513 $r = q("UPDATE `photo`
517 `resource-id` = '%s',
532 where id = %d limit 1",
538 dbesc(datetime_convert()),
539 dbesc(datetime_convert()),
540 dbesc(basename($filename)),
541 dbesc($this->getType()),
543 intval($this->getHeight()),
544 intval($this->getWidth()),
545 dbesc($this->imageString()),
556 $r = q("INSERT INTO `photo`
557 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
558 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
563 dbesc(datetime_convert()),
564 dbesc(datetime_convert()),
565 dbesc(basename($filename)),
566 dbesc($this->getType()),
568 intval($this->getHeight()),
569 intval($this->getWidth()),
570 dbesc($this->imageString()),
585 * Guess image mimetype from filename or from Content-Type header
587 * @arg $filename string Image filename
588 * @arg $fromcurl boolean Check Content-Type header from curl request
590 function guess_image_type($filename, $fromcurl=false) {
591 logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
596 $h = explode("\n",$a->get_curl_headers());
598 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
601 if (array_key_exists('Content-Type', $headers))
602 $type = $headers['Content-Type'];
605 // Guessing from extension? Isn't that... dangerous?
606 if(class_exists('Imagick')) {
608 * Well, this not much better,
609 * but at least it comes from the data inside the image,
610 * we won't be tricked by a manipulated extension
612 $image = new Imagick($filename);
613 $type = $image->getImageMimeType();
615 $ext = pathinfo($filename, PATHINFO_EXTENSION);
616 $types = Photo::supportedTypes();
617 $type = "image/jpeg";
618 foreach ($types as $m=>$e){
619 if ($ext==$e) $type = $m;
623 logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
628 function import_profile_photo($photo,$uid,$cid) {
632 $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
637 $hash = $r[0]['resource-id'];
640 $hash = photo_new_resource();
643 $photo_failure = false;
645 $filename = basename($photo);
646 $img_str = fetch_url($photo,true);
648 $type = guess_image_type($photo,true);
649 $img = new Photo($img_str, $type);
650 if($img->is_valid()) {
652 $img->scaleImageSquare(175);
654 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
657 $photo_failure = true;
659 $img->scaleImage(80);
661 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
664 $photo_failure = true;
666 $img->scaleImage(48);
668 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
671 $photo_failure = true;
673 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
674 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
675 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
678 $photo_failure = true;
681 $photo = $a->get_baseurl() . '/images/person-175.jpg';
682 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
683 $micro = $a->get_baseurl() . '/images/person-48.jpg';
686 return(array($photo,$thumb,$micro));