GD library usage summary --- 2, gd summary --- 2_PHP tutorial

Source: Internet
Author: User
Tags image flip imagecopy imagejpeg rtrim
GD database usage summary --- 2, gd summary --- 2. GD database usage summary --- 2, gd summary --- 2 and the previous article. The GD library can be used in a lot of ways. of course, it must be related to drawing. in addition to the verification code and watermark, the GD library of the image can also be used as a summary --- 2, gd summary --- 2

Next, go to the previous article. The GD library can be used in a lot of ways. of course, it must be related to drawing. in addition to the verification code and watermark above, it can also perform operations such as scaling, cropping, and rotating the image, this can be seen in many applications.

1. add a watermark

As we already know, we can use imagechar or imagestring to draw characters or strings (or even Chinese characters) to the image to achieve the purpose of the watermark. There is also a better way, not only can we add character watermarks, but also image watermarks: imagecopy.

Prototype: bool imagecopy (resource $ dst_im, resource $ src_im, int $ dst_x, int $ dst_y, int $ src_x, int $ src_y, int $ src_w, int $ src_h ), the name indicates that this is a copy. the 1st and 2 parameters are the target image handle and source file handle. when adding a watermark, if the watermark image is a small image, add it to a large image, the first parameter is the big image handle, and the second parameter is the small image handle. 3rd and 4 parameters are the x and y coordinate values of the watermark on the target image. 5th and 6 parameters are the x and y coordinate values starting from the watermark image, 7th and 8 parameters indicate that the watermark image is about to be used as the width and height of the watermark. Therefore, this method means that the coordinates of the vertices in the upper-left corner of the watermark image src_im are (src_x, src_y, copy the src_w and src_h parts of the width and height to the dst_im image, and then draw the dst_im image to the canvas to save or output the image. the code is as follows:

 '; Return false;} // Obtain the width and height list of the original image and watermark image ($ srcWidth, $ srcHeight) = getimagesize ($ srcFile); list ($ markWidth, $ markHeight) = getimagesize ($ markFile); // The watermark image cannot be larger than the original image pixel. if ($ markWidth> $ srcWidth | $ markHeight> $ srcHeight) {return false ;} // obtain the original image handle and watermark image handle to be added to the watermark $ dstImg = imagecreatefromjpeg ($ srcFile); $ markImg = imagecreatefrompng ($ markFile); // position of the watermark, put $ dst_x = $ srcWidth-$ markWidth; $ dst_y = $ srcHeight-$ markHeight; // get the file information $ fileinfo = pathinfo ($ srcFile ); if (empty ($ dstFile) {$ dstFile = rtrim ($ fileinfo ['dirname'], DS ). DS. 'Mark _'. $ fileinfo ['filename']. date ('ymdhis '). mt_rand (1, 10002.16.'.jpeg ';} // Copy the watermark image to an existing image. imagecopy ($ dstImg, $ markImg, $ dst_x, $ dst_y, 0, 0, $ srcWidth, $ srcHeight); // Save the newly added watermark image to imagejpeg ($ dstImg, $ dstFile); imagedestroy ($ dstImg); imagedestroy ($ markImg); return true ;} $ srcFile = 'G: \ wamp \ www \ html \ image \ p125.jpg '; // original image $ markFile = 'G: \ wamp \ www \ html \ image \ ooopic_5.png '; // watermark image watermark ($ srcFile, $ markFile );

Effect:

Here, the watermark image is simply placed in the bottom right corner of the image, so it requires a simple calculation to put it in the bottom right corner of the image. when imagecopy is called, the vertex from the top left corner of the watermark image (coordinates 0, 0) copy all (the width and height of the watermark image) to the image to be added, and then draw and save the image with imagejpeg. Note that it is similar to imagejpeg (this is simple to use) when the second parameter is input by the drawing function, the image is saved as a file instead of output to the browser. Therefore, you do not need to call the header function to send the header information.

Of course, we also need to figure out which is the source file (src) and the target File (dst). when we add a small image to a large image, the source file is a small image watermark and copy it to the big image, A large image is a target.

Images can be used as watermarks because the second parameter of imagecopy is the image handle. Therefore, you can create an image (such as imagecreatefromjpeg) from an existing image ), of course, you can also create a character image handle variable from an existing character --- use the imagecreatefromstring method, so this is more common.

2. image scaling

In many applications, the image list is a thumbnail. when you click a thumbnail, the entire image is displayed. this involves the processing of image shrinking. There are two methods available: imagecopyresized and imagecopyresampled. The difference is that the latter ressample the image (which seems to be my professional term ), therefore, the quality of the image is better (the method used for re-sampling may become more scum:

Bool imagecopyresampled (resource $ dst_image, resource $ src_image, int $ dst_x, int $ dst_y, int $ src_x, int $ src_y, int $ dst_w, int $ dst_h, int $ src_w, int $ src_h)

The first and second parameters are similar to the preceding ones. how can we scale them? For example, the coordinates of the vertices in the upper left corner of the original image are (src_x, src_y), src_w and src_h, and the coordinates of the target image are (dst_x, dst_y), where the width and height are dst_w and dst_h. Therefore, if the width and height of the source image on the target image are small and small, the version is reduced, of course, the coordinates of the two top-left vertices must be the same. take jpg as an example:

 1) & $ percent = 0.5; $ newWidth = floor ($ width * $ percent); // The scaled down width and height $ newHeight = floor ($ height * $ percent ); $ dstImg = imagecreatetruecolor ($ newWidth, $ newHeight); // Create a canvas with a new image width and height $ srcImg = imagecreatefromjpeg ($ srcFile ); // Create a canvas from the original image file $ pathinfo = pathinfo ($ srcFile); if (! $ DstFile) $ dstFile = rtrim ($ pathinfo ['dirname'], DS ). DS. 'zoom _'. $ pathinfo ['filename']. date ('ymdhis '). mt_rand (1, 1000 ). ". jpeg "; // zoom out from the top left vertex of the source file // imagecopyresized ($ dstImg, $ srcImg, 0, 0, 0, 0, $ newWidth, $ newHeight, $ width, $ height); imagecopyresampled ($ dstImg, $ srcImg, 0, 0, 0, 0, $ newWidth, $ newHeight, $ width, $ height ); // draw and save the image imagejpeg ($ dstImg, $ dstFile); // destroy the resource imagedestroy ($ dstImg); imagedestroy ($ srcImg); return true ;} // test $ srcFile = 'G: \ wamp \ www \ html \ image \ p179.jpg '; zoomPic ($ srcFile );

