The following example shows how to create a 200x100 pixel canvas, fill it with a blue background, and finally output the image in PNG format.
<?php
// Create a 200x100 pixel canvas
$image = imagecreate(200, 100);
// Allocate a color for the canvas (RGB)
$background_color = imagecolorallocate($image, 0, 0, 255); // Blue
// Output HTTP header to inform the browser this is a PNG image
header('Content-Type: image/png');
// Output the image
imagepng($image);
// Free up resources
imagedestroy($image);
?>
Why set the header?
By default, PHP scripts output HTML text. Setting header('Content-Type: image/png') informs the browser of the image type so it displays correctly.
Palette-based images vs True color images
imagecreate creates a palette-based image with limited colors. For richer colors, use imagecreatetruecolor.
Supported Formats
You can output images in PNG, JPEG, or GIF formats using imagepng(), imagejpeg(), and imagegif() respectively.
imagecreate is an entry-level function in PHP for creating basic image canvases, suitable for simple image generation and manipulation. Once you're familiar with it, you can explore other powerful features in the GD library to create dynamic images and complex graphics.
For more PHP image processing techniques, visit https://gitbox.net/php-image-processing for detailed information.