How does the GD library in PHP contribute to creating graphics?
The GD library in PHP contributes to creating graphics by providing functions to create and manipulate images. It allows developers to draw shapes, text, and apply various effects to images programmatically. This library is commonly used for generating dynamic images, such as charts, graphs, thumbnails, and captcha codes.
// Example code snippet using GD library to create a simple image with text
$width = 200;
$height = 100;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $bgColor);
imagestring($image, 5, 50, 40, 'Hello, GD!', $textColor);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);