Location: PHPKode > scripts > Image Modifier > image-modifier/ImageModifier.class.php
<?php
/**
 * standalone class for ImageModifier
 * 
 * Features :
 * Generating Image withe the combination of text & image on a image template:
 * 
 * @author sudhir vishwakarma <hide@address.com>
 * @copyright Sudhir Vishwakarma 
 * 
 * @version 0.1 20081003
 *
 */
class ImageModifier {

    /**
     *  Holds Text Information for Maniuplating on images
     *
     *  default is array()
     *
     *  @access private
     */
    var $aTextData = array();

    /**
     *  Holds Image Information for Maniuplating on images
     *
     *  default is array()
     *
     *  @access private
     */
    var $aImageData = array();

    /**
     *  Resource Identifier For Imagick
     *
     *  default is FALSE
     *
     *  @access private
     */
    var $image = "";

    /**
     *  Holds Error status
     *
     *  default is 0
     *
     *  @var    integer
     */
    var $sError = 0;


   /**
     *  Constructor of the class
     *  
     *  @param  string  $tplImage   template image
     *  @access private
     */
    public function __construct($tplImage) {

        // Check Imagick class exist or not if not show error.
        if (!class_exists("Imagick", false)) {
            exit("Unable to load class: Imagick\n. Imagick Image Library Missing.");
        }
        // create a object of Imagick template image
        $this->image = new Imagick($tplImage);
    }

    /**
     *  Setting Text properties 
     *
     *  @param  string  $sText          text to print on the image (i.e. Buy 1 Get 1 Free )
     *  @param  integer  $x             text to print from x codinates      
     *  @param  integer  $y             text to print from y codinates
     *  @param  integer  $font          text size for printing  
     *  @param  string  $color          text color for print  
     *  @param  integer  $text_angle    rotate text from 0-360  
     *  @param  string  $font_style     installed font name and path (i.e /usr/share/fonts/liberation/LiberationSans-Italic.ttf)
     *  @Creating an array of text properties
     */

    public function setText($sText, $x = 0, $y = 0, $font = 12, $color = 'black', $text_angle = 0, $font_style = './LiberationSans-Italic.ttf') {
        $this->aTextData[] = array("text"=>$sText, "font_color"=>$color, "font_size"=>$font,"x"=>$x,"y"=>$y, "font_style"=>$font_style, "text_angle"=>$text_angle);
    }

    /**
     *  Setting Image properties 
     *
     *  @param  string  $sImage         text to print on the image (i.e. /home/httpd/images/brand.jpg )
     *  @param  integer  $x             text to print from x codinates      
     *  @param  integer  $y             text to print from y codinates
     *  @param  integer  $text_angle    rotate text from 0-180  
     *  @Creating an array of image properties
     */

    public function setImage($sImage, $x = 0, $y = 0, $angle=0) {
        $this->aImageData[] = array("image"=>$sImage, "x"=>$x, "y"=>$y, "angle"=>$angle);
    }

    /**
     *  generating final image from text & image properties 
     *
     *  @param  string  $sImage         Output image Name
     *  @return boolean returns TRUE on success and FALSE upon failure
     */

    public function generateImage($sImage) {
        foreach ($this->aImageData as $aImageValue) {
            if (!trim($aImageValue["image"])) {
                $sError = 1;
                break;
            }
            $oImg = new Imagick($aImageValue["image"]);
            $oImg->rotateImage("transparent", $aImageValue["angle"]);
            $this->image->compositeImage($oImg, $oImg->getImageCompose(), $aImageValue["x"], $aImageValue["y"]);
            unset($oImg);
        }
        foreach ($this->aTextData as $aTextValue){
            if (!trim($aTextValue['text'])) {
                $sError = 2;
                break;
            }
            $oDraw = new ImagickDraw();
            $oDraw->setFont($aTextValue['font_style']);
            $oDraw->setFontSize($aTextValue['font_size']);
            $oDraw->setFillColor($aTextValue['font_color']);
            $this->image->annotateImage($oDraw, $aTextValue['x'], $aTextValue['y'], $aTextValue['text_angle'], $aTextValue['text']);
            unset($oDraw);
        }
        if ($sError == 1) {
            exit("Unable to generate Image. Check \"setImage\" Properties");
        }elseif ($sError == 2) {
            exit("Unable to generate Image. Check \"setText\" Properties");
        }
        $this->image->setImageFormat("jpg");
        return $this->image->writeImage($sImage);
    }
}
?>
Return current item: Image Modifier