Add text (Chinese supported) to the image // PHP function-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? Php *************************************** * function: add a text parameter to the image: $ img image file name $ new_img save another image file name, if it is null, it means no other image $ text string content text_size string size text_angle font string output angle tex script ec (2); script

//************************************** **//
// Function: add text to the image
// Parameter: $ img Image File Name
// $ New_img saves another image file name. If it is null, it means no other image is saved.
// $ Text string content
// Text_size string size
// Text_angle font string output Angle
// Output x coordinate of text_x string
// Output y coordinate from text_y string
// $ Text_font-shaped file name
// $ R, $ g, $ B string color RGB Value
//************************************** **//
Function img_text ($ img, $ new_img, $ text, $ text_size, $ text_angle, $ text_x, $ text_y, $ text_font, $ r, $ g, $ B ){

$ Text = iconv ("gb2312", "UTF-8", $ text );
Header ("Content-type: image/gif ");
$ Im = @ imagecreatefromstring (file_get_contents ($ img) or die ("opening the image failed! ");
$ Color = ImageColorAllocate ($ im, $ r, $ g, $ B );

// ImageTTFText (int im, int size, int angle, int x, int y, int col, string fontfile, string text ):
// This function writes TTF (TrueType Fonts) text to an image.
// Parameter: The size is the font size;
// Angle is the font angle, which is calculated clockwise. 0 degrees is horizontal (from left to right), and 90 degrees is the bottom-to-top text;
// The x and y parameters are the coordinate values of the text (the origin is the upper left corner );
// Col indicates the color of the word;
// Fontfile is the font file name;
// Text is the string content.
ImageTTFText ($ im, $ text_size, $ text_angle, $ text_x, $ text_y, $ color, $ text_font, $ text );

If ($ new_img = ""):
ImageGif ($ im); // do not save the image, only show
Else:
ImageGif ($ im, $ new_img); // Save the image but it is not displayed
Endif;

ImageDestroy ($ im); // ends the image and releases the memory space.
}

?>

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.