Effect:

Here, we can store the source image and the small image in two sets. The list shows the small image, and the low-level check shows the original image.

3. crop images

Consider the above imagecopyresampled method. if it is not from the upper left corner of the original image, but from the lower right corner of the upper left corner, the width and height of the cut Image are equal to the full width and height of the source file, but partial width and height, the new ressample image is a part of the source image, which achieves the cropping effect. Therefore, the cropping and scaling methods are the same.


 $ SrcWidth) & $ width = $ srcWidth; ($ height + $ y)> $ srcHeight) & $ height = $ srcHeight; ($ width <= 0) & $ width = $ srcWidth; ($ height <= 0) & $ height = $ srcHeight; $ dstImg = imagecreatetruecolor ($ width, $ height ); // switch ($ type) {case IMG_GIF: $ srcImg = imagecreatefromgif ($ srcFile); // Obtain the source file resource handle and extension for processing, to use appropriate functions and extensions $ ext = 'GIF'; $ imagefun = 'imagegif'; break; case IMG_JPG: $ srcImg = imagecreatefromjpeg ($ srcFile ); $ ext = 'jpeg '; $ imagefun = 'imagejpeg'; break; default: $ srcImg = imagecreatefrompng ($ srcFile); $ ext = 'PNG '; $ imagefun = 'imagepng '; break;} // you can specify the path for saving the cut file. $ fileinfo = pathinfo ($ srcFile); if (empty ($ dstFile )) {$ dstFile = rtrim ($ fileinfo ['dirname'], DS ). DS. 'Cut _'. $ fileinfo ['filename']. date ('ymdhis '). mt_rand (1, 1000 ). ". {$ ext} ";}// run the cut operation imagecopyresampled ($ dstImg, $ srcImg, 0, 0, $ x, $ y, $ width, $ height, $ width, $ height); // draw on the canvas and save the file $ imagefun ($ dstImg, $ dstFile); imagedestroy ($ dstImg); imagedestroy ($ srcImg); return true ;} // test $ srcFile = 'G: \ wamp \ www \ html \ image \ p221.jpg '; cutPic ($ srcFile, 50, 50, 50 );

Effect:

A common application is that when we change an avatar for an application, the Avatar is too big and we will use cropping. This allows us to perform a simulation.

4. rotating images

Image rotation is also very common, mainly used in the imagerotate function. prototype: resource imagerotate (resource $ image, float $ angle, int $ bgd_color [, int $ ignore_transparent = 0]), the first parameter is the image handle to be rotated, the second parameter is the rotation angle value, and the third parameter specifies a color, that is, the color that is used when there is no space after rotation, the fourth parameter specifies a transparent color. the default value 0 indicates that the transparent color is retained. This method returns a new image handle, that is, the rotated image resource variable, which can be drawn and saved. Note that the rotation angle can be specified to be between 0 and, which is counter-clockwise. take jpg as an example:

 '; Return false ;}$ srcImg = imagecreatefromjpeg ($ srcFile); // address of the saved file for processing $ fileinfo = pathinfo ($ srcFile); if (empty ($ dstFile )) {$ dstFile = rtrim ($ fileinfo ['dirname'], DS ). DS. 'rotate _'. $ fileinfo ['filename']. date ('ymdhis '). mt_rand (1, 10002.16.'.jpeg ';} $ white = imagecolorallocate ($ srcImg, 0xff, 0xff, 0xf1); // execute the rotation, note that the direction is counter-clockwise $ dstImg = imagerotate ($ srcImg, $ angular, $ white); // draw the image to the canvas and save the imagejpeg ($ dstImg, $ dstFile) file; imagedestroy ($ dstImg); imagedestroy ($ srcImg); return true ;} // test the $ srcFile = 'G: \ wamp \ www \ html \ image \ p219.jpg '; rotatePic ($ srcFile, 220 );

Effect: after the source image is rotated

5. Image flip

This is not so common in applications. The so-called flip refers to performing a mirror flip on the image, for example, taking the vertical line in the middle of the image as the axis, switching from the left to the right, and switching from the right to the left. Imagine the case where the vertical line in the middle is the axis of symmetry, the pixels on the y axis remain unchanged, and the pixels on the x axis are reversed right and right. you can still use the imagecopy method. when copying source files to the target image, in this case, rotate the image around the y axis. jpg is used as an example.

 

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.