3 if(! class_exists("Photo")) {
10 * Put back gd stuff, not everybody have Imagick
20 * supported mimetypes and corresponding file extensions
22 static function supportedTypes() {
23 if(class_exists('Imagick')) {
25 * Imagick::queryFormats won't help us a lot there...
26 * At least, not yet, other parts of friendica uses this array
29 'image/jpeg' => 'jpg',
35 $t['image/jpeg'] ='jpg';
36 if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
42 public function __construct($data, $type=null) {
43 $this->imagick = class_exists('Imagick');
45 if($this->is_imagick()) {
46 $this->image = new Imagick();
47 $this->image->readImageBlob($data);
49 // If it is a gif, it may be animated, get it ready for any future operations
50 if($this->image->getFormat() !== "GIF") $this->image = $this->image->coalesceImages();
52 $this->ext = strtolower($this->image->getImageFormat());
54 $this->types = $this->supportedTypes();
55 if (!array_key_exists($type,$this->types)){
60 $this->image = @imagecreatefromstring($data);
61 if($this->image !== FALSE) {
62 $this->width = imagesx($this->image);
63 $this->height = imagesy($this->image);
65 imagealphablending($this->image, false);
66 imagesavealpha($this->image, true);
71 public function __destruct() {
73 if($this->is_imagick()) {
74 $this->image->clear();
75 $this->image->destroy();
78 imagedestroy($this->image);
82 public function is_imagick() {
83 return $this->imagick;
86 public function is_valid() {
87 if($this->is_imagick())
88 return ($this->image !== FALSE);
92 public function getWidth() {
93 if(!$this->is_valid())
96 if($this->is_imagick())
97 return $this->image->getImageWidth();
101 public function getHeight() {
102 if(!$this->is_valid())
105 if($this->is_imagick())
106 return $this->image->getImageHeight();
107 return $this->height;
110 public function getImage() {
111 if(!$this->is_valid())
115 if($this->is_imagick()) {
116 $this->image = $this->image->deconstructImages();
122 public function getType() {
123 if(!$this->is_valid())
126 if($this->is_imagick()) {
127 // This should do the trick (see supportedTypes above)
128 return 'image/'. $this->getExt();
133 public function getExt() {
134 if(!$this->is_valid())
137 if($this->is_imagick())
139 return $this->types[$this->type];
142 public function scaleImage($max) {
143 if(!$this->is_valid())
146 if($this->is_imagick()) {
148 * If it is not animated, there will be only one iteration here,
149 * so don't bother checking
151 // Don't forget to go back to the first frame for any further operation
152 $this->image->setFirstIterator();
154 $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true);
155 } while ($this->image->nextImage());
159 $width = $this->width;
160 $height = $this->height;
162 $dest_width = $dest_height = 0;
164 if((! $width)|| (! $height))
167 if($width > $max && $height > $max) {
168 if($width > $height) {
170 $dest_height = intval(( $height * $max ) / $width);
173 $dest_width = intval(( $width * $max ) / $height);
178 if( $width > $max ) {
180 $dest_height = intval(( $height * $max ) / $width);
183 if( $height > $max ) {
184 $dest_width = intval(( $width * $max ) / $height);
188 $dest_width = $width;
189 $dest_height = $height;
195 $dest = imagecreatetruecolor( $dest_width, $dest_height );
196 imagealphablending($dest, false);
197 imagesavealpha($dest, true);
198 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
199 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
201 imagedestroy($this->image);
202 $this->image = $dest;
203 $this->width = imagesx($this->image);
204 $this->height = imagesy($this->image);
207 public function rotate($degrees) {
208 if(!$this->is_valid())
211 if($this->is_imagick()) {
212 $this->image->setFirstIterator();
214 $this->image->rotateImage(new ImagickPixel(), $degrees);
215 } while ($this->image->nextImage());
219 $this->image = imagerotate($this->image,$degrees,0);
220 $this->width = imagesx($this->image);
221 $this->height = imagesy($this->image);
224 public function flip($horiz = true, $vert = false) {
225 if(!$this->is_valid())
228 if($this->is_imagick()) {
229 $this->image->setFirstIterator();
231 if($horiz) $this->image->flipImage();
232 if($vert) $this->image->flopImage();
233 } while ($this->image->nextImage());
237 $w = imagesx($this->image);
238 $h = imagesy($this->image);
239 $flipped = imagecreate($w, $h);
241 for ($x = 0; $x < $w; $x++) {
242 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
246 for ($y = 0; $y < $h; $y++) {
247 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
250 $this->image = $flipped;
253 public function orient($filename) {
254 // based off comment on http://php.net/manual/en/function.imagerotate.php
256 if(!$this->is_valid())
259 if( (! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg') )
262 $exif = exif_read_data($filename);
263 $ort = $exif['Orientation'];
270 case 2: // horizontal flip
274 case 3: // 180 rotate left
278 case 4: // vertical flip
279 $this->flip(false, true);
282 case 5: // vertical flip + 90 rotate right
283 $this->flip(false, true);
287 case 6: // 90 rotate right
291 case 7: // horizontal flip + 90 rotate right
296 case 8: // 90 rotate left
304 public function scaleImageUp($min) {
305 if(!$this->is_valid())
308 if($this->is_imagick())
309 return $this->scaleImage($min);
311 $width = $this->width;
312 $height = $this->height;
314 $dest_width = $dest_height = 0;
316 if((! $width)|| (! $height))
319 if($width < $min && $height < $min) {
320 if($width > $height) {
322 $dest_height = intval(( $height * $min ) / $width);
325 $dest_width = intval(( $width * $min ) / $height);
330 if( $width < $min ) {
332 $dest_height = intval(( $height * $min ) / $width);
335 if( $height < $min ) {
336 $dest_width = intval(( $width * $min ) / $height);
340 $dest_width = $width;
341 $dest_height = $height;
347 $dest = imagecreatetruecolor( $dest_width, $dest_height );
348 imagealphablending($dest, false);
349 imagesavealpha($dest, true);
350 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
351 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
353 imagedestroy($this->image);
354 $this->image = $dest;
355 $this->width = imagesx($this->image);
356 $this->height = imagesy($this->image);
361 public function scaleImageSquare($dim) {
362 if(!$this->is_valid())
365 if($this->is_imagick()) {
366 $this->image->setFirstIterator();
368 $this->image->resizeImage($dim, $dim, imagick::FILTER_LANCZOS, 1, false);
369 } while ($this->image->nextImage());
373 $dest = imagecreatetruecolor( $dim, $dim );
374 imagealphablending($dest, false);
375 imagesavealpha($dest, true);
376 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
377 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
379 imagedestroy($this->image);
380 $this->image = $dest;
381 $this->width = imagesx($this->image);
382 $this->height = imagesy($this->image);
386 public function cropImage($max,$x,$y,$w,$h) {
387 if(!$this->is_valid())
390 if($this->is_imagick()) {
391 $this->image->setFirstIterator();
393 $this->image->cropImage($w, $h, $x, $y);
395 * We need to remove the canva,
396 * or the image is not resized to the crop:
397 * http://php.net/manual/en/imagick.cropimage.php#97232
399 $this->image->setImagePage(0, 0, 0, 0);
400 } while ($this->image->nextImage());
401 return $this->scaleImage($max);
404 $dest = imagecreatetruecolor( $max, $max );
405 imagealphablending($dest, false);
406 imagesavealpha($dest, true);
407 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
408 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
410 imagedestroy($this->image);
411 $this->image = $dest;
412 $this->width = imagesx($this->image);
413 $this->height = imagesy($this->image);
416 public function saveImage($path) {
417 if(!$this->is_valid())
420 $string = $this->imageString();
421 file_put_contents($path, $string);
424 public function imageString() {
425 if(!$this->is_valid())
432 * we should do the conversion/compression at the initialisation i think
433 * This method may be called several times,
434 * and there is no need to do that more than once
437 if(!$this->is_imagick()) ob_start();
439 switch($this->getType()){
441 $quality = get_config('system','png_quality');
442 if((! $quality) || ($quality > 9))
443 $quality = PNG_QUALITY;
444 if($this->is_imagick()) {
446 * From http://www.imagemagick.org/script/command-line-options.php#quality:
448 * 'For the MNG and PNG image formats, the quality value sets
449 * the zlib compression level (quality / 10) and filter-type (quality % 10).
450 * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
451 * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
453 $quality = $quality * 10;
454 } else imagepng($this->image,NULL, $quality);
457 // We change nothing here, do we?
460 // Convert to jpeg by default
461 $quality = get_config('system','jpeg_quality');
462 if((! $quality) || ($quality > 100))
463 $quality = JPEG_QUALITY;
464 if($this->is_imagick())
465 $this->image->setImageFormat('jpeg');
466 else imagejpeg($this->image,NULL,$quality);
469 if($this->is_imagick()) {
470 if($quality !== FALSE) {
471 // Do we need to iterate for animations?
472 $this->image->setImageCompressionQuality($quality);
473 $this->image->stripImage();
476 $string = $this->image->getImagesBlob();
478 $string = ob_get_contents();
487 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
489 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
493 $guid = $r[0]['guid'];
497 $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
504 $r = q("UPDATE `photo`
508 `resource-id` = '%s',
523 where id = %d limit 1",
529 dbesc(datetime_convert()),
530 dbesc(datetime_convert()),
531 dbesc(basename($filename)),
532 dbesc($this->getType()),
534 intval($this->getHeight()),
535 intval($this->getWidth()),
536 dbesc($this->imageString()),
547 $r = q("INSERT INTO `photo`
548 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
549 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
554 dbesc(datetime_convert()),
555 dbesc(datetime_convert()),
556 dbesc(basename($filename)),
557 dbesc($this->getType()),
559 intval($this->getHeight()),
560 intval($this->getWidth()),
561 dbesc($this->imageString()),
576 * Guess image mimetype from filename or from Content-Type header
578 * @arg $filename string Image filename
579 * @arg $fromcurl boolean Check Content-Type header from curl request
581 function guess_image_type($filename, $fromcurl=false) {
582 logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
587 $h = explode("\n",$a->get_curl_headers());
589 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
592 if (array_key_exists('Content-Type', $headers))
593 $type = $headers['Content-Type'];
596 // Guessing from extension? Isn't that... dangerous?
597 if($this->is_imagick()) {
599 * Well, this not much better,
600 * but at least it comes from the data inside the image,
601 * we won't be tricked by a manipulated extension
603 $image = new Imagick($filename);
604 $type = 'image/'. strtolower($image->getImageFormat());
606 $ext = pathinfo($filename, PATHINFO_EXTENSION);
607 $types = Photo::supportedTypes();
608 $type = "image/jpeg";
609 foreach ($types as $m=>$e){
610 if ($ext==$e) $type = $m;
614 logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
619 function import_profile_photo($photo,$uid,$cid) {
623 $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
628 $hash = $r[0]['resource-id'];
631 $hash = photo_new_resource();
634 $photo_failure = false;
636 $filename = basename($photo);
637 $img_str = fetch_url($photo,true);
639 if($this->is_imagick()) $type = null;
641 // guess mimetype from headers or filename
642 $type = guess_image_type($photo,true);
644 $img = new Photo($img_str, $type);
645 if($img->is_valid()) {
647 $img->scaleImageSquare(175);
649 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
652 $photo_failure = true;
654 $img->scaleImage(80);
656 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
659 $photo_failure = true;
661 $img->scaleImage(48);
663 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
666 $photo_failure = true;
668 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
669 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
670 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
673 $photo_failure = true;
676 $photo = $a->get_baseurl() . '/images/person-175.jpg';
677 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
678 $micro = $a->get_baseurl() . '/images/person-48.jpg';
681 return(array($photo,$thumb,$micro));