Thursday, November 20, 2025
HomeLanguagesPHP | ImagickDraw translate() Function

PHP | ImagickDraw translate() Function

The ImagickDraw::translate() function is an inbuilt function in PHP which is used to apply a translation to the current coordinate system. It applies a translation to the current coordinate system which moves the coordinate system origin to the specified coordinate.

Syntax:

bool ImagickDraw::translate( $x, $y )

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

  • $x: This parameter is used to hold the value of x translation coordinate.
  • $y: This parameter is used to hold the value of y translation coordinate.

Return Value: This function does not return any value.

Below programs illustrates the ImagickDraw::translate() function in PHP:

Program 1:




<?php
  
// require_once('path/vendor/autoload.php');
  
// Create a ImagickDraw object to draw into.
$draw = new ImagickDraw();
  
// Set the stroke color
$draw->setStrokeColor('black');
  
// Set the Image Filled Color
$draw->setFillColor('red');
  
// Draw the circle
$draw->circle(250, 250, 100, 150);
   
// Set the Image Filled Color
$draw->setFillColor('green');
  
// Set the translation points
$draw->translate(90, 30);
  
// Draw the circle
$draw->circle(350, 350, 250, 350);
   
// Create new imagick object
$image = new Imagick();
  
// Set the dimensions of the image
$image->newImage(500, 500, 'white');
  
// Set the image format
$image->setImageFormat("png");
  
// Draw the image
$image->drawImage($draw);
header("Content-Type: image/png");
  
// Display the image
echo $image->getImageBlob();
?>


Output:
translate()

Program 2:




<?php
  
// require_once('path/vendor/autoload.php');
   
// Create a ImagickDraw object to draw into.
$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 image filled color 
$draw->setFillColor('green');
   
// Set the translation points
$draw->translate(10, 30);
$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 image filled color 
$draw->setFillColor('blue');
   
// Set the translation points
$draw->translate(10, 30);
$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 image filled color
$draw->setFillColor('yellow');
   
// Set the translation points
$draw->translate(10, 30);
$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);
   
// Create new imagick object
$image = new Imagick();
   
// Set the image dimensions
$image->newImage(400, 400, 'white');
   
// Set the image format
$image->setImageFormat("png");
   
// Draw the image 
$image->drawImage($draw);
header("Content-Type: image/png");
   
// Display the image
echo $image->getImageBlob();
?>


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6778 POSTS0 COMMENTS
Nicole Veronica
11927 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7163 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS