skip to Main Content

I have the following code:

$im = new Imagick($original_image_filename);

$original_width = $im->getImageWidth();
$original_height = $im->getImageHeight();
$original_length = $im->getImageLength();

if($original_width > 300) {

    $im->scaleImage(300, 0);    // max size for images is 300px wide

}

$scaled_width = $im->getImageWidth();
$scaled_height = $im->getImageHeight();
$scaled_length = $im->getImageLength();

$compression_quality = 20;

$im->setImageCompression(Imagick::COMPRESSION_JPEG);
$im->setImageCompressionQuality($compression_quality);

$compressed_length = $im->getImageLength();

$original_width, $original_height, and $original_length all work as expected but $scaled_length and $compressed_length are all set to 0.

What can I do to get the length of the image after scaling and compressing it?

I need to know as I want to limit thumbnail file size.

2

Answers


  1. You can convert the image to string and get its length:

    $finalSize = strlen($im->__toString());
    

    See more here https://www.php.net/manual/en/imagick.tostring.php

    Login or Signup to reply.
  2. If you want to limit the thumbnail size of a JPEG, you may be better off just telling ImageMagick to do that for you:

    $imagick->setOption('jpeg:extent', '86kb');
    

    It will then set the quality as high as possible for you whilst respecting your maximum size.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search