In PHP, image processing is a very practical and powerful feature. By rationally using functions in the GD library, such as imageopenpolygon() and imagecopymerge() , complex image synthesis effects can be easily achieved. This article will introduce the use of these two functions step by step and demonstrate their practical application through a simple example.
imageopenpolygon() is a function used to draw open polygons (i.e. polygon lines that are not connected to the end) on an image. The basic syntax is as follows:
bool imageopenpolygon ( GdImage $image , array $points , int $num_points , int $color )
$image : Image resource.
$points : an array containing coordinate points, such as [x1, y1, x2, y2, ...] .
$num_points : The number of points.
$color : The color of the line is drawn.
Note: Unlike imagepolygon() , imageopenpolygon() does not automatically connect the start and end points.
imagecopymerge() is used to merge parts of one image onto another image and can specify transparency. The basic syntax is as follows:
bool imagecopymerge(
GdImage $dst_image,
GdImage $src_image,
int $dst_x, int $dst_y,
int $src_x, int $src_y,
int $src_width, int $src_height,
int $pct
)
$dst_image : Target image resource.
$src_image : Source image resource.
$dst_x, $dst_y : The coordinates of the starting point where the source image is placed on the target image.
$src_x, $src_y : The starting point to copy the source image.
$src_width, $src_height : The width and height of the copy.
$pct : Merged transparency (0 = completely transparent, 100 = completely opaque).
Here is a complete example, we will use imageopenpolygon() to draw a simple graph and combine it onto a background image with imagecopymerge() :
<?php
// Create a background image
$background = imagecreatetruecolor(400, 300);
$white = imagecolorallocate($background, 255, 255, 255);
imagefill($background, 0, 0, $white);
// Create a foreground map(Polygonal graph)
$polygon = imagecreatetruecolor(200, 200);
$transparent = imagecolorallocatealpha($polygon, 0, 0, 0, 127);
imagefill($polygon, 0, 0, $transparent);
imagesavealpha($polygon, true);
// Define the point of a polygon
$points = [
50, 50,
150, 50,
150, 150,
50, 150
];
// Assign colors
$red = imagecolorallocate($polygon, 255, 0, 0);
// Draw open polygons
imageopenpolygon($polygon, $points, count($points) / 2, $red);
// Synthesize images
imagecopymerge($background, $polygon, 100, 50, 0, 0, 200, 200, 70);
// Output image to browser
header('Content-Type: image/png');
imagepng($background);
// Free up resources
imagedestroy($background);
imagedestroy($polygon);
?>
In this example, we first create an image with a white background, then create a foreground map with a transparent background, use imageopenpolygon() to draw a red open square outline on the foreground map, and finally merge this foreground map with 70% transparency through imagecopymerge() .
If you want to save the final result as a file instead of outputting to the browser, just replace imagepng($background); with:
imagepng($background, '/path/to/your/folder/merged_image.png');
Of course, here /path/to/your/folder/ path can be replaced with your own server address, such as:
imagepng($background, 'https://m66.net/uploads/merged_image.png');
Through the combination of imageopenpolygon() and imagecopymerge() , we can easily draw various open graphics and flexibly blend them on any background image to achieve rich visual effects. This technology can be widely used in avatar synthesis, watermark production, dynamic graphics processing and other scenarios.