This PHP code is part of a program I made. I will share it with you now. The function of this Code is to create an image. You can not only write English on the image in a Custom font, but also write Chinese! This Code is applicable to verification code, image watermarking, and other functions.
<? Php
/*
Photozero.net adapted from PHP Reference Document
*/
// Set the Header. If the output image is unsuccessful, remove the Code temporarily to view the error message.
Header ("Content-type: image/png ");
Header ("Cache-Control: no-cache ");
$ Zh_text =;
$ En_text = Test;
// The word to be output
$ Zh_font = simhei. ttf;
$ En_font = arial. ttf;
// File Name of the font. We recommend that you use different Chinese fonts and E-text fonts, because the English in the and libraries are not good.
// TTF fonts are used here. Just Copy a few fonts to the program directory in C: WINDOWSFonts.
// Create a true color image background. The parameter is X length and Y width.
$ Im = imagecreatetruecolor (400,400 );
// Or you can call an existing PNG Image: $ im = imagecreatefrompng(background.png );
// Define several colors
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Gray = imagecolorallocate ($ im, 128,128,128 );
$ Black = imagecolorallocate ($ im, 0, 0, 0 );
// Fill the background in white
Imagefill ($ im, 0, 0, $ white );
// Make the background transparent
Imagecolortransparent ($ im, $ white );
// The above two rows are the key to creating transparent images!
// Many people think that the background color of the image created by imagecreatetruecolor is black and cannot be changed to white.
// First fill the background in white and then perform transparent processing! The test is successful in Firefox3 and IE7!
// This is a simple shadow effect.
// First, the gray font is output a little bit in the lower left corner of the original position to be output, and then written in the black font to have the shadow effect.
// Provide some other tricks next time ~
// Imagettftext ($ im, 20, 0, 11, 21, $ gray, $ en_font, $ en_text );
// Write font
Imagettftext (USD im, 12, 0,100,120, $ black, $ en_font, $ en_text );
Imagepng ($ im );
Imagedestroy ($ im );
// Output the image and clear the cache.
// Functions are built in PHP. For more information, see the document.
?>
The function of the entire code is relatively simple, but this is a basic framework for creating images. Whether it is a verification code or a service that provides a display IP address, it can be used immediately after slight modification. Keep this code for future use.