Page 1 of 1

An Imagick code example

Posted: 2007-08-21T17:18:45-07:00
by ridera
Make this a file and execute it. Makes a very pretty label.

Code: Select all

<?php
/*********************************** Make a nifty label ************************************/

   $report= "<html><body style= \"margin:5%; font-size:12pt\">\n\n";

    $report .= "<p style=\"color:blue; text-align:center\">PHP Imagick Image Library Label Test</p>\n\n";

/* This is the IM command version:
-size 900x85 xc:#ffffee -font Bookman-DemiItalic -pointsize 30 -draw "text 10,60 'Image created and saved: 9 Aug 2007 02:36:38 PM'" -channel RGBA -gaussian 0x6 -fill darkred -stroke magenta -draw "text 5,55 'Image created and saved: 9 Aug 2007 02:36:38 PM'"
*/

   $obj2=       new Imagick();                  //Object
   $draw=      new ImagickDraw();                //Drawing objects
   $color=    new ImagickPixel();               //Pixels color objects

   $php5= ((int)PHP_VERSION >= 5)? TRUE : FALSE;

   if($php5)date_default_timezone_set('America/New_York');                     //Set as needed, php5.1+

   $text= 'Image created and saved: ' . date('j M Y h:i A');
   $width=    940;
   $height=    85;
   $font_size=    32;
   $left_margin=   30;
   $top_margin=   55;                                       //Text and shadow location
   $label_img=   './test_label.jpg';

   $color->setColor('skyblue');
   $obj2->newImage($width, $height, $color);                           //Create the label image

   $draw->setFont("Bookman-DemiItalic");                              //Set the font
   $draw->setFontSize($font_size);                                    //Set the size

   $color->setColor("black");                                                                                              //Shadow color
   $draw->setFillColor($color);

   $obj2->annotateImage($draw, $left_margin+5, $top_margin+5, 0, $text);                //Add the text shadow

   $obj2->gaussianBlurImage(0, 6);                                 //Blur it

   $color->setColor("darkred");                                    //Text color
   $draw->setFillColor($color);
   $color->setColor('magenta');                                    //Stroke color
   $draw->setStrokeColor($color);

   $draw->annotation($left_margin, $top_margin, $text);                     //Add pretty text

   $obj2->drawImage($draw);                                 //Add the text layer

/********* 3d Frame it ********************************/
/*
* The IM command:  -mattecolor Blue -frame 10x10+0+6
*/

   $color->setColor('blue');

   $obj2->frameImage($color, 14, 14, 0, 8);

   $obj2->SetImageFormat('jpeg');

   $obj2->writeImage($label_img);

   $report .= "<img alt=\"image missing\" src=\"$label_img\">\n\n";

        $report .= "</body></html>\n\n";

        echo $report;

Re: An Imagick code example

Posted: 2008-02-25T14:39:08-07:00
by Mitron
Hmm, I gave that a shot but ended up with this error in my error logs:

Code: Select all

[Mon Feb 25 16:29:38 2008] [error] [client 10.0.0.3] PHP Fatal error:  Uncaught exception 'ImagickException' with message 'Unable to annotate image' in drive:\\path\\to\\test.php:39\nStack trace:\n#0 drive:\\path\\to\\test.php(39): Imagick->annotateimage(Object(ImagickDraw), 35, 60, 0, 'Image created a...')\n#1 {main}\n  thrown in drive:\\path\\to\\test.php on line 39
I'm running Windows 2000 Server, Apache 2.2.6, PHP 5.2.5, ImageMagick 6.3.7 with Mikko's Imagick 2.1.1B1 extention.