Open In App

PHP | ImagickDraw pushPattern() Function

Last Updated : 30 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The ImagickDraw::pushPattern() function is an inbuilt function in PHP which is used to contain the definition of a named pattern. Everything between pushPattern() and popPattern() is the definition of pattern.

Syntax:

bool ImagickDraw::pushPattern( string $pattern_id, 
      float $x, float $y, float $width, float $height )

Parameters: This function accepts five parameters as mentioned above and described below:

  • $pattern_id: It specifies the unique name of the pattern
  • $x: It specifies the x-coordinate of the top-left corner.
  • $y: It specifies the y-coordinate of the top-left corner.
  • $width: It specifies the width of the pattern.
  • $height: It specifies the height of the pattern.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.
Below given programs illustrate the ImagickDraw::pushPattern() function in PHP:
Program 1:




<?php
  
// Create a new imagick object
$imagick = new Imagick();
  
// Create a image on imagick object
$imagick->newImage(800, 250, 'white');
  
// Create a new imagickDraw object
$draw = new ImagickDraw();
  
// Push the pattern
$draw->pushPattern("MyPattern", 0, 0, 50, 50);
$color = ['red', 'green', 'blue'];
for ($x = 0; $x < 50; $x += 10) {
    for ($y = 0; $y < 50; $y += 5) {
        $draw->setFillColor($color[$y % 3]);
        $draw->rectangle($x % 5, $y + 1, $x, $y + 50);
    }
}
// Pop the pattern
$draw->popPattern();
  
// Set the fill Opacity
$draw->setFillOpacity(0);
  
// Set the fill pattern URL
$draw->setFillPatternURL('#MyPattern');
  
// Draw a rectangle
$draw->rectangle(0, 0, 900, 900);
  
// Render the draw commands
$imagick->drawImage($draw);
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Program 2:




<?php
  
// Create a new imagick object
$imagick = new Imagick();
  
// Create a image on imagick object
$imagick->newImage(800, 250, 'white');
  
// Create a new imagickDraw object
$draw = new ImagickDraw();
  
// Push the pattern
$draw->pushPattern("MyPattern", 0, 0, 50, 50);
$color = ['red', 'green', 'cyan'];
for ($x = 0; $x < 50; $x += 10) {
    for ($y = 0; $y < 50; $y += 5) {
        $draw->setFillColor($color[$y % 3]);
        $draw->circle($x % 2, $y + 100, $x, $y);
    }
}
// Pop the pattern
$draw->popPattern();
  
// Set the fill Opacity
$draw->setFillOpacity(0);
  
// Set the fill pattern URL
$draw->setFillPatternURL('#MyPattern');
  
// Draw a rectangle
$draw->rectangle(0, 0, 900, 900);
  
// Render the draw commands
$imagick->drawImage($draw);
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Reference: https://www.php.net/manual/en/imagickdraw.pushpattern.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads