PHP creates (Chinese/English text) images

Source: Internet
Author: User
Create a PHP (Chinese/English text) image and read the PHP creation (Chinese/English text) image. This PHP code is part of a program I have created. I will post it and share it with you. 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/* Pho "> <LINKhref =" http://www.p

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.

/*
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 = 'simhei ';
$ 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. If you are too lazy to access the internet, just go to C: \ WINDOWS \ Fonts and Copy a few Fonts to the program directory.
// 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.

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.