Talking about PHP drawing (d) (end)

Source: Internet
Author: User
To the front, we have been able to use GD to complete the basic needs of mapping. But sometimes I'm afraid I'll imagestring
The five fonts you can use are small and ugly, and you'll need to use the following function. This function allows us to use TTF fonts;
You must have files for these fonts.

<?php
Header ("Content-type:image/png");
$im = Imagecreate (400,250);
$col _back = imagecolorallocate ($im, 136,200,152);
$col _write = imagecolorallocate ($im, 255,255,255);
$col _black = imagecolorallocate ($im, 0,0,0);

Imagettftext ($im, 160,15,40,220, $col _black, "C:/windows/fonts/verdana.ttf", "PNG");
The new content is only this one sentence. The argument is this:
$im say no more. 160 This position, is the font size (PT). 15 string is tilt angle, horizontal direction counterclockwise.
40,220 is the horizontal ordinate. Notice, unlike imagestring,
The coordinates specified in the imagestring are the upper-left corner of the string, and the imagettftext specified is the lower-left corner.
Next $col _black is a color,
"C:/windows/fonts/verdana.ttf" is the font file path, in Linux is "/.../...".
It can even be "http://...". However, I have not used it, nor do I recommend it.
Because the things that are not on their machines are unreliable after all, they cannot be entrusted with heavy responsibilities.
The last is the string to output. This is especially noteworthy,
The string here should be encoded with UTF-8!!!
ASCII code 0~127 characters, ASCII code equals UTF-8 encoding, so we do not need to convert the Latin string when we output it.
And if you want to export Chinese, you need a series of transformations.
Www.phpx.com's sadly writes a GB2312 code to the UTF-8 code conversion function.
My other article is devoted to analyzing how this function works.

Imagepng ($im);
Imagedestroy ($im);
?>

Similar to Imagefontwidth () and Imagefontheight () help us calculate what the imagestring output string will occupy
Height and width, imagettfbbox can help us compute the Imagettftext output string. Its return value is a
8 members of the array, respectively (note this order) lower left, lower right, upper right, upper left horizontal ordinate. Try this:

?
$p =imagettfbbox (160,0, "C:/windows/fonts/verdana.ttf", "PNP");
for ($i =0; $i <8; $i +=2)
echo "(". $p [$i]. ",". $p [$i +1]. "." <br> ";
?>

The result is this:
(15,-1)
(306,-1)
(306,-117)
(15,-117)

Why is there a negative number? I don't know. What are these coordinates relative to? Whatever it was relative to what they were between the
The relative position is not changed. So, these are not very important, we are based on the left and right axis of the difference between the upper and lower ordinate
, it's enough to figure out where to put the lower left.

All right, that's it. Using TTF font output string, also by the way to solve the front "to write Chinese characters still have to cost some trouble"
Legacy. In my opinion, GD has the last piece of content-open the existing picture, processing, and re-output.

First of all, get the picture information, is the basic need. Look at the following example:
?
$im =imagecreatefrompng ("Test.png");
This is the opening of an existing image.
Quite simply, the parameter is the picture path, and the return value is the image ID.

echo "The image ' s width is". Imagesx ($im). ", and height is." Imagesy ($im). ".";
Imagesx () and Imagesy, respectively, get the width and height of the image, and they all need only one parameter-the image ID that has been opened.

?>

In addition, there is a function that does not belong to the GD library to obtain image information: getimagesize.

?
$p =getimagesize ("Test.png");
For ($i =0 $i <4; $i + +) echo $p [$i]. " <br> ";

?>

The result is this:
50
100
3
Width= "100" height= "

Visible, the function returns an array of information about the picture, with four elements: the first two are the width and height of the image;
The third represents the format of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP. Last one
The element is used in HTML to represent the image wide and high string (it's really convenient!). )。 For example, we can use this:

?
....
$pic _name= "...";
$pic _size=getimagesize ($pic _name);

?>
"<?echo $pic _size[3];? >>

.....

Again, this function does not belong to the GD library, so, in the absence of the GD library installed in the PHP environment can also be used!!


Thumbnails are features that are often required. Using GD's copy and resize function makes it easy to implement thumbnails.

?
Suppose you want to shrink (enlarge) A picture of any size to 200 pixels wide.
Header ("Content-type:image/jpeg");
$pic _name= "Test.jpg";
$ims =imagecreatefromjpeg ($pic _name);
Open the original artwork.

$owidth =imagesx ($ims);
$oheight =imagesy ($ims);
Get the width and height of the original image.

$nheight =round ($oheight *200.0/$owidth);
Calculates the height of the new diagram.

$IMT =imagecreate ($nheight);
Create a new diagram.

Imagecopyresized ($IMT, $ims, 0,0,0,0,200, $nheight, $owidth, $oheight);
Copy to new diagram and resize.
This function has more arguments, first, the ID of the destination image and the original image,
Then the four parameters are the coordinates of the destination image and the copy position of the original image.
As from the 20,30 of the original image to the 10,0 of the destination image
Then these four parameters are 10,0,20,30.
The next four and finally four parameters are the width of the copy area of the destination image and the original image,
such as from the original image copy 100x50 so large area to the destination image and reduced to 50x25
Then these four parameters are 50,25,100,50.
And here I use the parameter is to copy the original image completely to the new diagram, (copy position all is 0,0)
Adjust the original size to a width of 200 pixels, whether magnified or reduced.
$nheight is the width, height, $owidth of the new picture, $oheight is the width and height of the original image.
When using this function, notice that in each set of parameters,
The parameters associated with the destination image are in the front, and the parameters associated with the original image are later.

Imagejpeg ($IMT);
Imagedestroy ($IMT);
Imagedestroy ($IMS);
?>


Well, speaking of which, my experience in doing GD process is all over. Thank you for your attention! Let us make progress together!


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.