How to compress an image using imagemagick in PHP?

By | November 17, 2013

(first instal imagemagick into the server, then it will set the path as follows)

$imagemagicpath=’usr/bin’ //usualy this is the path for image magick

$source=’images/first.jpg’;

//first.jpg is renamed to converted.jpg
//Specifying destination path

$destination=’images/converted.jpg’;

//path for imagemagic function
//specify quality here
$cmd=$imagemagicpath.”/convert “.$source.” -quality 10% “.$destination;

//above line is executing here
exec($cmd);

this will reduce the image quality to 10%
for example first.jpg is initially 120kb it will reduced to 12 kb.