How can PHP be used to create dynamic images?

PHP can be used to create dynamic images by utilizing the GD library, which provides functions for creating and manipulating images. By using functions like imagecreate() to create a new image, and imagefilledrectangle() to draw shapes or text onto the image, developers can generate images on-the-fly based on user input or other dynamic data.

<?php
// Create a blank image
$image = imagecreate(200, 200);

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

// Draw a rectangle
$rectangle_color = imagecolorallocate($image, 0, 0, 255);
imagefilledrectangle($image, 50, 50, 150, 150, $rectangle_color);

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

// Free up memory
imagedestroy($image);
?>