标签:
大家对加水印这块无异议,仅在压缩上面做了文章,我只贴这里的代码。
/*** 缩小图片尺寸.** @param $image 待处理的二进制图片* @param $width 处理后图片尺寸的宽度(px)* @param $height 处理后图片尺寸的高度(px)* @param $crop 是否裁剪图片** @return 处理好的二进制图片*/function resize($image, $width, $height, $crop) {$imagick = new Imagick();$imagick->readImageBlob($image);$w = $imagick->getImageWidth();$h = $imagick->getImageHeight();if ($w > $width || $h > $height) {if ($crop) {$imagick->cropThumbnailImage($width, $height);} else {$imagick->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1, true);}}$processed_image = $imagick->getImageBlob();return $processed_image;}
function resize($image, $width, $height, $crop) {
$im = new Imagick();
$im->readImageBlob($image);
$input_width = $width;
$input_height = $height;
$src_width = $im->getImageWidth();
$src_height = $im->getImageHeight();
$width_rate = $src_width/$width;
$height_rate = $src_height/$height;
if($width_rate>1||$height_rate>1){
if($crop){
if($width_rate>$height_rate){
$width = $src_width/$height_rate;
}else{
$height = $src_height/$width_rate;
}
}else{
if($width_rate>$height_rate){
$height = $src_height/$width_rate;
}else{
$width = $src_width/$height_rate;
}
}
$im->resizeImage($width, $height, Imagick::FILTER_CATROM, 1, false);
if($crop){
if($width>$input_width){
$im->cropImage ( $input_width , $height , ($width-$input_width)/2 , 0 );
}elseif($height>$input_height){
$im->cropImage ( $width , $input_height , 0 , ($height-$input_height)/2 );
}
}
}
$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality(75);
$im->stripImage();
$im->setImageFormat(‘JPEG’);
$blob = $im->getImageBlob();
$im->clear();
$im->destroy();
return $blob;
}
function resize($image,$width,$height,$crop) {
$imagick = new Imagick();
$imagick->readImageBlob($image);
$imagick->setImageCompression($compression_type);
$imagick->setImageCompressionQuality(80);
if($crop) {
$imagick->cropThumbnailImage($width, $height);
}else{
$imagick->resizeImage($width, $height, Imagick::FILTER_CATROM, 1, true);
}
$imagick->stripImage();
$processed_image = $imagick->getImageBlob();
return $processed_image;
}
function resize($image, $width, $height, $crop) {
$imagick = new Imagick();
$imagick->readImageBlob($image);
if ($crop) {
$imagick->cropThumbnailImage($width, $height);
} else {
$imagick->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1, true);
}
$imagick->setImageFormat(‘JPEG’);
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$a = $imagick->getImageCompressionQuality() * 0.75;
if ($a == 0) $a = 75;
$imagick->setImageCompressionQuality($a);
$geo = $imagick->getImageGeometry();
$imagick->ThumbnailImage($geo[‘width‘], $geo[‘height‘]);
$imagick->stripImage();
$blob = $imagick->getImageBlob();
$imagick->clear();
$imagick->destroy();
return $blob;
}
function resize($image, $width, $height, $crop) {
$imagick = new Imagick();
$imagick->readImageBlob($image);
$w = $imagick->getImageWidth();
$h = $imagick->getImageHeight();
if ($w > $width || $h > $height) {
if ($crop) {
$imagick->cropThumbnailImage($width, $height);
} else {
$imagick->resizeImage($width, $height, Imagick::FILTER_CATROM, 1, true);
}
}
$imagick->setImageFormat(‘JPEG’);
$imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
$a = $imagick->getImageCompressionQuality() * 0.75;
if ($a == 0) {
$a = 75;
}
$imagick->setImageCompressionQuality($a);
$imagick->stripImage();
$blob = $imagick->getImageBlob();
$imagick->clear();
$imagick->destroy();
return $blob;
}
对300张生产环境下抽取的原始图片进行测试,结果如下:
性能方面都符合要求。除了第3组比示例代码慢5%左右,其他两组都比示例代码更快(1组约快15%,2组约快6%)
2组提交太慢太快,有一处遗漏,其实可以简单提高压缩比到58%左右
之后,综合3组的代码,弄了个best版本,测试结果为,
标签:
原文地址:http://www.cnblogs.com/sanwenyu/p/4573657.html