Some Functions for the community,
Code: Select all
/**
* applyImage
* @param imCanvas - Imagick object to apply image to
* @param src - the path/url to the relavent image file
* @param x - optional, x-coord to start applying image
* @param y - optional, y-coord to start applying image
* @param w - optional, pre-crop image to this width
* @param h - optional, pre-crop image to this height
**/
function applyImage($imCanvas, $src = '', $x = 0, $y = 0, $w = 0, $h = 0) {
$img = new Imagick();
$img->readImageFile(fopen($src, 'rb'));
if (!empty($w) && !empty($h)) $img->cropThumbnailImage($w, $h);
$imCanvas->compositeImage($img, Imagick::COMPOSITE_DEFAULT, $x, $y);
$img->destroy();
}
/*.................................................................*/
/**
* applyText
* @param imCanvas - Imagick object to apply image to
* @param text - text to apply
* @param font - font file to use
* @param x - optional, x-coord to start applying text
* @param y - optional, y-coord to start applying text
* @param w - optional, max width of text box
* @param h - optional, max height of text box
**/
function applyText($imCanvas, $text = '', $font = 'Arial', $fontSize = 10, $color = '#000000', $x = 0, $y = 0, $w = 0, $h = 0) {
$w = $w ? $w : ($imCanvas->getImageWidth() - $x);
$h = $h ? $h : ($imCanvas->getImageHeight() - $y);
$temp = new Imagick();
$temp->newImage($w, $h, 'transparent');
$textSettings = new ImagickDraw();
$textSettings->setFont($font);
$textSettings->setFontSize($fontSize);
$textSettings->setFillColor(new ImagickPixel($color));
$textSettings->setGravity(Imagick::GRAVITY_NORTHWEST);
annotateWrapped($temp, $textSettings, $text);
$imCanvas->compositeImage($temp, Imagick::COMPOSITE_OVER, $x, $y);
$textSettings->destroy();
$temp->destroy();
}
/*.................................................................*/
/**
* applyText
* @param imCanvas - Imagick object to apply image to
* @param imdSettings - ImagickDraw object that holds font settings
* @param text - text to apply
*
* @thanks http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
**/
function annotateWrapped($imCanvas, $imdSettings, $text = '') {
$maxWidth = $imCanvas->getImageWidth();
$metrics = $imCanvas->queryFontMetrics($imdSettings, $text);
if ($metrics["textWidth"] > $maxWidth) {
$avgCharWidth = $metrics["textWidth"] / strlen($text);
$charsPerLine = floor($maxWidth / $avgCharWidth);
$text = preg_replace('/(.{1,'.$charsPerLine.'})( +|$\n?)|(.{1,'.$charsPerLine.'})/', "$1$3\n", $text);
}
$imCanvas->annotateImage($imdSettings, 0, 0, 0, $text);
}
/*.................................................................*/
Usage Example:
Code: Select all
[code]
$image = new Imagick();
$image->newImage(500, 200, 'transparent');
applyImage($image, 'http://www.imagemagick.org/discourse-server/download/file.php?avatar=2_1196458341.png');
applyImage($image, 'http://www.imagemagick.org/discourse-server/download/file.php?avatar=2_1196458341.png', 50, 50, 25, 40);
applyText($image, 'ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you may freely use, copy, modify, and distribute. Its license is compatible with the GPL. It runs on all major operating systems.', '/myfont/Arial.ttf', 12, 75, 50, 150, 100);