1

With imagick command, i want to resize image form 2Mb to 200KB, i tried:

convert a.jpg -strip -define jpeg:extent=200k a1.jpg

And it work. But i want to use imagick function of php to resize blob image.

Php's documentation regarding the resizeImage method require width and height.

How can resize blob image from 2Mb to 200Kb like the command line above.

I tried:

    $imageBlob = file_get_contents('a.jpg');
    $image = new \Imagick();
    $image->readImageBlob($imageBlob);
    $height = $image->getImageHeight();
    $width = $image->getImageWidth();
    $image->resizeImage( $width, $height,  \Imagick::FILTER_LANCZOS, 1 );
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_buffer($finfo, $image->getImageBlob());
    $mime == "image/svg" && $mime.= "+xml";
    $base64 =  "data:$mime;base64," . base64_encode($image->getImageBlob());
    echo "<img src=\"".$base64.'">';die;

But this doesn't reduce the size from 2Mb to 200Kb

4
  • 1
    The command-line command you show does not decrease the image size to 200kB. It resizes the image to be 200 pixels wide. Commented Apr 27, 2021 at 11:58
  • 1
    To resize to a particular file size for JPG only, see -define jpeg:extent at legacy.imagemagick.org/Usage/formats/#jpg_write Commented Apr 27, 2021 at 15:45
  • @Mark Setchell, i have been updated script Commented Apr 28, 2021 at 2:48
  • @ fmw42, i want to use Imagick of Php to resize it, instead of command Commented Apr 28, 2021 at 2:50

1 Answer 1

2

I have not tested this, but am fairly sure you can use setOption like this to set the upper limit for a JPEG's filesize:

$imagick->setOption('jpeg:extent', $extent);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.