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 return $this->image->getImageMimeType();
132 public function getExt() {
133 if(!$this->is_valid())
136 if($this->is_imagick())
138 return $this->types[$this->type];
141 public function scaleImage($max) {
142 if(!$this->is_valid())
145 if($this->is_imagick()) {
147 * If it is not animated, there will be only one iteration here,
148 * so don't bother checking
150 // Don't forget to go back to the first frame for any further operation
151 $this->image->setFirstIterator();
153 $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true);
154 } while ($this->image->nextImage());
158 $width = $this->width;
159 $height = $this->height;
161 $dest_width = $dest_height = 0;
163 if((! $width)|| (! $height))
166 if($width > $max && $height > $max) {
167 if($width > $height) {
169 $dest_height = intval(( $height * $max ) / $width);
172 $dest_width = intval(( $width * $max ) / $height);
177 if( $width > $max ) {
179 $dest_height = intval(( $height * $max ) / $width);
182 if( $height > $max ) {
183 $dest_width = intval(( $width * $max ) / $height);
187 $dest_width = $width;
188 $dest_height = $height;
194 $dest = imagecreatetruecolor( $dest_width, $dest_height );
195 imagealphablending($dest, false);
196 imagesavealpha($dest, true);
197 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
198 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
200 imagedestroy($this->image);
201 $this->image = $dest;
202 $this->width = imagesx($this->image);
203 $this->height = imagesy($this->image);
206 public function rotate($degrees) {
207 if(!$this->is_valid())
210 if($this->is_imagick()) {
211 $this->image->setFirstIterator();
213 $this->image->rotateImage(new ImagickPixel(), $degrees);
214 } while ($this->image->nextImage());
218 $this->image = imagerotate($this->image,$degrees,0);
219 $this->width = imagesx($this->image);
220 $this->height = imagesy($this->image);
223 public function flip($horiz = true, $vert = false) {
224 if(!$this->is_valid())
227 if($this->is_imagick()) {
228 $this->image->setFirstIterator();
230 if($horiz) $this->image->flipImage();
231 if($vert) $this->image->flopImage();
232 } while ($this->image->nextImage());
236 $w = imagesx($this->image);
237 $h = imagesy($this->image);
238 $flipped = imagecreate($w, $h);
240 for ($x = 0; $x < $w; $x++) {
241 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
245 for ($y = 0; $y < $h; $y++) {
246 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
249 $this->image = $flipped;
252 public function orient($filename) {
253 // based off comment on http://php.net/manual/en/function.imagerotate.php
255 if(!$this->is_valid())
258 if( (! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg') )
261 $exif = exif_read_data($filename);
262 $ort = $exif['Orientation'];
269 case 2: // horizontal flip
273 case 3: // 180 rotate left
277 case 4: // vertical flip
278 $this->flip(false, true);
281 case 5: // vertical flip + 90 rotate right
282 $this->flip(false, true);
286 case 6: // 90 rotate right
290 case 7: // horizontal flip + 90 rotate right
295 case 8: // 90 rotate left
303 public function scaleImageUp($min) {
304 if(!$this->is_valid())
307 if($this->is_imagick())
308 return $this->scaleImage($min);
310 $width = $this->width;
311 $height = $this->height;
313 $dest_width = $dest_height = 0;
315 if((! $width)|| (! $height))
318 if($width < $min && $height < $min) {
319 if($width > $height) {
321 $dest_height = intval(( $height * $min ) / $width);
324 $dest_width = intval(( $width * $min ) / $height);
329 if( $width < $min ) {
331 $dest_height = intval(( $height * $min ) / $width);
334 if( $height < $min ) {
335 $dest_width = intval(( $width * $min ) / $height);
339 $dest_width = $width;
340 $dest_height = $height;
346 $dest = imagecreatetruecolor( $dest_width, $dest_height );
347 imagealphablending($dest, false);
348 imagesavealpha($dest, true);
349 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
350 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
352 imagedestroy($this->image);
353 $this->image = $dest;
354 $this->width = imagesx($this->image);
355 $this->height = imagesy($this->image);
360 public function scaleImageSquare($dim) {
361 if(!$this->is_valid())
364 if($this->is_imagick()) {
365 $this->image->setFirstIterator();
367 $this->image->resizeImage($dim, $dim, imagick::FILTER_LANCZOS, 1, false);
368 } while ($this->image->nextImage());
372 $dest = imagecreatetruecolor( $dim, $dim );
373 imagealphablending($dest, false);
374 imagesavealpha($dest, true);
375 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
376 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
378 imagedestroy($this->image);
379 $this->image = $dest;
380 $this->width = imagesx($this->image);
381 $this->height = imagesy($this->image);
385 public function cropImage($max,$x,$y,$w,$h) {
386 if(!$this->is_valid())
389 if($this->is_imagick()) {
390 $this->image->setFirstIterator();
392 $this->image->cropImage($w, $h, $x, $y);
394 * We need to remove the canva,
395 * or the image is not resized to the crop:
396 * http://php.net/manual/en/imagick.cropimage.php#97232
398 $this->image->setImagePage(0, 0, 0, 0);
399 } while ($this->image->nextImage());
400 return $this->scaleImage($max);
403 $dest = imagecreatetruecolor( $max, $max );
404 imagealphablending($dest, false);
405 imagesavealpha($dest, true);
406 if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
407 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
409 imagedestroy($this->image);
410 $this->image = $dest;
411 $this->width = imagesx($this->image);
412 $this->height = imagesy($this->image);
415 public function saveImage($path) {
416 if(!$this->is_valid())
419 $string = $this->imageString();
420 file_put_contents($path, $string);
423 public function imageString() {
424 if(!$this->is_valid())
431 * we should do the conversion/compression at the initialisation i think
432 * This method may be called several times,
433 * and there is no need to do that more than once
436 if(!$this->is_imagick()) ob_start();
438 switch($this->getType()){
440 $quality = get_config('system','png_quality');
441 if((! $quality) || ($quality > 9))
442 $quality = PNG_QUALITY;
443 if($this->is_imagick()) {
445 * From http://www.imagemagick.org/script/command-line-options.php#quality:
447 * 'For the MNG and PNG image formats, the quality value sets
448 * the zlib compression level (quality / 10) and filter-type (quality % 10).
449 * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
450 * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
452 $quality = $quality * 10;
453 } else imagepng($this->image,NULL, $quality);
456 // We change nothing here, do we?
459 // Convert to jpeg by default
460 $quality = get_config('system','jpeg_quality');
461 if((! $quality) || ($quality > 100))
462 $quality = JPEG_QUALITY;
463 if($this->is_imagick())
464 $this->image->setImageFormat('jpeg');
465 else imagejpeg($this->image,NULL,$quality);
468 if($this->is_imagick()) {
469 if($quality !== FALSE) {
470 // Do we need to iterate for animations?
471 $this->image->setImageCompressionQuality($quality);
472 $this->image->stripImage();
475 $string = $this->image->getImagesBlob();
477 $string = ob_get_contents();
486 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
488 $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
492 $guid = $r[0]['guid'];
496 $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
503 $r = q("UPDATE `photo`
507 `resource-id` = '%s',
522 where id = %d limit 1",
528 dbesc(datetime_convert()),
529 dbesc(datetime_convert()),
530 dbesc(basename($filename)),
531 dbesc($this->getType()),
533 intval($this->getHeight()),
534 intval($this->getWidth()),
535 dbesc($this->imageString()),
546 $r = q("INSERT INTO `photo`
547 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
548 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
553 dbesc(datetime_convert()),
554 dbesc(datetime_convert()),
555 dbesc(basename($filename)),
556 dbesc($this->getType()),
558 intval($this->getHeight()),
559 intval($this->getWidth()),
560 dbesc($this->imageString()),
575 * Guess image mimetype from filename or from Content-Type header
577 * @arg $filename string Image filename
578 * @arg $fromcurl boolean Check Content-Type header from curl request
580 function guess_image_type($filename, $fromcurl=false) {
581 logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
586 $h = explode("\n",$a->get_curl_headers());
588 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
591 if (array_key_exists('Content-Type', $headers))
592 $type = $headers['Content-Type'];
595 // Guessing from extension? Isn't that... dangerous?
596 if($this->is_imagick()) {
598 * Well, this not much better,
599 * but at least it comes from the data inside the image,
600 * we won't be tricked by a manipulated extension
602 $image = new Imagick($filename);
603 $type = 'image/'. strtolower($image->getImageFormat());
605 $ext = pathinfo($filename, PATHINFO_EXTENSION);
606 $types = Photo::supportedTypes();
607 $type = "image/jpeg";
608 foreach ($types as $m=>$e){
609 if ($ext==$e) $type = $m;
613 logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
618 function import_profile_photo($photo,$uid,$cid) {
622 $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
627 $hash = $r[0]['resource-id'];
630 $hash = photo_new_resource();
633 $photo_failure = false;
635 $filename = basename($photo);
636 $img_str = fetch_url($photo,true);
638 if($this->is_imagick()) $type = null;
640 // guess mimetype from headers or filename
641 $type = guess_image_type($photo,true);
643 $img = new Photo($img_str, $type);
644 if($img->is_valid()) {
646 $img->scaleImageSquare(175);
648 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
651 $photo_failure = true;
653 $img->scaleImage(80);
655 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
658 $photo_failure = true;
660 $img->scaleImage(48);
662 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
665 $photo_failure = true;
667 $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
668 $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
669 $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
672 $photo_failure = true;
675 $photo = $a->get_baseurl() . '/images/person-175.jpg';
676 $thumb = $a->get_baseurl() . '/images/person-80.jpg';
677 $micro = $a->get_baseurl() . '/images/person-48.jpg';
680 return(array($photo,$thumb,$micro));