PHP Create (Chinese/English characters) Picture _php tutorial

Source: Internet
Author: User

This PHP code is part of a program that I do, and now post it to share. The function of this code is to create a picture, not only can use a custom font in the picture to write English, can also write Chinese! This Code applies to the verification code, to the picture watermark and so on function.

/*
Photozero.net adapted from PHP reference documentation
*/

Set the header, if the output image is not successful, please temporarily remove this code, see the error message
Header ("Content-type:image/png");
Header ("Cache-control:no-cache");

$zh _text = blackbody;
$en _text = Test;

The word to output
$zh _font = Simhei.ttf;
$en _font = Arial.ttf;

The file name of the font. Suggested that the Chinese font and the E-text body with a different, because the song body, boldface font in English is really not a drop
The TTF font is used here. Too lazy to find on the internet to c:windowsfonts, copy a few fonts to the program directory is good
Create a true-color picture background with the parameters x length and y width.
$im = Imagecreatetruecolor (400, 400);

Or you can call an existing PNG image, etc.: $im = Imagecreatefrompng (background.png);
Define several colors
$white = Imagecolorallocate ($im, 255, 255, 255);
$grey = Imagecolorallocate ($im, 128, 128, 128);
$black = imagecolorallocate ($im, 0, 0, 0);

Fill background is white
Imagefill ($im, 0, 0, $white);

Transparency of the background
Imagecolortransparent ($im, $white);

The above two lines are the key to creating a transparent picture!
Many people react, Imagecreatetruecolor created after the picture, the background color is black, no matter how the changes can not become white
I'm going to fill the background with white, and then do the transparent processing! Tested successfully under FIREFOX3 and IE7!
This is a very simple effect of creating shadows.
In the original to output the position of the lower left corner of a little output gray font, and then write in black font has a shadow effect
Next time to provide some other tricks ~
Imagettftext ($im, 0, one, one, $grey, $en _font, $en _text);

Write font
Imagettftext ($im, 0, $black, $en _font, $en _text);
Imagepng ($im);
Imagedestroy ($im);

Needless to say, output pictures, and then clear the cache
Functions are built in PHP, if you do not understand, please refer to the documentation.
?>

The functionality of the entire code is relatively single, but this is a basic framework for creating a picture. Whether it is to do a verification code or to provide a service to display IP, as long as a slight modification will soon be used. Just keep this code spare, and someday it might be used.

http://www.bkjia.com/PHPjc/508258.html www.bkjia.com true http://www.bkjia.com/PHPjc/508258.html techarticle This PHP code is part of a program that I do, and now post it to share. The function of this code is to create a picture, not only can use a custom font in the picture to write English, ...

  • 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.