Page 1 of 1

Resize GIF images using imagick with php

Posted: 2017-11-23T10:05:55-07:00
by simone
is it possible to resize a gif image to several sizes and save in separate folders?

Below code doesn't work

$imagick = new Imagick($_FILES['file']['tmp_name']);
foreach ($paramVars as $var) {
$imagick = $imagick->coalesceImages();

do {
$imagick->scaleImage($var['cols'], $var['rows'], true);
} while ($imagick->nextImage());

$imagick = $imagick->deconstructImages();
$imagick->writeImages($path, true);
}

$imagick->clear();
$imagick->destroy();

Re: Resize GIF images using imagick with php

Posted: 2017-11-23T12:57:55-07:00
by fmw42
It should be possible. I presume you mean with Imagick. But please always provide your IM versions and platform and the code you tried. Is this an animated gif or a single frame gif.

Sorry, I do not know Imagick well.

I suspect if you can convert it to one size, then you can loop over it several times and resize to different sizes and then save the resulting gif. So first get your code working without variables and one resize. Then add the looping with variables. Perhaps the issue is with your variables?

Re: Resize GIF images using imagick with php

Posted: 2017-11-26T00:01:14-07:00
by simone
@fmw42 I've update with my code.

Re: Resize GIF images using imagick with php

Posted: 2017-11-26T11:17:27-07:00
by Bonzo
A good place to start is the php imagick website. The first example on this page worked for me except it squished the image:
http://php.net/manual/en/imagick.coalesceimages.php

I added a true option to the thumbnail setting and the original image is only read in once. You could probably optimise the code a bit.

Code: Select all

<?php 
$file_src='giphy.gif';
$file_dst='small.gif';
$file_dst_tiny='tiny.gif';

$size_w = 150;
$size_h = 150;

$image = new Imagick($file_src); 

$image = $image->coalesceImages(); 

foreach ($image as $frame) { 
  $frame->thumbnailImage($size_w, $size_h, true);  
} 

$image = $image->deconstructImages(); 
$image->writeImages($file_dst, true); 

$size_w = 100;
$size_h = 100;

$image = $image->coalesceImages();

foreach ($image as $frame) {  
  $frame->thumbnailImage($size_w, $size_h, true);  
} 

$image = $image->deconstructImages(); 
$image->writeImages($file_dst_tiny, true);
?> 

<img src="giphy.gif">
<img src="small.gif">
<img src="tiny.gif">
I do not use Imagick code so will not be able to answer many questions about it.