Optimize uploaded images with php (jpeg)
Asked Answered
B

4

18

When running Page Speed in Google Chrome it suggests to optimize/compress the images. These images are mostly uploaded by users, so I would need to optimize them during uploading. What I find about optimizing jpeg images with php is something like using the following GD functions:

getimagesize()
imagecreatefromjpeg()
imagejpeg()

Since I am resizing the images after upload I'm already pulling the image through these functions and in addition I use imagecopyresampled() after imagecreatefromjpeg() to resize it.

But then, Page Speed is still telling me these images can be optimized. How can I accomplish this optimisation in a php script? Set the quality lower in imagejpeg() doesn't make a difference either.

Bondswoman answered 28/12, 2012 at 16:19 Comment(0)
S
22

The imagejpeg function is where you assign the quality. If you're already setting that to an appropriate value then there is little else you can do.

Page speed probably considers all images above a certain size to be "needing compression", perhaps just ensure they are all as small as reasonable (in terms of height/width) and compressed.

You can find more about page speed and it's compression suggestions on the pagespeed docs http://code.google.com/speed/page-speed/docs/payload.html#CompressImages which describes some of the techniques/tools to compress appropriately.

I've also just read the following:

Several tools are available that perform further, lossless compression on JPEG and PNG files, with no effect on image quality. For JPEG, we recommend jpegtran or jpegoptim (available on Linux only; run with the --strip-all option). For PNG, we recommend OptiPNG or PNGOUT.

So perhaps (if you really want to stick to Google's suggestions) you could use PHP's exec to run one of those tools on files as they are uploaded.


To compress with php you do the following (sounds like you are already doing this):

Where $source_url is the image, $destination_url is where to save and $quality is a number between 1 and 100 choosing how much jpeg compression to use.

function compressImage($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);

    //save file
    imagejpeg($image, $destination_url, $quality);

    //return destination file
    return $destination_url;
}
Succuss answered 28/12, 2012 at 16:39 Comment(1)
Oke thanks, I guess it's fine the way it's now then. I'm indeed already doing what you describe. Further compression with jpegtran etc. won't be necessary in my caseBondswoman
B
8

Repaired function:

function compressImage($source_url, $destination_url, $quality) {

    //$quality :: 0 - 100

    if( $destination_url == NULL || $destination_url == "" ) $destination_url = $source_url;

    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg')
    {
        $image = imagecreatefromjpeg($source_url);
        //save file
        //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).
        imagejpeg($image, $destination_url, $quality);

        //Free up memory
        imagedestroy($image);
    }
    elseif ($info['mime'] == 'image/png')
    {
        $image = imagecreatefrompng($source_url);

        imageAlphaBlending($image, true);
        imageSaveAlpha($image, true);

        /* chang to png quality */
        $png_quality = 9 - round(($quality / 100 ) * 9 );
        imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression).
        //Free up memory
        imagedestroy($image);
    }else
        return FALSE;

    return $destination_url;

}
Belike answered 12/1, 2016 at 21:18 Comment(0)
D
2

You could use Imagick class for this. Consider following wrapper function:

<?php
    function resizeImage($imagePath, $width, $height, $blur, $filterType = Imagick::FILTER_LANCZOS, $bestFit = false)
    {
        //The blur factor where &gt; 1 is blurry, &lt; 1 is sharp.
        $img= new \Imagick(realpath($imagePath));
        $img->setCompression(Imagick::COMPRESSION_JPEG); 
        $img->setCompressionQuality(40);
        $img->stripImage();
        $img->resizeImage($width, $height, $filterType, $blur, $bestFit);
        $img->writeImage();
    }

?>

Read more on how to resize images with Imagick at:
http://php.net/manual/en/class.imagick.php
http://php.net/manual/en/imagick.resizeimage.php http://php.net/manual/en/imagick.constants.php#imagick.constants.filters

Dennett answered 24/3, 2018 at 20:26 Comment(0)
B
-2

it is very important to optimize your images. Several CMS platforms out there have modules or plugins to preform this process. However if you are programming it yourself there is a complete php tutorial located at this page https://a1websitepro.com/optimize-images-with-php-in-a-directory-on-your-server/ You will be shown how to implement the imagecreatefromjpeg($SrcImage); and imagecreatefrompng($SrcImage); and imagecreatefromgif($SrcImage); There are written and video instruction on the page.

Bohi answered 29/3, 2015 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.