It is man's ability to remember that sets us apart, we are the only species concerned with the past. Our memories give us voice, to bear witness to history so that others might learn, so they might celebrate our triumph and be warned of our failures ...

How to create transparent images with Imagick

Filed Under (PHP) by Abhishek Jain on 11-05-2010

Tagged Under : , , , ,

<?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());
        }
    }
}
Subscribe to Rss Feed : Rss