The content of this article is about image processing in php, which has certain reference value. Now I share it with you. Friends in need can refer to it
Image processing
Image format
jpeg/jpg
png
is a A kind of network image, using lossless compression standard
gif
Dynamic pictures
What can image processing do
Draw a picture
Verification code
Change the picture
Scaling of the image (to prevent the image from being too large and wasting resources), adding a watermark ( Prevent image theft)
GD library
The GD library itself is an extension, you need to check whether it is turned on
View PHP extension file C: \xampp\php\extWhether there is php_gd2.dll
Check whether php_gd2.dll is enabled in php.ini or you can check it through phpinfo
drawing
Steps
1. Create a background image
imagecreatetruecolor(width, height)
imagecolorallocate(resource, red, Green, blue)
imagefill() fills the canvas
Note: imagefill is area filling, it will only fill the colors adjacent to the x and y coordinates and the same color, Even if the colors are not adjacent, they will not be filled
2. Draw on the background or enter text
imagsetpixel() to draw dots
imageline() line drawing
imagerectangle() rectangle
imageellipse() ellipse
imagettftext(resource, font size, angle, x , y, color, font, written content)
x, y represents the coordinates of the lower left corner of the first character
To include the font file
If you want to write Chinese, you need to find a font that supports Chinese
imagettfbbox() gets the range of the text
imagettfbbox() returns a font containing 8 The four corners of the text frame represented by a cell array X position of the lower right corner
3 Y position of the lower right corner
4 X position of the upper right corner
5 Y position of the upper right corner
6 X position of the upper left corner
7 Y position of the upper left corner
How to get the width and height of a custom font
abs($array[0]-$array[2]); Font width
abs($array[7]-$array[1]); Font height
3. Output or save the image
imagejpeg()
imagepng()
imagegif()
If the file name is not written but a required parameter (resource) is written, it means output to the browser
4. Close the resource
imagedestroy ()
Other
imagecreateformjpeg()
imagecreatefrompng()
imagecreatefromgif()
Create a canvas from a file
Image scaling
imagecopyresampled(target image, source image, target x-axis 0, target y-axis 0, source x 0, source y 0, target width, target height, source Width, source height)
getimagesize(file name) Returns an array containing image information
0=>Width
1=>Height
2=>Type of file
1->gif
2->jpeg
3->png
Equal scaling formula
If (Xinkuka & & & LT; Old High) {
新 = (New high/old high)*Old width
} Else {
新 = (New Width/Old Width/Old Width )*Old height
}
1. According to this formula, under normal circumstances, only the width and height are equal to one of the values, and the other values are smaller than the original values
2. If you don’t need black borders, then just set the canvas to the new width and new height (new width and new height after scaling)
Image watermark
bool imagecopy (target image, watermark image, target x, target y, watermark x, watermark y, watermark width, watermark height) target x, y means that the watermark image is placed at x and y of the target image coordinate. The x and y of the watermark represent the coordinates of the starting point of the watermark image.
Note
When outputting the image, be sure to pay attention to the format of your editor: UTF-8 BOM-free format.
Related recommendations:
The above is the detailed content of Image processing in php. For more information, please follow other related articles on the PHP Chinese website!