Friday, October 10, 2025
HomeLanguagesPHP | ImagickDraw affine() Function

PHP | ImagickDraw affine() Function

The ImagickDraw::affine() function is an inbuilt function in PHP which is used to adjust the current affine transformation matrix.

Syntax:

bool ImagickDraw::affine( array $affine )

Parameters: This function accepts a single parameter $affine which holds the array containing the affine matrix parameters.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

Below given programs illustrate the ImagickDraw::affine() function in PHP:

Program 1:




<?php
// Create a new Imagick object
$imagick = new Imagick();
   
// Create a image on imagick object with 
// green background
$imagick->newImage(800, 250, 'green');
   
// Create a new ImagickDraw object
$draw = new ImagickDraw();
   
// Translate the object
$draw->translate(100, 100);
   
// Apply the affine() function
$draw->affine(array("sx" => 7, "sy" => 1,
                    "rx" => 9, "ry" => 0,
                    "tx" => 200, "ty" => 0));
   
// Draw a rectangle
$draw->rectangle(-50, -50, 50, 50);
   
// Render the draw commands in the ImagickDraw object
$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 with 
// purple background
$imagick->newImage(800, 250, 'purple');
  
// Create a new ImagickDraw object
$draw = new ImagickDraw();
  
// Translate the object
$draw->translate(100, 100);
  
// Apply the affine() function
$draw->affine(array("sx" => 2, "sy" => 1, 
                    "rx" => 0, "ry" => 0, 
                    "tx" => 0, "ty" => 0));
  
// Draw a rectangle
$draw->rectangle(0, -50, 50, 50);
  
// Draw a rectangle
$draw->rectangle(100, 50, 150, 100);
  
// Draw a rectangle
$draw->rectangle(200, -10, 250, 90);
  
//  Render the draw commands in the ImagickDraw object
$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.affine.php

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS