php Editor Xiaoxin will introduce to you how to use PHP to output GD2 images to a browser or file. The GD library is a graphics library for PHP that can be used to create and process images. Through the GD library, we can generate verification codes, thumbnails, watermarks, etc. Output images using the GD library can be displayed directly in the browser or saved as a file. Next, we will introduce in detail how to use PHP combined with the GD library to implement this function.
PHP Output GD2 image to browser or file
The GD2 library inphp provides rich functionality for creating, editing and outputting images. Here's how to output a GD2 image to a browser or file:
Output to browser
imagecreate()
function. imagestring()
, imageline()
and other functions to draw text, line segments and other content. header()
function to set the correct MIME type, for example Content-Type: image/png
. imagepng()
, imagejpeg()
and other functions to output the image to the browser. Code example:
<?php // create image $image = imagecreate(200, 100); //Set background color $white = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $white); // draw text $black = imagecolorallocate($image, 0, 0, 0); imagestring($image, 5, 50, 50, "Hello World!", $black); //Output the image to the browser header("Content-Type: image/png"); imagepng($image); // Release image resources imagedestroy($image); ?>
Output to file
imagepng()
, imagejpeg()
to save the image to a file. Code example:
<?php // create image $image = imagecreate(200, 100); //Set background color $white = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $white); // draw text $black = imagecolorallocate($image, 0, 0, 0); imagestring($image, 5, 50, 50, "Hello World!", $black); //Save image to file imagepng($image, "image.png"); // Release image resources imagedestroy($image); ?>
Other notes
imageinterlace()
function to enable progressive display of images. imagescale()
function. gd_info()
function to query GD library information. The above is the detailed content of PHP output GD2 image to browser or file. For more information, please follow other related articles on the PHP Chinese website!