Php drawing skills how to write Chinese and English on pictures

Source: Internet
Author: User
Php drawing skills how to write Chinese and English on pictures

  1. // 1. create a canvas

  2. $ Im = imagecreatetruecolor (300,200); // create a real-color image. the default background is black and the image identifier is returned. Another function, imagecreate, is not recommended.
  3. $ Red = imagecolorallocate ($ im, 255, 0, 0 );

  4. // 2. write

  5. $ Str = "hello, world ";
  6. Imagestring ($ im, 5, 30, 60, $ str, $ red); // parameter description: 5-indicates the size of the text. Function imagestring cannot write Chinese characters

  7. // 3. output image

  8. Header ("content-type: image/png"); // bbs.it-home.org
  9. Imagepng ($ im); // output to the page. If the second parameter [, $ filename] exists, the image is saved.

  10. // 4. destroy images and release memory

  11. Imagedestroy ($ im );
  12. ?>

Method 2: write Chinese.

  1. // 1. create a canvas

  2. $ Im = imagecreatetruecolor (300,200); // create a real-color image. the default background is black and the image identifier is returned. Another function, imagecreate, is not recommended.
  3. $ Red = imagecolorallocate ($ im, 255, 0, 0 );

  4. // 2. write

  5. $ Str = iconv ("gb2312", "UTF-8", "Beijing, you are early! Hello, world "); // The file format is gbk, and here to the uft-8 format, in order to normal output, otherwise it is garbled. Unknown
  6. Imagettftext ($ im, 12, rand (20,100), $ red, "simhei. ttf", $ str );

  7. // 3. output image

  8. Header ("content-type: image/png ");
  9. Imagepng ($ im); // output to the page. If the second parameter [, $ filename] exists, the image is saved.

  10. // 4. destroy images and release memory

  11. Imagedestroy ($ im );
  12. ?>

The imagettftext () function is far better than the imagestring () function, mainly manifested in: 1. imagettftext () can output both Chinese and English, and can specify the font; imagestring () can only output English, you can only use the default font. 2. the imagettftext () font size can be infinitely large; imagestring () font size can only be 1 ~ 5. 3. the font output by imagettftext () can be changed to an angle. imagestring () can only be output horizontally.

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.