Resize GIF images using imagick with php

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
simone
Posts: 2
Joined: 2017-11-23T10:02:03-07:00
Authentication code: 1152

Resize GIF images using imagick with php

Post 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();
Last edited by simone on 2017-11-26T00:00:26-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Resize GIF images using imagick with php

Post 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?
simone
Posts: 2
Joined: 2017-11-23T10:02:03-07:00
Authentication code: 1152

Re: Resize GIF images using imagick with php

Post by simone »

@fmw42 I've update with my code.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Resize GIF images using imagick with php

Post 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.
Post Reply