PHP uses the GD library to implement the method of adding text on a picture (code)

Source: Internet
Author: User
This article brings you the content is about PHP using the GD library to implement the image of the method of adding text (code), there is a certain reference value, the need for a friend can refer to, I hope you have some help.

We can use the PHP GD Library extension to process our images, for example, to generate thumbnails, crop images, and this chapter will say in the picture to add text.

1. First of all we need to accept the text we need: $key = $_get[' key '), specifically, to receive the value of the anti-SQL processing and other operations here is not introduced.

2. If the text affixed directly to the GD library, will not be in line with our requirements, because the general directly affixed to the word spacing is not, we will see by very close, affecting our aesthetic and customers do not need, so we need to docking received content to split, The following will use a regular wrapper function to split the Chinese string into an array.

/** * Splits a string into     arrays * @param string  $str String * @return       array */function mb_str_split ($str) {    return PR Eg_split ('/(? <!^) (?! $)/U ', $str);}

3. The above function will be able to split the text into an array, next we will use the GD library to add a word array of words on the picture

<?phpfunction index () {     $img = $back = Path_root. " 1.jpg ";//Picture with path     $str = $_get (' key '),//Receive value     $text = $this->mb_str_split ($STR);//split value into array     $size = 40;// Font size     $font = path_root. " /1.ttf ";//Load font ttf     $img = imagecreatefromjpeg ($img);//Load existing image     $black = Imagecolorallocate ($img, 24, 165, 234); /Set the color to blue     $x =355;//The first word of the horizontal axis     $red = Imagecolorallocate ($img, 255,255,255);//Create a white background     imagefilledrectangle ( $img, 340,40,420,120, $red);//Build a rectangular     foreach ($text as $k = $v) {         imagettftext ($img, $size, 0, $x, 100, $ Black, $font, $v);//Loop add text         $x = $x + 70;//Increase the horizontal axis to achieve the effect of spacing     }     $time = rand (1,10000). Time (). ". PNG ";//define Picture name     imagepng ($img, Path_root.") /". $time);//Save Picture}function Mb_str_split ($str) {     return Preg_split ('/(? <!^) (?! $)/U ', $str);}

So we can add the received text to the picture, but also save it.

If the header (' content-type:image/png ') is added; The picture will be displayed in the browser

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.