Generating image text and mixed images is usually used for verification code. Let's take a look at an example of generating image text and mixed images in php. I hope this will be helpful to you. generating image text and mixed images is usually used for verification code. Let's take a look at an example of generating image text and mixed images in php. I hope this will be helpful to you.
Script ec (2); script
Example 1: image text Synthesis
$ Im = imagecreatetruecolor (400, 30); // create a canvas of 400 30 pixels
$ White = imagecolorallocate ($ im, 255,255,255 );
$ Gray = imagecolorallocate ($ im, 128,128,128 );
$ Black = imagecolorallocate ($ im, 0, 0, 0 );
Imagefilledrectangle ($ im, 0, 0,399, 29, $ white); // outputs a rectangle filled with white as the background
// If there is a Chinese output, it needs to be transcoded, converted to a UTF-8 string can be passed directly
// $ Text = iconv ("GB2312", "UTF-8", "Recall classic ");
$ Text = "Chinese aflasjflasjflasjf ";
// Set the font and copy the font corresponding to simsun. ttc in the system to the current directory.
$ Font = 'simsun. TTC ';
Imagettftext ($ im, 20, 0, 12, 21, $ gray, $ font, $ text); // output a gray string as the shadow.
Imagettftext ($ im, 20, 0, 10, 20, $ black, $ font, $ text); // output a black string in the shadow.
Header ("Content-type: image/png ");
Imagepng ($ im );
Imagedestroy ($ im );
Exit;
?>
Example 2
// Font size
$ Size = 30;
// Font type. In this example, the font type is .
$ Font = "c:/windows/fonts/simsun. ttc ";
// Displayed text
$ Text = "www.111cn.net ";
// Create a blank image with a length of 500 to 80
$ Img = imagecreate (500, 80 );
// Assign color to the image
Imagecolorallocate ($ img, 0xff, 0xcc, 0xcc );
// Set the font color
$ Black = imagecolorallocate ($ img, 0, 0, 0 );
// Write ttf text to the image
Imagettftext ($ img, $ size, 0,100, 50, $ black, $ font, $ text );
// Sending header information
Header ('content-Type: image/gif ');
// Output image
Imagegif ($ img );
?>
Note:
The GD2 extension library in php is an excellent image processing library that allows you to create images of various formats. It has powerful functions!
Enable GD2: Find the php configuration file php. ini and search extension = php_gd2.dll to remove the preceding ";