Web Design Tutorials: Photoshop, HTML, Flash, PHP

PHP Tutorial – Best Way to Optimize JPEG Images of Website with ImageMagick and PHP

Optimize images of website is really helpful for save bandwidth and website make more faster. Big website is really need to that. Because they huge user and serve their site to them is really challenging. To optimize the images for your existing website, the following code might be useful:

$dir = '/home/some_directory/'; // the directory with your files
$compr = 80; // the quality precentage
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
$path = $dir.$file;
if (is_file($path)) {
$ext = pathinfo($path, PATHINFO_EXTENSION);
if (preg_match('/^(jpg|jpeg)$/i', $ext)) {
exec(sprintf('convert %s -quality %d %s', $path, $compr, $path));
}
}
}
closedir($handle);
}

Just enter the path to the the directory you like to optimize safe the code as a PHP script and execute the file from the command line of browser. Note only the JPEG files are getting compressed.

Optimize your images top make them load faster, but be careful don’t compress them too much.

Source: webwhiz.info

you may also like:

One Response to “PHP Tutorial – Best Way to Optimize JPEG Images of Website with ImageMagick and PHP”

  1. [...] may ore may not have even thought about this up to this point in your PHP use but it is kind of a big deal. Using the right or wrong quotes can not only cause errors that [...]

  2. avatar Hue Ciresi says:

    I like it, this is just the thing I was looking for, hopefully there will be more of this on your site over time?

Leave a Reply

Powered by WordPress | Designed by Elegant Themes