Imagick picture compression.

Source: Internet
Author: User

    1. Select an appropriate image processing extension package.
      • Common extensions such as Gd,imagick,gmagick.
      • The antique GD loses, the efficiency is very low, and the compressed picture volume is very big =. = Imagick is a good choice and is very noticeable in PHP's image processing extensions. Whether it's a static picture of a JPG or PNG, or a animated picture of GIF, compressing and zooming in and out enlarges the size.
      • Gmagic did not try, and GraphicsMagick is said to be quite a force, but many comments on the internet are basically some of the evaluation of efficiency.
      • Program optimization, see the next three groups of solutions and the ultimate best solution.

Everyone to add watermark this piece no objection, only in the compression above did the article, I only paste here code.

Before optimization:
/*** reduce the image size. * * @param $image pending binary picture * @param the width of the image size after $width processing (px) * @param $height The height of the image size after processing (px) * @param $crop Crop Picture * * @return processed binary picture */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;}
First group:

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;

}

Group II:

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;

}

Group III:

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 =;

$imagick->setimagecompressionquality ($a);

$geo = $imagick->getimagegeometry ();

$imagick->thumbnailimage ($geo [' width '], $geo [' height ']);

$imagick->stripimage ();

$blob = $imagick->getimageblob ();

$imagick->clear ();

$imagick->destroy ();

Return $blob;

}

Final Solution:

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;

}

Look at the results:

The original images extracted from 300 production environments were tested with the following results:

    • Example code :
      29,220,912 (28,536KB)
    • 1 Groups
      11,282,151 (11,018KB) Savings over example code: 61.39%
    • 2 groups
      16,281,139 (15,900KB) Savings over example code: 44.28%
    • 3 groups
      10,531,926 (10,285KB) Savings over example code: 63.96%

Performance is in line with the requirements. In addition to the 3rd group is about 5% slower than the sample code, the other two groups are faster than the sample code (1 groups about Fast 15%,2 Group about 6% faster)
2 groups submitted too slow too fast, there is a omission, in fact, can simply improve the compression ratio to about 58%

After that, a combination of 3 sets of code, the best version, the test results are,

    • Best
      9,626,986 (9,401KB) Savings over example code: 67.05%
Summary: 1, the compression rate as small as possible, this should be discussed with the business unit to find a balance point. (Note that the best method sets the quality method using the get to current picture compression rate and then take 75%, if the current picture compression rate is 60%, if using the $imagick->setimagecompressionquality (80) method will increase the picture compression rate to 80%, this will make the picture Bigger!!!  ) 2, be sure to remove the EXIF information of the picture!!!! For details of this section, please see HTTP://BAIKE.BAIDU.COM/VIEW/22006.HTM3, compression size using the Imagick::filter_catrom method to improve speed, the quality of the picture itself has not changed greatly. 4, $imagick->setimageformat (' JPEG ') is also very force. 5, simple forget, these lines of code each month to our company province at least 2W of the traffic cost, if our picture library is getting bigger, it will be more force.

Imagick picture compression.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.