Padding thumbnail with color 'Imagick' : Create and modify images : Graphics Image PHP Source Code


PHP Source Code » Graphics Image » Create and modify images »

 

Padding thumbnail with color 'Imagick'


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$heighttrue );
/* 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$imimagick::COMPOSITE_OVER$x$y );
/* Output the image*/
header"Content-Type: image/png" );
echo 
$canvas;
?>


HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Graphics Image
» Create and modify images