Image processing in php (7)

Image processing
Image format
jpeg/jpg
png
is a kind of network image, which adopts lossless compression standard
gif
dynamic image. What can
image processing do?
Drawing,
verification code,
changing
image Figure)
GD library The
GD library itself is an extension, you need to check whether it is enabled or not.
Check PHP extension file C:\xampp\php\ext has php_gd2.dll.
Check whether php_gd2.dll is enabled in php.ini. You can also check through phpinfo.
Drawing
steps
1. Create a background image
imagecreatetruecolor (width, height)
imagecolorallocate (resource, red, green, blue)
imagefill() fills the canvas
Note : imagefill is an area fill, which will only fill the color adjacent to the x and y coordinates and the same color, not Adjacent even if the color is the same, it will not be filled 2. Draw on
the background or enter text




imagettftext (resource, font size, angle, x, y, color, font, written content)   
x, y represent the coordinates of the lower left corner of the first character
To include font files
If you want to write Chinese, you need to find a font that supports Chinese
imagettfbbox() gets the range of the text
imagettfbbox() returns an 8-cell array representing the four corners of the text box
                 0 lower left corner X position 
                1 lower left corner Y position 
                2 lower right corner X position 
                3 lower right corner Y position 
                4 upper right corner X Position 
                5 upper right Y position 
                6 upper left X position 
                7 upper left Y position 
how to get the width and height of the custom font
abs($array[0]-$array[2]); the width of the font
abs($array[7] -$array[1]); Font height 
3, output or save image
imagejpeg()
imagepng()
imagegif()
If no file name is written but a required parameter (resource) is written, it means output to the browser
4. Close resource
imagedestroy()
other
imagecreateformjpeg()
imagecreatefrompng()
imagecreatefromgif()
create a canvas with file
image scale
imagecopyresampled(destination image, source image, destination x-axis 0, destination y-axis 0, source x 0, source y 0, target width, target height, source width, source height)
getimagesize(file name) returns an array of package image information
    0=>width1
    =>height2
    =>file type
        1->gif
        2->jpeg
        3-> png
proportional scaling formula
if(new width && old width < old height){
            new width=(new height/old height)*old width
        }else{
            new height=(new 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 ​​inside, and the other values ​​are smaller than the original value.
2. If black borders are not required, then set the canvas to this new width and height (after scaling) new width, new height)
image watermarking
bool imagecopy ( target image, watermark image, target x , target y , watermark x , watermark y , watermark width, watermark height) target x, y represent the x and y coordinates where the watermark image is placed on the target image. The x of the watermark and the y of the watermark represent the coordinates of the starting point for intercepting the watermark image.
Note
When outputting the image, be sure to pay attention that the format of your editor is utf-8 without bom.
Image format
jpeg/jpg
png
is a kind of network image, which adopts lossless compression standard
gif
dynamic image. What can
image processing do?
drawing,
verification code,
changing image,
image zooming (to prevent the image from being too large and wasting resources), adding watermark (to prevent image theft)
GD library The
GD library itself is an extension, you need to check whether it is enabled or not.
Check PHP extension file C:\xampp\php\ext has php_gd2.dll.
Check whether php_gd2.dll is enabled in php.ini. You can also check through phpinfo.
Drawing
Step
1. create background image
imagecreatetruecolor(width, height)
imagecolorallocate(resource, red, green, blue)
imagefill() fill canvas
Note: imagefill is an area filling, it will only fill the adjacent colors of x and y coordinates and the same color, not adjacent even if the color is the same, it will not be filled
2, draw on the background or input text
imagsetpixel() draw point
imageline() draw line
imagerectangle() rectangle
imageellipse() ellipse
imagettftext(resource, font size, angle, x, y, color, font, write content)   
x, y represent the coordinates of the lower left corner of the first character
to include the font file
if you To write Chinese, you need to find a font that supports Chinese.
imagettfbbox() gets the range of the text
imagettfbbox() returns an 8-unit array representing the four corners of the text frame
                 0 lower left corner X position 
                1 lower left corner Y position 
                2 lower right corner X Position 
                3 Bottom Right Y Position 
                4 Top Right X Position 
                5 Top Right Y Position 
                6 Top Left X Position 
                7 Top Left Y Position 
How to get the width and height of the custom font
abs($array[0]-$array[2]); width of font
abs($array[7]-$array[1]); height of font 
3, output or save image
imagejpeg()
imagepng()
imagegif( )
If you do not write a file name but only write a required parameter (resource), then it means output to the browser
4. Close the resource
imagedestroy()
other
imagecreateformjpeg()
imagecreatefrompng()
imagecreatefromgif()
Create a canvas with 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(filename) returns an array of package image information
    0=> Width
    1=> Height
    2=> Type of file
        1->gif
        2->jpeg
        3->png
Equal scaling formula
if(new width && old width < old height){
            new width=(new height/old height)*old width
        }else{
            new height=(new width/old width)*old height
        }
1. According to this formula, in general, only the width and height are equal to one of the values ​​inside, and the other values ​​are smaller than the original value
2. If black borders are not required , then set the canvas to this new width and height (new width and new height after scaling)
and add watermark to the image
bool imagecopy (target image, watermark image, target x , target y , watermark x , watermark y , watermark width, watermark height) target x, y represent the x and y coordinates where the watermark image is placed on the target image. The x of the watermark and the y of the watermark represent the coordinates of the starting point for intercepting the watermark image.
Note
When outputting the image, be sure to pay attention that the format of your editor is utf-8 without bom.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324673210&siteId=291194637