GD library functions implement Chinese watermarks

Source: Internet
Author: User
Tags image identifier imagejpeg
$ Imimagecreatetruecolor (100,100); // create a true color image $ whiteimagecolorallocate ($ im, 255,235,255); // assign color to an image, which is different from my design knowledge .. I have never thought of assigning color to the specified Image logo before painting .. this letter

$ Im = imagecreatetruecolor (100,100); // create a true color image

$ White = imagecolorallocate ($ im, 255,235,255); // assign color to an image, which is different from my design knowledge .. I have never thought of assigning color to the specified Image logo before painting .. this function is often mixed with the imagefill function.

Imagefill ($ im, $ white); // fill in the area

$ Black = imagecolorallocate ($ im, 50, 50); // assign color to an image

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

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

Imagejpeg ($ im, ImageName, 100); // output an image, name and quality

Imagedestroy ($ im); // release the memory associated with the image. Image is the image identifier returned by the image creation function.

?>

Implement "Chinese watermark" using GD Library"

Author: hy0kl Source: PHPChina open source community portal

In this post, I would like to thank lmhllr Ren Xiong. without his guidance, I may still be searching for information ......

I declare that I am a newbie and I have little knowledge about the GD Library. if not, please forgive me. ^_^

Since I saw that the php + GD library can add "Chinese watermarks" to images on the Internet, I started learning about the GD Library.

So I changed a script with reference to the downloaded materials to implement php plotting. at that time, I was so fond of chatting for a long time. (laugh !)

After that, the imagestring () function is used to implement an English "watermark" on the image. However, when you try to add a "Chinese watermark" to the image, all the output is garbled.

As a result, he began to embark on the road to solving garbled text.

I gave up without a satisfactory answer for a long time.

Recently, I found that lmhllr's personalized signature is very special, after the query finally knows the reason: GD library for Chinese support is not very good, imagettftext () function character set if UTF-8, it can be passed directly.

In other words, you must convert the character set of gb2312 to the character set of the UTF-8. you can use the iconv () function. even the collector version of PHP Manual can not find this function, but you can go to the php official website to view the http://cn.php.net/manual/zh/ref.iconv.php, but unfortunately not all functions have a Chinese translation, even the E text is not good, I can only guess from the lmhllr example script. this function converts the character of gb2312 to the character of the UTF-8. then, call the function to add a "Chinese watermark ".

Even after the test, the Chinese fonts supported by GD are simhei. ttf (), SIMKAI. TTF (entity body), SIMFANG. TTF (Imitation Song), SIMSUN. TTC (&) and so on. of course, only English names can be entered in the script. who is the operating system.

I hope this post will inspire the comrades who have encountered the same thing. ^_^

The following is a simple test script.

[Php]

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

$ Im = imagecreate (400,300);/* define the image size */

$ Gray = ImageColorAllocate ($ im, 235,235,235 );

$ Pink = ImageColorAllocate ($ im, 255,128,255 );

/*

$ Fontfile = "C: WINDOWSFontsSIMHEI. TTF ";

Sorry, this sentence is lost when it is stuck to the last commit. I don't know what is going on. if you want to test it, I will comment it out and test it now.

*/

/* $ Fontfile font path, depending on the operating system, can be simhei. ttf (), SIMKAI. TTF (entity body), SIMFANG. TTF (Imitation Song), SIMSUN. chinese fonts supported by GD, such as TTC (& */

$ Str = iconv (GB2312, UTF-8, Chinese Watermark !!!); /* Convert the gb2312 character set to the UTF-8 character set */

ImageTTFText ($ im, 30, 0, 50,140, $ pink, $ fontfile, $ str );

/* Add a Chinese watermark */

Imagepng ($ im );

ImageDestroy ($ im );

?> [/Php]

OK, close the job. I hope this is my first quality water Post.

The following is the result of this example. Thanks again for lmhllr !!!

Related Article

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.