How do I thumbnail the image inside given dimensions proportionally and fill the "blank" areas with a color?
The code is for Imagick 2.1.0 but adapting to older versions should not be hard.
<?php
/* Define width and height of the thumbnail */
$width = 100;
$height = 100;
/* Instanciate and read the image in */
$im = new Imagick( "test.png" );
/* Fit the image into $width x $height box
The third parameter fits the image into a "bounding box" */
$im->thumbnailImage( $width, $height, true );
/* Create a canvas with the desired color */
$canvas = new Imagick();
$canvas->newImage( $width, $height, 'pink', 'png' );
/* Get the image geometry */
$geometry = $im->getImageGeometry();
/* The overlay x and y coordinates */
$x = ( $width - $geometry['width'] ) / 2;
$y = ( $height - $geometry['height'] ) / 2;
/* Composite on the canvas */
$canvas->compositeImage( $im, imagick::COMPOSITE_OVER, $x, $y );
/* Output the image*/
header( "Content-Type: image/png" );
echo $canvas;
?>
|