图片压缩的一些心得(转载)

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;

}