Open In App

PHP | ImagickDraw pathLineToRelative() Function

The ImagickDraw::pathLineToRelative() function is an inbuilt function in PHP which is used to draw a line path from the current point to the given coordinate using relative coordinates. Then the coordinate becomes the new current point. The initial point can be set using pathMoveToRelative() function.

Syntax:



bool ImagickDraw::pathLineToRelative( float $x, float $y )

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

Return Value: This function returns TRUE on success.



Below programs illustrate the ImagickDraw::pathLineToRelative() 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();
  
// Set the fill color
$draw->setFillColor('black');
  
// Create a path
$draw->pathStart();
  
// Draw a line
// Start coordinates of line
$draw->pathMoveToRelative(350, 50);
  
// End coordinates of line
$draw->pathLineToRelative(50, 150);
  
// End the path
$draw->pathFinish();
  
// 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();
  
// Set the fill color
$draw->setFillColor('yellow');
  
// Set the stroke color
$draw->setStrokeColor('black');
  
// Set the stroke width
$draw->setStrokeWidth(1);
  
// Create a path
$draw->pathStart();
  
// Draw a star
$draw->pathMoveToRelative(350, 20); 
$draw->pathLineToRelative(40, 60); 
$draw->pathLineToRelative(80, 20); 
$draw->pathLineToRelative(-80, 30); 
$draw->pathLineToRelative(30, 60); 
$draw->pathLineToRelative(-50, -20); 
$draw->pathLineToRelative(-50, 40); 
$draw->pathLineToRelative(10, -70); 
$draw->pathLineToRelative(-50, -10); 
$draw->pathLineToRelative(50, -30); 
$draw->pathLineToRelative(20, -80); 
  
// End the path
$draw->pathFinish();
  
// 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.pathlinetorelative.php


Article Tags :