How to compress images in PHP?

Asked

Viewed 7,305 times

6

On my website, people can post messages and along with these messages an image (which is saved in a folder). I wonder if there is any class or any means using PHP to compress these images without losing quality.

Note: Extensions accepted: . jpg and . png

2 answers

7


You can test the quality loss using the PHP GD and see if it fits, you can also do this procedure while uploading the image:

function compressImage($source_path, $destination_path, $quality) {
    $info = getimagesize($source_path);

    if ($info['mime'] == 'image/jpeg') {
        $image = imagecreatefromjpeg($source_path);
    } elseif ($info['mime'] == 'image/png') {
        $image = imagecreatefrompng($source_path);
    }

    imagejpeg($image, $destination_path, $quality);

    return $destination_path;
}

Explaining the functions used:

  • getimagesize(): returns image information (type, size, dimensions, etc.), we use to get the original image MIME Type.
  • imagecreatefromjpeg(): creates a new image from the original image.
  • imagejpeg(): sends an image to the browser or to a file, this is where the lightest image is created.

Finally the function returns the path of the lightest image.

Example of use: $img = compressImage("images/praia.jpg", "images/compressed/compressed_praia.jpg", 6);

  • First thank you for answering. I have a question, in case, when lower the value of #quality Do I have the size of the image or less quality? I didn’t quite understand what the code itself does. For example, this imagecreatefromjpeg would create a "lighter image"?

  • This, the lower the value of $quality worse will be its quality, the default is +- 7.5. I edited the answer with the explanation of the functions used. The function imagecreatefromjpeg /png creates the image identifier, not the image itself, it is actually created with the function imagejpeg(), it is important to emphasize that the generated image will always be with the format .jpeg.

  • Thank you for answering, @Eduardosilva, but I’ll stick to the Imagick() alternative, which in my case seems more viable. Thank you!

  • I like the solution because it uses a lib that usually comes in PHP. Image Magik may even have more resources, but none of them will help the problem in question.

  • Is it@Bacco? I’ll look between the two and comment here which was better...

  • @Igor both serve to resize images, and both have a quality setting when saving, so I usually give preference to GD in my projects. But it’s good you know them both, for when you need them for other uses.

  • It really, really in this case what suits me is GD. Thank you! D

  • 1

    To complement the @Eduardosilva response here explains how to remove EXIF headers from the image with GD.

  • 1

    And what are these EXIF headers, @fernandosavio? Does it make a difference to remove them or not?

  • 1

    EXIF are data on the camera and other metadata that are interesting to save with the original image, but to use on the web image optimizers usually take them out of the image to decrease the size of the image.

Show 5 more comments

3

There are several image processing libraries, some I know are:

I advise you to use the Imagemagick (that is why).

Some things you can do to let your images get smaller and smaller.

Sources:

http://php.net/manual/en/book.imagick.php

Example:

<?php
    $image = new Imagick();

    $image->thumbnailImage(800, 300);
    $image->readImage('image.jpg');
    $image->setImageFormat('jpeg');
    $image->setImageCompressionQuality(85);
    $image->stripImage();

    $image->writeImage('nova_imagem.jpg');

?>
  • Thank you very much, it was exactly what I needed! Just one more question. In the case where will this new image be saved? And how I do with an image I just uploaded?

  • I had already suffered with it last year and I got the images to be very small (1100x700 images with 150k or less).

  • 1

    this function Imagick(); is already implemented in PHP or I have to download some external file?

  • It is a PHP module that connects to the library. In phpinfo(); you see if it is enabled. If it is not, just modify it in php.ini to load it. If you’re not in there in php.ini you’ll probably have to install for your operating system

Browser other questions tagged

You are not signed in. Login or sign up in order to post.