PHP | ImagickDraw setViewbox() Function

The ImagickDraw::setViewbox() function is an inbuilt function of PHP which is used to set the overall canvas size. This function set the overall canvas size to be recorded with the drawing vector data. It will be specified using the same size as the canvas image.

Syntax:

bool ImagickDraw::setViewbox( $x1, $y1, $x2, $y2 )

Parameters: This function accepts four parameter as mentioned above and described below:



  • $x1: This parameter is used to hold the value of left x coordinate.
  • $y1: This parameter is used to hold the value of left y coordinate.
  • $x2: This parameter is used to hold the value of right x coordinate.
  • $y2: This parameter is used to hold the value of right y coordinate.

Return Value: This function does not return any value.

Below programs illustrate the ImagickDraw::setViewbox() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
  
// Create new ImagickDraw object
$draw = new \ImagickDraw();
  
// Set the Stroke color 
$draw->setStrokeColor('black');
  
// Set the image filled color
$draw->setFillColor('yellow');
  
// Set the Stroke Width
$draw->setStrokeWidth(2);
  
// Set the Font Size
$draw->setFontSize(72);
  
// Draw the rectangle
$draw->rectangle(150, 450, 450, 300);
  
// Set the view box
$draw->setviewbox(0, 0, 200, 200);
  
// Set the image filled color
$draw->setFillColor('green');
  
// Draw the rectangle
$draw->rectangle(150, 450, 250, 300);
  
// Set the image filled color
$draw->setFillColor('red');
  
// Draw the circle
$draw->circle(100, 100, 125, 0);
  
// Create new Imagick object  
$imagick = new \Imagick();
  
// Set the i age dimensions
$imagick->newImage(500, 500, 'white');
  
// Set the image format
$imagick->setImageFormat("png");
  
// Draw the image 
$imagick->drawImage($draw);
header("Content-Type: image/png");
  
// Display the image
echo $imagick->getImageBlob();
?>

chevron_right


Output:
setViewBox

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an ImagickDraw object 
$draw = new \ImagickDraw();
   
// Set the stroke color 
$draw->setStrokeColor('black');
   
// Set the image filled color
$draw->setFillColor('red');
$points = [['x' => 40 * 5, 'y' => 10 * 5], 
           ['x' => 70 * 5, 'y' => 50 * 5],
           ['x' => 60 * 5, 'y' => 15 * 5], ];
   
// Draw the polygon
$draw->polygon($points);
   
// Set the view box
$draw->setviewbox(0, 0, 200, 200);
   
// Set the image filled color
$draw->setFillColor('green');
$points = [['x' => 40 * 7, 'y' => 10 * 4], 
           ['x' => 70 * 2, 'y' => 50 * 3], 
           ['x' => 60 * 3, 'y' => 15 * 3], ];
   
// Draw the polgon
$draw->polygon($points);
   
// Set the image filled color
$draw->setFillColor('yellow');
   
// Draw the circle
$draw->circle(100, 100, 125, 0);
   
// Create new Imagick object  
$imagick = new \Imagick();
   
// Set the image dimensions
$imagick->newImage(400, 300, 'white');
   
// Set the image format
$imagick->setImageFormat("png");
   
// Draw the image 
$imagick->drawImage($draw);
header("Content-Type: image/png");
   
// Display the image
echo $imagick->getImageBlob();
?>

chevron_right


Output:
setViewBox

Reference: http://php.net/manual/en/imagickdraw.setviewbox.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Shivam_k