Technology Powered Knowledge Base
Get Our Free Instant Messenger with Audio & Video Call!

Image Optimization & Compression Using PHP and ImageMagic

VN:F [1.9.22_1171]
Rating: +12 (from 28 votes)
VN:F [1.9.22_1171]
Rating: 8.0/10 (43 votes cast)

Now a days image compression very much required for website and web applications. In today’s world bigger images and many css and js are creating rich web apps but without compression it’s very difficult to serve websites quicker to user’s browser. So compression and optimization are used to server lots of data with compression. We can compress full webpage using Gzip compression technology. In that case we can use .htaccess or PHP to do that, Example:

if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’)){

In this topic I would like to provide image specific optimization technique. Now a days many websites providing us image compression technologies as a manner of API based service and web interface based service. But I prefer own setup and that’s why I would like to use ImageMagic. You know ImageMagic is open-source and free application which can be used for image manipulations. If you have PHP environment then may you need to setup ImageMagic and you need to create a bridge(Driver) to connect ImageMagic with PHP.

Step by step procedure to setup PHP with ImageMagic environment:
Please note, All ImageMagic versions are not compatible with all PHP versions. And you should remember your OS environment before proceed. In my example I am using PHP 5.4.4 in Windows8.1 environment. I would like to prefer ImageMagic 6.8.0 hence I have installed “ImageMagick-6.8.0-7-Q16-windows-dll.exe”. Please visit Official Website: If you required old archived version of ImageMagic then you may visit official website and chose any of mirror and you can find it.
Now install the image magic and you should enable to add imagemagic directory in path environment variable. So that PHP and Bridge can find the ipath of imagemagic executable.
Now you should download exact version of bridge dll(driver) to connect PHP with ImageMagic. You may download your exact version of “php_imagick.dll” by googling. and you should place the dll file in PHP extension folder. php/ext is the right choice for that. Now open php.ini file and add a entry of dll as “extension=php_imagick.dll” in extension section. That’s it.
Now finally you should restart your computer, please do that otherwise you may get dll related some errors. Apache restart is not enough for that. Now check ImageMagic status in PHPinfo. you should see something like “ImageMagick 6.8.0-6 2012-11-20 Q16”.
Code snippet for compress and optimize images using ImageMagic:
Now create a PHP script and write following code on it,

// Create new imagick object
$im = new Imagick("File_Path/Image_Name.jpg");

// Optimize the image layers

// Compression and quality

// Write the image back
$im->writeImages("File_Path/Image_Opti.jpg", true);

In above example I have used jpg type image and that’s why I have used “COMPRESSION_JPEG” you may modify the parameter as per your requirement, I prefer to do that as dynamic possible. To do that you may use “$im->getFormat();”.

If you have any query feel free to ask me via bellow comment section.

Image Optimization & Compression Using PHP and ImageMagic, 8.0 out of 10 based on 43 ratings

Sign Up     Sign In