This PHP code is part of a program I do, now post it for sharing. The function of this code is to create a picture, not only can use the custom font in the picture to write in English, can also write Chinese! This code is applicable to the verification code, the image watermark and other functions.
1.<?php
2./*
3.photozero.net adapted from PHP reference documents
4.*/
5.
6.//Set Header, if the output picture is unsuccessful, please temporarily remove this code to view the error message
7.header ("Content-type:image/png");
8.header ("Cache-control:no-cache");
9.
$zh _text = ' blackbody ';
$en _text = ' Test ';
12.//the word to be output
13.
$zh _font = ' Simhei.ttf ';
$en _font = ' Arial.ttf ';
The file name of the 16.//font. It is suggested that the Chinese font and e text are not the same, because the song body, bold font in the English language is not a drop
17.//here are all TTF fonts. Lazy to find the Internet to C:\WINDOWS\Fonts, copy a few fonts to the program directory is good
18.
19.//Create a picture background with a true color, with an x length and y width.
$im = Imagecreatetruecolor (400, 400);
21.//or can invoke existing PNG pictures, etc.: $im = imagecreatefrompng (' background.png ');
22.
23.//defines a few colors
$white = Imagecolorallocate ($im, 255, 255, 255);
$grey = Imagecolorallocate ($im, 128, 128, 128);
$black = imagecolorallocate ($im, 0, 0, 0);
27.
28.//Fill Background is white
29.imagefill ($im, 0, 0, $white);
30.//the background transparent
31.imagecolortransparent ($im, $white);
32.//the above two lines is the key to creating transparent pictures!
33.//Many people reaction, Imagecreatetruecolor created after the picture, the background color is black, no matter how the modification can not become white
34.//I first fill the background with white, then do transparent processing on it! Tested successfully under FIREFOX3 and IE7!
35.
36.//This is a very simple effect of creating a shadow.
37.//first in the original to output the position of the lower left corner of a little bit of output gray font, and then use the black font to write up a shadow effect
38.//the next time to provide some other tricks ~
39.//imagettftext ($im, 0, one, $grey, $en _font, $en _text);
40.
41.//Write Font
42.imagettftext ($im, 0, _font, $black, $en, $en _text);
43.
44.imagepng ($im);
45.imagedestroy ($im);
46.//Needless to say, output the picture, and then clear the cache
47.
48.
49.//functions are built in PHP, if you do not know, please refer to the documentation.
50.?>
The entire code has a relatively simple function, but it is a basic framework for creating pictures. Whether to do the verification code or to provide services to display IP, as long as a slight modification will soon be able to use. Just keep the code for a while, and then maybe use it someday.