What is the purpose of using GD functions in PHP for image manipulation?
When working with images in PHP, the GD library provides a set of functions that allow for image manipulation such as resizing, cropping, adding text, and applying filters. By using GD functions, developers can dynamically generate or modify images on the fly, making it easier to create thumbnails, watermarks, or other image effects on websites.
// Example of resizing an image using GD functions
$sourceImage = 'original.jpg';
$destinationImage = 'resized.jpg';
list($sourceWidth, $sourceHeight) = getimagesize($sourceImage);
$desiredWidth = 200;
$desiredHeight = 200;
$source = imagecreatefromjpeg($sourceImage);
$destination = imagecreatetruecolor($desiredWidth, $desiredHeight);
imagecopyresampled($destination, $source, 0, 0, 0, 0, $desiredWidth, $desiredHeight, $sourceWidth, $sourceHeight);
imagejpeg($destination, $destinationImage);
imagedestroy($source);
imagedestroy($destination);