Here is an example of a bit more complex drawing operation using ImagickDraw. This example extends the ImagickDraw class to create a custom shape, in this case a flower. That flower is then rendered to a canvas.

I like to keep it short with the descriptions, here's the code:

PHP:
  1. <?php
  2.  
  3. class Flower extends ImagickDraw
  4. {
  5.     private $ImagickPixel;
  6.  
  7.     public function __construct()
  8.     {
  9.         $this->ImagickPixel = new ImagickPixel();
  10.     }
  11.  
  12.     public function drawFlower()
  13.     {
  14.         $this->createStraw();
  15.         $this->createPetals();
  16.         $this->createLeafs();
  17.     }
  18.  
  19.     private function createPetals()
  20.     {
  21.         $this->ImagickPixel->setColor( 'red' );
  22.         $this->setFillColor( $this->ImagickPixel );
  23.  
  24.         $this->ImagickPixel->setColor( 'black' );
  25.         $this->setStrokeColor( $this->ImagickPixel );
  26.         $this->setStrokeWidth( 0.5 );
  27.  
  28.         $this->circle( 40, 90, 70, 110 );
  29.         $this->circle( 85, 50, 115, 70 );
  30.         $this->circle( 125, 90, 155, 110 );
  31.         $this->circle( 85, 130, 115, 150 );
  32.  
  33.         $this->ImagickPixel->setColor( 'white' );
  34.         $this->setFillColor( $this->ImagickPixel );
  35.  
  36.         $this->circle( 85, 90, 115, 110 );
  37.     }
  38.  
  39.     private function createStraw()
  40.     {
  41.         $this->ImagickPixel->setColor( 'transparent' );
  42.         $this->setFillColor( $this->ImagickPixel );
  43.  
  44.         $this->ImagickPixel->setColor( 'green' );
  45.         $this->setStrokeColor( $this->ImagickPixel );
  46.         $this->setStrokeWidth( 4 );
  47.  
  48.         $this->bezier( array(
  49.                              array( 'x' => 85, 'y' => 150 ),
  50.                              array( 'x' => 105, 'y' => 190 ),
  51.                              array( 'x' => 55, 'y' => 400 ),
  52.                              array( 'x' => 75, 'y' => 500 ),
  53.                              )
  54.                       );
  55.     }
  56.  
  57.     private function createLeafs()
  58.     {
  59.         $this->ImagickPixel->setColor( 'green' );
  60.         $this->setFillColor( $this->ImagickPixel );
  61.         $this->setStrokeColor( $this->ImagickPixel );
  62.  
  63.         $this->bezier( array(
  64.                              array( 'x' => 75, 'y' => 370 ),
  65.                              array( 'x' => 65, 'y' => 250 ),
  66.                              array( 'x' => 25, 'y' => 270 ),
  67.                              array( 'x' => 15, 'y' => 290 ),
  68.                              array( 'x' => 25, 'y' => 310 ),
  69.                              array( 'x' => 35, 'y' => 330 ),
  70.                              array( 'x' => 55, 'y' => 350 ),
  71.                              )
  72.                       );
  73.  
  74.         $this->bezier( array(
  75.                              array( 'x' => 75, 'y' => 365 ),
  76.                              array( 'x' => 95, 'y' => 250 ),
  77.                              array( 'x' => 135, 'y' => 270 ),
  78.                              array( 'x' => 145, 'y' => 280 ),
  79.                              array( 'x' => 145, 'y' => 290 ),
  80.                              array( 'x' => 135, 'y' => 310 ),
  81.                              array( 'x' => 125, 'y' => 320 ),
  82.                             )
  83.                      );
  84.     }
  85. }
  86.  
  87. $Flower = new Flower();
  88. $Flower->drawFlower();
  89.  
  90. $Imagick = new Imagick();
  91. $Imagick->newPseudoImage( 175, 500, "gradient:pink-white" );
  92. $Imagick->setImageFormat( 'png' );
  93.  
  94. $Imagick->drawImage( $Flower );
  95.  
  96. header( "Content-Type: image/{$Imagick->getImageFormat()}" );
  97. echo $Imagick;
  98.  
  99. ?>

I didn't have time to comment the code. But here's the result:

flower

Bookmark and Share