As far as before, we have been able to use GD to complete the basic needs of the mapping. But sometimes I am afraid IMagestring
The five fonts can be used less and ugly, then use the following functions. This function allows us to use TTF fonts; but you
Must have files for these fonts.
PHP
Header ("Content-Type: Image / PNG");
$ IM = ImageCreate (400, 250);
$ colorAllocate ($ IM, 136, 200, 152);
$ colorAllocate ($ IM, 255, 255, 255);
$ colorAllocate ($ IM, 0, 0, 0);
ImageTfText ($ IM, 160, 15, 40, 220, $ COL_BLACK, "C: /Windows/fonts/verdana.ttf", "png");
// The new content is only this sentence. The parameter is like this:
// $ IM doesn't have to say it. 160 This location is a font size (PT). 15 strings are tilt angles and a counterclockwise in the horizontal direction.
// 40, 220 is a horizontal unit. Note that it is different from imageString,
The coordinates specified in ImageString are the upper left corner of the string, and the coordinates specified by the imagettftext are the upper left corner.
// Next $ col_black is a color,
// "C: /Windows/fonts/verdana.ttf" is a font file path, in Linux is "/ ... / .....".
// can even be "http: // ....". However, I have not used this, nor I recommend this.
// Because it is unreliable because things that are not in their own machine, it is not possible to commit compensation.
// The last is the string to output. This is especially not to pay attention,
// The string here should be encoded with UTF-8! ! !
// ASCII code 0 ~ 127 characters, the ASCII code is equal to UTF-8 encoding, so we do not need to conversion when outputting a Western string.
// If you want to output Chinese, a range of conversions are required.
// www.phpx.com 's SADLY writes a function of GB2312 code to UTF-8 code conversion.
// I have another article specially analyzed the working principle of this function.
ImagePNG ($ IM);
ImageDestroy ($ IM);
?>
Similar to ImageFontWidth () and ImageFontHeight () help us calculate the imageString output string will take up
Height and width, imagettfbox can help us calculate the situation of imagetftext output strings. Its return value is one
8 Members of the array, are (note this order), lower left, right, upper right, upper left horizontal length coordinates. try it:
$ P = imageTfbbox (160, 0, "C: /Windows/fonts/verdana.ttf", "PNP");
For ($ I = 0; $ i <8; $ i = 2)
echo "(". $ P [$ I]. ",". $ P [$ I 1]. ")" "" "
";
?>
The result is this:
(15, -1)
(306, -1)
(306, -117)
(15, -117)
Why is there a negligible number? I do not know either. What these coordinates are relative? Whether it is relative to what, between them
The relative position will not change. So, these are not important. We are enough to calculate what the left point is arranged by the difference between the left and right abscissa.
Ok, I have finished using the TTF font output string, but in the way, I solved the front "To write Chinese characters to pay some trouble"
Left handle. In my opinion, GD remains the last part - Open the existing picture, process, and re-output.
First, the information obtained is the basic needs. Look at the example below:
$ IM = ImageCreateFromNG ("Test.png");
// This is to open an image already existing.
// is very simple, the parameter is a 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 image consisted are wide and high, they only need a parameter - the image ID that has been opened.
?>
In addition, there is a function of acquiring image information that does not belong to the GD library: GetImagesize.
$ P = GetImagesize ("Test.png");
For ($ I = 0; $ I <4; $ I ) ECHO $ P [$ I]. "
";
?>
The result is this:
50
100
3
Width = "50" height = "100"
It can be seen that the function returns an array of information about the image, and the four elements are: the first two are the width, high;
The third representative image format: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP. the last one
The element is used in HTML indicates that the image is wide, high strings (it is too convenient!). For example, we can use this:
....
$ PIC_NAME = "...."
$ PIC_SIZE = GetImagesize ($ PIC_NAME);
?>
>
.....
Repeat again, this function is not a GD library, so it can also be used in the PHP environment without the GD library! !
Thumbnails are frequently required features. A thumbnail can be easily implemented using the "copy and adjustment size" of GD.
/ / Suppose you want to reduce the image of any size (zoom) to the width 200 pixels.
//Header ("Content-Type: Image / JPEG");
$ PIC_NAME = "Test.jpg";
$ IMS = ImageCreateFromJPEG ($ PIC_NAME);
// Open the original picture.
$ width = Imagesx ($ IMS);
$ OHEIGHT = ImageESY ($ IMS);
// Get the width and high of the original map.
$ nHeiGHT = ROUND ($ OHEIGHT * 200.0 / $ OWIDTH);
// Calculate the height of the new map.
$ IMT = ImageCreate (200, $ NHEIGHT);
// Establish a new picture.
ImageCopyResized ($ IMT, $ IMS, 0, 0, 0, 0, 200, $ NHEIGHT, $ OWIDTH, $ OHEIGHT); // Copy to the new map and resize.
// This function parameter is more, first, it is the ID of the destination image and the original image.
// The four parameters are then the coordinates of the purpose image and the copy position of the original image.
// To copy the 20, 30 of the original image to the destination image 10,0
// The four parameters are 10, 0, 20, 30.
// The four of the four also the last four parameters are the aspects of the purpose image and the original image of the original image.
// Copy 100X50 from the original image to the destination image and shrink to 50x25
// The four parameters are 50, 25, 100, 50.
// The parameters I used here are to copy the original map to the new map, (copy position is 0, 0)
// adjust the original size whether zoom in, regardless of zooming or shrinking, and is adjusted to a wide 200 pixel.
// 200, $ nHeiGHT is the width, high, $ OWIDTH, $ OHEIGHT is the width, high.
// Pay attention to each group of parameters when using this function.
// is the parameters related to the image of the destination image, and the parameters associated with the original image are behind.
Imagejpeg ($ IMT);
ImageDestroy ($ IMT);
ImageDestroy ($ IMS);
?>
Ok, say this, my experience in the process of doing the GD is finished. Thank you for your attention! Let us make progress together!