How can the positioning of X and Y coordinates impact the functionality of imagefilledrectangle in PHP?

The positioning of X and Y coordinates in the imagefilledrectangle function in PHP determines where the filled rectangle will be drawn on the image. If the coordinates are not correctly specified, the rectangle may not be visible or may be drawn in the wrong location. To ensure the rectangle is drawn in the desired position, make sure to specify the coordinates accurately based on the image dimensions.

// Create a new image
$image = imagecreate(200, 200);

// Set the background color
$bg_color = imagecolorallocate($image, 255, 255, 255);

// Set the rectangle color
$rect_color = imagecolorallocate($image, 0, 0, 0);

// Define the coordinates for the rectangle
$x1 = 50;
$y1 = 50;
$x2 = 150;
$y2 = 150;

// Draw a filled rectangle on the image
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $rect_color);

// Output the image
header('Content-type: image/png');
imagepng($image);

// Free up memory
imagedestroy($image);