Skip to content

Instantly share code, notes, and snippets.

@guilhermeblanco
Created February 11, 2011 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermeblanco/821843 to your computer and use it in GitHub Desktop.
Save guilhermeblanco/821843 to your computer and use it in GitHub Desktop.
With PECL Imagick extension and using PHP
<?php
$maxWidth = 200;
$maxHeight = 200;
try {
$imagick = new Imagick();
$fh = fopen(__DIR__ . '/batman.gif', 'r');
$imagick->readImageFile($fh);
$newImagick = $imagick->coalesceImages();
$width = $newImagick->getImageWidth();
$height = $newImagick->getImageHeight();
$fitByWidth = (($maxWidth / $width) < ($maxHeight / $height)) ? true : false;
$maxWidth = ($fitByWidth) ? $maxWidth : 0;
$maxHeight = ($fitByWidth) ? 0 : $maxHeight;
foreach ($newImagick as $k => $frame) {
$newImagick->thumbnailImage($maxWidth, $maxHeight, false);
}
header('Content-Type: image/' . $newImagick->getImageFormat());
echo $newImagick->getImagesBlob();
} catch (Exception $e) {
die($e->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment