Filed Under (PHP) by Abhishek Jain on 11-05-2010
<?php
class ImageCreator {
var $ttf_file = "English.ttf";
function createPng($text,$dest_file) {
try {
/*** a new Imagick object ***/
$im = new Imagick();
/*** a new draw object ***/
$draw = new ImagickDraw();
/*** set the font ***/
$draw->setFont($this->ttf_file);
/*** set the font color ***/
$draw->setFillColor(new ImagickPixel("#d4d4d4"));
/*** set the font size ***/
$draw->setFontSize( 40 );
/*** set the box color ***/
$pixel = new ImagickPixel( 'transparent' );
/*** get the font info ***/
$font_info = $im->queryFontMetrics($draw, $text );
/*** the width ***/
$width = $font_info['textWidth']+10;
/*** the height ***/
$height = $font_info['textHeight'];
/*** a new image with the dynamic sizes ***/
$im->newImage($width, $height, $pixel);
/*** annotate the text on the image ***/
$im->annotateImage($draw, 0,27, 0, $text);
/*** set the image format ***/
$im->setImageFormat('png');
/*** write image to disk ***/
$im->writeImage($dest_file);
} catch (Exception $e) {
$this->log($e->getMessage());
}
}
}