GdImage objects rather than resources. They are released automatically when no longer referenced; imagedestroy() is unnecessary on PHP 8 and deprecated as of PHP 8.5.The GD extension can create and edit JPEG, PNG, GIF, WebP and other supported formats. Check that the extension is loaded, create a true-color image, draw on it, then output or save the result.
<?php
if (!extension_loaded('gd')) {
exit('GD extension is not enabled.');
}
$image = imagecreatetruecolor(320, 120);
$background = imagecolorallocate($image, 245, 245, 245);
$text = imagecolorallocate($image, 30, 60, 120);
imagefilledrectangle($image, 0, 0, 319, 119, $background);
imagestring($image, 5, 20, 45, 'PHP GD', $text);
header('Content-Type: image/png');
imagepng($image);
?>| Functions | Details |
| ImageRotate() | Rotate Image |
| ImageString() | Add text string to image |
| Imagettftext() | Add text of any font size to image |
| Imagerectangle() | Drawing rectangles |
| imagecopyresampled() | resize and copy an image |
| Imagearc() | Drawing Arc and circles |
| Imagefilledarc() | Drawing filled Arc, circles and Pie chart |
| Imageellipse() | Drawing ellipse and circles |
| Imagefilledellipse() | Drawing Filled ellipse and circles |
| Imagefilledrectangle() | Drawing Filled rectangles and bar chart |
imagecopy() and imagecopyresampled() functions?Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.