Home  >  Article  >  Backend Development  >  GD library function to implement Chinese watermark_PHP tutorial

GD library function to implement Chinese watermark_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:38:54960browse

 $im = imagecreatetruecolor(100,100);//Create a new true color image

 $white = imagecolorallocate($im,255,235,255);//Assigning a color to an image is different from my design common sense. I have never thought that when painting, I must first assign a specified image Logo assigns color. This function is often confused with the imagefill function.

Imagefill($im,0,0,$white); //Area filling

 $black = imagecolorallocate($im,250,50,50);//Assign a color to an image

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

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

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

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

 ?>

Use GD library to implement "Chinese watermark"

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

First of all, I would like to express my special thanks to lmhllr, my friend. Without his guidance, I might still be searching for information...

First of all, I would like to state that I am a newbie and have little knowledge of the GD library. Please forgive me for any shortcomings. ^_^

Since I saw on the Internet that php + GD library can add "Chinese watermark" to images, I started to learn the GD library.

So I modified a script based on the downloaded information, which can realize PHP drawing. I was very happy for a long time. (Laughing!)

Then I used the imagestring() function to implement an English "watermark" on the image; but when I tried to use it to add a "Chinese watermark", all that was printed was garbled.

So I started on the road to solve the garbled code again.

I gave up after not getting a satisfactory answer for a long time.

Recently, I discovered that lmhllr’s personalized signature is very special. After asking, I finally found out the reason: the GD library does not support Chinese very well. If the character set of the imagettftext() function is UTF-8, it can be passed directly.

In other words, the character set of gb2312 must be converted into UTF-8 characters. You can use the iconv() function to achieve this. I can’t find this function in the collector’s edition PHP manual, but you can go to the official PHP website to view http ://cn.php.net/manual/zh/ref.iconv.php, but unfortunately not all functions have Chinese translations. My E language is not good, so I can only guess by looking at the example script of lmhllr. This is The function converts gb2312 characters into UTF-8 characters. Then the function is called to add "Chinese watermark".

After testing on my machine, I found that the Chinese fonts supported by GD include simhei.ttf (Heoldbody), SIMKAI.TTF (Kaili), SIMFANG.TTF (Imitation Song Dynasty), SIMSUN.TTC (Song Dynasty & New Song Dynasty), etc. Of course, only English names can be substituted into the script. Hey, we didn’t write the operating system.

I hope this post can give some inspiration to comrades who have experienced the same thing as me. ^_^

The following is a simple test script.

 [php]

Header("Content-type: image/png"); /*Notify the browser to output an 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 is always lost after being pasted and submitted. I don’t know what happened. Friends who want to test it, please leave the comment and test it now

 */

 /*$fontfile The path of the font, depending on the operating system, can be simhei.ttf (Helvetica), SIMKAI.TTF (Italian), SIMFANG.TTF (Imitation Song Dynasty), SIMSUN.TTC (Song Dynasty & New Song Dynasty), etc. Chinese fonts supported by GD*/

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

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

 /* Add Chinese watermark */

Imagepng($im);

ImageDestroy($im);

 ?>[/php]

OK, call it a day, I hope this is my first quality post.

The following is the effect of this example. I would like to express my sincere thanks to lmhllr again!!!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486450.htmlTechArticle$im = imagecreatetruecolor(100,100);//Create a true color image $white = imagecolorallocate($im,255,235,255 );//Assigning a color to an image is different from my design common sense...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn