This commit is contained in:
friendica 2012-07-22 15:40:15 -07:00
commit 96c47a6577

View file

@ -4,7 +4,6 @@ if(! class_exists("Photo")) {
class Photo { class Photo {
private $image; private $image;
private $ext;
/** /**
* Put back gd stuff, not everybody have Imagick * Put back gd stuff, not everybody have Imagick
@ -41,22 +40,53 @@ class Photo {
public function __construct($data, $type=null) { public function __construct($data, $type=null) {
$this->imagick = class_exists('Imagick'); $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()) { if($this->is_imagick()) {
$this->image = new Imagick(); $this->image = new Imagick();
$this->image->readImageBlob($data); $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()); // Always coalesce, if it is not a multi-frame image it won't hurt anyway
} else { $this->image = $this->image->coalesceImages();
$this->types = $this->supportedTypes();
if (!array_key_exists($type,$this->types)){ /**
$type='image/jpeg'; * 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->valid = false;
$this->type = $type;
$this->image = @imagecreatefromstring($data); $this->image = @imagecreatefromstring($data);
if($this->image !== FALSE) { if($this->image !== FALSE) {
$this->width = imagesx($this->image); $this->width = imagesx($this->image);
@ -83,6 +113,18 @@ class Photo {
return $this->imagick; 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() { public function is_valid() {
if($this->is_imagick()) if($this->is_imagick())
return ($this->image !== FALSE); return ($this->image !== FALSE);
@ -111,8 +153,8 @@ class Photo {
if(!$this->is_valid()) if(!$this->is_valid())
return FALSE; return FALSE;
/* Clean it */
if($this->is_imagick()) { if($this->is_imagick()) {
/* Clean it */
$this->image = $this->image->deconstructImages(); $this->image = $this->image->deconstructImages();
return $this->image; return $this->image;
} }
@ -123,10 +165,6 @@ class Photo {
if(!$this->is_valid()) if(!$this->is_valid())
return FALSE; return FALSE;
if($this->is_imagick()) {
// This should do the trick (see supportedTypes above)
return 'image/'. $this->getExt();
}
return $this->type; return $this->type;
} }
@ -134,9 +172,7 @@ class Photo {
if(!$this->is_valid()) if(!$this->is_valid())
return FALSE; return FALSE;
if($this->is_imagick()) return $this->types[$this->getType()];
return $this->ext;
return $this->types[$this->type];
} }
public function scaleImage($max) { public function scaleImage($max) {
@ -148,7 +184,7 @@ class Photo {
* If it is not animated, there will be only one iteration here, * If it is not animated, there will be only one iteration here,
* so don't bother checking * 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(); $this->image->setFirstIterator();
do { do {
$this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true); $this->image->resizeImage($max, $max, imagick::FILTER_LANCZOS, 1, true);
@ -365,7 +401,7 @@ class Photo {
if($this->is_imagick()) { if($this->is_imagick()) {
$this->image->setFirstIterator(); $this->image->setFirstIterator();
do { 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()); } while ($this->image->nextImage());
return; return;
} }
@ -425,59 +461,32 @@ class Photo {
if(!$this->is_valid()) if(!$this->is_valid())
return FALSE; return FALSE;
if($this->is_imagick()) {
/* Clean it */
$this->image = $this->image->deconstructImages();
$string = $this->image->getImagesBlob();
return $string;
}
$quality = FALSE; $quality = FALSE;
/** ob_start();
* 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
*/
if(!$this->is_imagick()) ob_start();
switch($this->getType()){ switch($this->getType()){
case "image/png": case "image/png":
$quality = get_config('system','png_quality'); $quality = get_config('system','png_quality');
if((! $quality) || ($quality > 9)) if((! $quality) || ($quality > 9))
$quality = PNG_QUALITY; $quality = PNG_QUALITY;
if($this->is_imagick()) { imagepng($this->image,NULL, $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;
} else imagepng($this->image,NULL, $quality);
break; break;
case "image/gif": case "image/jpeg":
// We change nothing here, do we?
break;
default:
// Convert to jpeg by default
$quality = get_config('system','jpeg_quality'); $quality = get_config('system','jpeg_quality');
if((! $quality) || ($quality > 100)) if((! $quality) || ($quality > 100))
$quality = JPEG_QUALITY; $quality = JPEG_QUALITY;
if($this->is_imagick()) imagejpeg($this->image,NULL,$quality);
$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();
} }
$string = ob_get_contents();
ob_end_clean();
return $string; return $string;
} }
@ -594,14 +603,14 @@ function guess_image_type($filename, $fromcurl=false) {
} }
if (is_null($type)){ if (is_null($type)){
// Guessing from extension? Isn't that... dangerous? // Guessing from extension? Isn't that... dangerous?
if($this->is_imagick()) { if(class_exists('Imagick')) {
/** /**
* Well, this not much better, * Well, this not much better,
* but at least it comes from the data inside the image, * but at least it comes from the data inside the image,
* we won't be tricked by a manipulated extension * we won't be tricked by a manipulated extension
*/ */
$image = new Imagick($filename); $image = new Imagick($filename);
$type = 'image/'. strtolower($image->getImageFormat()); $type = $image->getImageMimeType();
} else { } else {
$ext = pathinfo($filename, PATHINFO_EXTENSION); $ext = pathinfo($filename, PATHINFO_EXTENSION);
$types = Photo::supportedTypes(); $types = Photo::supportedTypes();
@ -636,11 +645,7 @@ function import_profile_photo($photo,$uid,$cid) {
$filename = basename($photo); $filename = basename($photo);
$img_str = fetch_url($photo,true); $img_str = fetch_url($photo,true);
if($this->is_imagick()) $type = null; $type = guess_image_type($photo,true);
else {
// guess mimetype from headers or filename
$type = guess_image_type($photo,true);
}
$img = new Photo($img_str, $type); $img = new Photo($img_str, $type);
if($img->is_valid()) { if($img->is_valid()) {