I’m using Imagick to compress images (JPEG mostly), but the results are quite random. For example, check out these results (aproximate numbers, and all JPEG):
1600kb -> 600kb (saved 1000kb)
1000kb -> 1200kb (wasted 200kb)
400kb -> 500kb (wasted 100kb)
Here’s the code I used:
$image = new Imagick($path);
$image->stripImage(); // remove metadata, though Imagick adds its own, not sure why
$image->setImageCompressionQuality(0); // lossless compression
$image->writeImages($path, true); // writeImages instead of writeImage, in case it's a GIF
Image recompression will vary depending on the source image files. If the original image was carefully made with good software, ImageMagick might not be able to match it. Simple solution: compare the file sizes of the original and new versions, keep the smaller.
Also, note that the Compression Quality number means different things depending on the file type of the image. In particular, 0 means “worst quality, smallest size” for JPEG, but means “fast compression, not necessarily small” for PNG. See http://www.imagemagick.org/script/command-line-options.php#quality