Current Location: Home> Latest Articles> imagecreatetruecolor() + imageopenpolygon(): Create an image from scratch

imagecreatetruecolor() + imageopenpolygon(): Create an image from scratch

M66 2025-05-29

In PHP, image processing functions are provided by the GD library, imagecreatetruecolor() and imageopenpolygon() are two of the very useful functions. This article will show you how to create an image from scratch using these two functions and draw a simple polygon. We will walk through step by step how to create an image, set the image color, and render the graphics using the polygon method.

1. Create an image using imagecreatetruecolor()

imagecreatetruecolor() is a very basic function to create a true color image resource. It receives two parameters: the width and height of the image, and returns a resource representing the image.

 <?php
// Create a wide 500 high 500 Images
$width = 500;
$height = 500;
$image = imagecreatetruecolor($width, $height);

The $image variable returned by this function is the image resource, and you can use it to perform image processing operations next.

2. Assign colors

In images, we usually need to set different colors to draw shapes, backgrounds, etc. Imagecolorallocate() can be used to assign colors. It accepts 4 parameters: image resource, red, green and blue values ​​(0-255).

 // Assign background color and polygon color
$background_color = imagecolorallocate($image, 255, 255, 255);  // White background
$polygon_color = imagecolorallocate($image, 0, 0, 255);        // Blue polygon

3. Fill the background color

Use imagefill() to fill the entire image with the specified color. For example, after we create the image, we want to fill a white background.

 // Fill background color
imagefill($image, 0, 0, $background_color);

4. Draw polygons using imagepolygon()

Next, we draw a polygon using imagepolygon() . The parameters of this function include image resources, vertex coordinate arrays, and vertex numbers. Each vertex of a polygon is represented by a coordinate (x, y).

 // Vertex coordinates of polygons
$points = array(
    150, 150,
    350, 150,
    400, 350,
    200, 400,
    100, 300
);

// Draw polygons
imagepolygon($image, $points, 5, $polygon_color);

5. Output image

Finally, we need to output the created image. PHP provides functions such as imagepng() , imagejpeg() and imagegif() . You can choose one of them to output images according to your needs. Here we use imagepng() to save the image as PNG format.

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

// Or save as a file
// imagepng($image, 'polygon_image.png');

6. Free up resources

Once the image is generated, in order to save memory, image resources should be released.

 // Destroy image resources
imagedestroy($image);

Complete code example

 <?php
// Create image resources
$width = 500;
$height = 500;
$image = imagecreatetruecolor($width, $height);

// Assign colors
$background_color = imagecolorallocate($image, 255, 255, 255);  // White background
$polygon_color = imagecolorallocate($image, 0, 0, 255);        // Blue polygon

// Fill background color
imagefill($image, 0, 0, $background_color);

// Vertex coordinates of polygons
$points = array(
    150, 150,
    350, 150,
    400, 350,
    200, 400,
    100, 300
);

// Draw polygons
imagepolygon($image, $points, 5, $polygon_color);

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

// Destroy image resources
imagedestroy($image);
?>