PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] How to resize image with imagick php

 September 18, 2022     imagemagick, imagick, php     No comments   

Issue

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


Solution

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);


Answered By - Mark Setchell
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing