_php of Chinese watermark with GD library function

Source: Internet
Author: User
Tags imagejpeg php website
$im = Imagecreatetruecolor (100,100);//Create a new true color image

$white = Imagecolorallocate ($im, 255,235,255);//Assign a color to an image, which is different from my design knowledge. I never thought that when painting, you should assign a color to the specified image ID first ... This function is often confused with the Imagefill function.

Imagefill ($im, 0,0, $white); Area fill

$black = Imagecolorallocate ($im, 250,50,50);//assigning colors to an image

Imagerectangle ($im, 5,5,50,50, $black);//Draw a rectangle

Header ("Content-type:image/jpeg"); Sends the header, with imagejpeg to output the image directly in the browser.

Imagejpeg ($im, imagename,100);//output a image, naming and quality

Imagedestroy ($im);//releases the memory associated with the image. Images are the image identifiers returned by the image creation function

?>

Using GD Library to realize "Chinese watermark"

Publish time: 2006-12-29 20:50 Author: hy0kl Source: Phpchina Open Source Community Portal

This post first special thanks to Lmhllr, without his guidance I may at this moment still in search data in ing ...

First of all, I am a novice, for the GD library is smattering, not to the place also please forgive me. ^_^

Since the web to see the PHP + GD library can achieve the picture plus "Chinese watermark", began the study of GD Library.

So the reference to download the data changed a script, can implement PHP drawing, was very good at that time N long. (Laughed at!)

After that, the imagestring () function is used to realize the English "watermark" in the image, but when I try to use it to add "Chinese watermark", the printed text is all garbled.

And then began to solve the garbled path.

Long time did not get satisfactory answer, gave up.

Recently, found Lmhllr personality signature is very special, ask after finally know the reason: GD Library to Chinese support is not very good, imagettftext () function character Set if UTF-8, you can pass directly.

In other words, the gb2312 character set must be converted to a UTF-8 character. You can use the Iconv () function to do this. Even the collector version of PHP manual can not find this function, but to the official PHP website to view the Http://cn.php.net/manual/zh/re f.iconv.php, but unfortunately not all functions have Chinese translation, even the E-text is not good, can only look at Lmhllr Guy's example script to guess. This function converts gb2312 characters to UTF-8 characters. Then call the function to implement the "Chinese watermark".

Even after the test of my machine: GD supports the Chinese font has SIMHEI.TTF (bold), Simkai. TTF (in italics), Simfang. TTF (imitation), SimSun. TTC (Arial & XXFarEastFont-Arial) and so on. Of course, the script can only be in English name, eh, who called the operating system is not what we wrote it.

I hope this post can give a little inspiration to the comrades who encounter the same. ^_^

The following is a simple test script.

[PHP]

Header ("Content-type:image/png"); /* Notify browser, to output image */

$im = Imagecreate (400, 300); /* Define the size of the image */

$gray = Imagecolorallocate ($im, 235, 235, 235);

$pink = Imagecolorallocate ($im, 255, 128, 255);

/*

$fontfile = "C:windowsfontssimhei.ttf";

Sorry, this sentence always adhere to the next submission is lost, do not know what is going on, want to test the friends will comment to the current test it

*/

/* $fontfile the path to the font, depending on the operating system, can be Simhei.ttf (bold), Simkai. TTF (in italics), Simfang. TTF (imitation), SimSun. TTC (Arial & XXFarEastFont-Arial) and other GD-supported Chinese fonts */

$str = Iconv (gb2312,utf-8, Chinese watermark!!!); /* Convert the gb2312 character set to UTF-8 characters */

Imagettftext ($im, 0, $pink, $fontfile, $STR);

/* Add Chinese watermark */

Imagepng ($im);

Imagedestroy ($im);

? >[/php]

OK, call it a day, I hope this is my first quality of the water posts.

Here is the effect of this example. Once again, thank you for LMHLLR!!!

http://www.bkjia.com/PHPjc/486450.html www.bkjia.com true http://www.bkjia.com/PHPjc/486450.html techarticle $im = Imagecreatetruecolor (100,100);//Create a new true color image $white = Imagecolorallocate ($im, 255,235,255);//Assign a color to an image, This is different from my design knowledge ...

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