Php-PHP source code

Source: Internet
Author: User
Tags image processing library
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 ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.