Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress jpeg on server with PHP

I have a site with about 1500 JPEG images, and I want to compress them all. Going through the directories is not a problem, but I cannot seem to find a function that compresses a JPEG that is already on the server (I don't want to upload a new one), and replaces the old one.

Does PHP have a built in function for this? If not, how do I read the JPEG from the folder into the script?

Thanks.

like image 408
Meir Avatar asked Jun 03 '12 11:06

Meir


1 Answers

I prefer using the IMagick extension for working with images. GD uses too much memory, especially for larger files. Here's a code snippet by Charles Hall in the PHP manual:

$img = new Imagick();
$img->readImage($src);
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(90);
$img->stripImage();
$img->writeImage($dest); 
$img->clean();
like image 150
Emil Vikström Avatar answered Sep 17 '22 21:09

Emil Vikström