diff options
author | friendica <info@friendica.com> | 2012-07-22 17:25:26 -0700 |
---|---|---|
committer | friendica <info@friendica.com> | 2012-07-22 17:25:26 -0700 |
commit | 4e4395b00fa8d7b06b11bdbb95da86ae15b1eb71 (patch) | |
tree | f547f3fae54e81e0d1ed3dfa3c92531f60f0b175 /include/Photo.php | |
parent | e9fa57966675fcfe5e267ebc21faa52b2cf6d983 (diff) | |
download | volse-hubzilla-4e4395b00fa8d7b06b11bdbb95da86ae15b1eb71.tar.gz volse-hubzilla-4e4395b00fa8d7b06b11bdbb95da86ae15b1eb71.tar.bz2 volse-hubzilla-4e4395b00fa8d7b06b11bdbb95da86ae15b1eb71.zip |
more registration functions plus ability to over-ride individual strings plus upstream merges plus rev update
Diffstat (limited to 'include/Photo.php')
-rw-r--r-- | include/Photo.php | 137 |
1 files changed, 71 insertions, 66 deletions
diff --git a/include/Photo.php b/include/Photo.php index 2733f5ed5..1044155b5 100644 --- a/include/Photo.php +++ b/include/Photo.php @@ -4,7 +4,6 @@ if(! class_exists("Photo")) { class Photo { private $image; - private $ext; /** * Put back gd stuff, not everybody have Imagick @@ -41,22 +40,53 @@ class Photo { public function __construct($data, $type=null) { $this->imagick = class_exists('Imagick'); + $this->types = $this->supportedTypes(); + if (!array_key_exists($type,$this->types)){ + $type='image/jpeg'; + } + $this->type = $type; if($this->is_imagick()) { $this->image = new Imagick(); $this->image->readImageBlob($data); - // If it is a gif, it may be animated, get it ready for any future operations - if($this->image->getFormat() !== "GIF") $this->image = $this->image->coalesceImages(); + /** + * Setup the image to the format it will be saved to + */ + $map = $this->get_FormatsMap(); + $format = $map[$type]; + $this->image->setFormat($format); - $this->ext = strtolower($this->image->getImageFormat()); - } else { - $this->types = $this->supportedTypes(); - if (!array_key_exists($type,$this->types)){ - $type='image/jpeg'; + // Always coalesce, if it is not a multi-frame image it won't hurt anyway + $this->image = $this->image->coalesceImages(); + + /** + * setup the compression here, so we'll do it only once + */ + switch($this->getType()){ + case "image/png": + $quality = get_config('system','png_quality'); + if((! $quality) || ($quality > 9)) + $quality = PNG_QUALITY; + /** + * From http://www.imagemagick.org/script/command-line-options.php#quality: + * + * 'For the MNG and PNG image formats, the quality value sets + * the zlib compression level (quality / 10) and filter-type (quality % 10). + * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering, + * unless the image has a color map, in which case it means compression level 7 with no PNG filtering' + */ + $quality = $quality * 10; + $this->image->setCompressionQuality($quality); + break; + case "image/jpeg": + $quality = get_config('system','jpeg_quality'); + if((! $quality) || ($quality > 100)) + $quality = JPEG_QUALITY; + $this->image->setCompressionQuality($quality); } + } else { $this->valid = false; - $this->type = $type; $this->image = @imagecreatefromstring($data); if($this->image !== FALSE) { $this->width = imagesx($this->image); @@ -83,6 +113,18 @@ class Photo { return $this->imagick; } + /** + * Maps Mime types to Imagick formats + */ + public function get_FormatsMap() { + $m = array( + 'image/jpeg' => 'JPG', + 'image/png' => 'PNG', + 'image/gif' => 'GIF' + ); + return $m; + } + public function is_valid() { if($this->is_imagick()) return ($this->image !== FALSE); @@ -111,8 +153,8 @@ class Photo { if(!$this->is_valid()) return FALSE; - /* Clean it */ if($this->is_imagick()) { + /* Clean it */ $this->image = $this->image->deconstructImages(); return $this->image; } @@ -123,10 +165,6 @@ class Photo { if(!$this->is_valid()) return FALSE; - if($this->is_imagick()) { - // This should do the trick (see supportedTypes above) - return 'image/'. $this->getExt(); - } return $this->type; } @@ -134,9 +172,7 @@ class Photo { if(!$this->is_valid()) return FALSE; - if($this->is_imagick()) - return $this->ext; - return $this->types[$this->type]; + return $this->types[$this->getType()]; } public function scaleImage($max) { @@ -148,7 +184,7 @@ class Photo { * If it is not animated, there will be only one iteration here, * so don't bother checking */ - // Don't forget to go back to the first frame for any further operation + // Don't forget to go back to the first frame $this->image->setFirstIterator(); do { $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true); @@ -365,7 +401,7 @@ class Photo { if($this->is_imagick()) { $this->image->setFirstIterator(); do { - $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, false); + $this->image->resizeImage($dim, $dim, imagick::FILTER_LANCZOS, 1, false); } while ($this->image->nextImage()); return; } @@ -425,59 +461,32 @@ class Photo { if(!$this->is_valid()) return FALSE; - $quality = FALSE; + if($this->is_imagick()) { + /* Clean it */ + $this->image = $this->image->deconstructImages(); + $string = $this->image->getImagesBlob(); + return $string; + } - /** - * Hmmm, for Imagick - * we should do the conversion/compression at the initialisation i think - * This method may be called several times, - * and there is no need to do that more than once - */ + $quality = FALSE; - if(!$this->is_imagick()) ob_start(); + ob_start(); switch($this->getType()){ case "image/png": $quality = get_config('system','png_quality'); if((! $quality) || ($quality > 9)) $quality = PNG_QUALITY; - if($this->is_imagick()) { - /** - * From http://www.imagemagick.org/script/command-line-options.php#quality: - * - * 'For the MNG and PNG image formats, the quality value sets - * the zlib compression level (quality / 10) and filter-type (quality % 10). - * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering, - * unless the image has a color map, in which case it means compression level 7 with no PNG filtering' - */ - $quality = $quality * 10; - } else imagepng($this->image,NULL, $quality); - break; - case "image/gif": - // We change nothing here, do we? + imagepng($this->image,NULL, $quality); break; - default: - // Convert to jpeg by default + case "image/jpeg": $quality = get_config('system','jpeg_quality'); if((! $quality) || ($quality > 100)) $quality = JPEG_QUALITY; - if($this->is_imagick()) - $this->image->setImageFormat('jpeg'); - else imagejpeg($this->image,NULL,$quality); - } - - if($this->is_imagick()) { - if($quality !== FALSE) { - // Do we need to iterate for animations? - $this->image->setImageCompressionQuality($quality); - $this->image->stripImage(); - } - - $string = $this->image->getImagesBlob(); - } else { - $string = ob_get_contents(); - ob_end_clean(); + imagejpeg($this->image,NULL,$quality); } + $string = ob_get_contents(); + ob_end_clean(); return $string; } @@ -583,14 +592,14 @@ function guess_image_type($filename, $fromcurl=false) { } if (is_null($type)){ // Guessing from extension? Isn't that... dangerous? - if($this->is_imagick()) { + if(class_exists('Imagick')) { /** * Well, this not much better, * but at least it comes from the data inside the image, * we won't be tricked by a manipulated extension */ $image = new Imagick($filename); - $type = 'image/'. strtolower($image->getImageFormat()); + $type = $image->getImageMimeType(); } else { $ext = pathinfo($filename, PATHINFO_EXTENSION); $types = Photo::supportedTypes(); @@ -625,11 +634,7 @@ function import_profile_photo($photo,$uid,$cid) { $filename = basename($photo); $img_str = fetch_url($photo,true); - if($this->is_imagick()) $type = null; - else { - // guess mimetype from headers or filename - $type = guess_image_type($photo,true); - } + $type = guess_image_type($photo,true); $img = new Photo($img_str, $type); if($img->is_valid()) { |