GD database usage summary --- 2, gd summary --- 2

Source: Internet
Author: User
Tags image flip imagecopy imagejpeg transparent color rtrim

GD database usage 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:

<? Php date_default_timezone_set ('Asia/Shanghai'); define ('ds ', DIRECTORY_SEPARATOR); // Add watermarks to text and image watermarks. Use the image function watermark ($ srcFile = '', $ markFile = '', $ dstFile ='') {if (! File_exists ($ srcFile) |! File_exists ($ markFile) {echo 'file not exists! <Br/> '; return false;} // obtain the list of the width and height of the original image and the 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. $ dstImg = imagecreatefromjpeg ($ srcFile); $ markImg = imagecreatefrompng ($ markFile ); // position of the watermark, which is simply placed in the lower right corner $ 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:

<? Php date_default_timezone_set ('Asia/Shanghai'); define ('ds ', DIRECTORY_SEPARATOR ); // zoom out the image/*** @ param src original image path * @ param percent zoom out ratio * @ param dstFile Save the image path */function zoomPic ($ srcFile = '', $ percent = 0.5, $ dstFile = '') {if (! File_exists ($ srcFile) {return false;} list ($ width, $ height) = getimagesize ($ srcFile ); // obtain the width and height ($ percent <= 0 | $ percent> 1) & $ percent = 0.5; $ newWidth = floor ($ width * $ percent ); // The reduced width and height $ newHeight = floor ($ height * $ percent); $ dstImg = imagecreatetruecolor ($ newWidth, $ newHeight ); // create a canvas with the width of the new image $ 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.


<? Php date_default_timezone_set ('Asia/Shanghai'); define ('ds ', DIRECTORY_SEPARATOR); // cut a picture function cutPic ($ srcFile = '', $ x = 0, $ y = 0, $ width = 16, $ height = 16, $ dstFile = '') {if (! File_exists ($ srcFile) {return false;} list ($ srcWidth, $ srcHeight, $ type) = getimagesize ($ srcFile); $ x <0 & $ x = 0; $ y <0 & $ y = 0; // set the width and height ($ width + $ x)> $ srcWidth) & $ width = $ srcWidth; ($ height + $ y)> $ srcHeight) & $ height = $ srcHeight; ($ width <= 0) & $ width = $ srcWidth; ($ height <= 0) & $ height = $ srcHeight; $ dstImg = imagecreatetruecolor ($ width, $ height); // switch ($ type) of the target file resource) {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 ;} // set the path to save 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:

<? Php date_default_timezone_set ('Asia/Shanghai'); define ('ds ', DIRECTORY_SEPARATOR ); /*** image rotation ** @ param angular rotation angle value 0-*/function rotatePic ($ srcFile = '', $ angular = 0, $ dstFile = '') {if (! File_exists ($ srcFile) {echo 'file not exists <br/> '; return false;} $ srcImg = imagecreatefromjpeg ($ srcFile ); // process the saved file address $ 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.

<? Php // image flip date_default_timezone_set ('Asia/Shanghai'); define ('ds', DIRECTORY_SEPARATOR); // flip along the Y axis, x coordinate value logarithm function turnY ($ srcFile = '', $ dstFile ='') {if (! File_exists ($ srcFile) {return false;} // get the original image handle and width/height $ srcImg = imagecreatefromjpeg ($ srcFile); $ srcWidth = imagesx ($ srcImg ); $ srcHeight = imagesy ($ srcImg); $ dstImg = imagecreatetruecolor ($ srcWidth, $ srcHeight); // flip along the Y axis, right and right of the pixel on the X axis for ($ I = 0; $ I <$ srcWidth; $ I ++) {imagecopy ($ dstImg, $ srcImg, $ srcWidth-$ I-1, 0, $ I, 0, 1, $ srcHeight );} // process the image save path $ fileinfo = pathinfo ($ srcFile); if (empty ($ dstFile) {$ dstFile = rtrim ($ fileinfo ['dirname'], DS ). DS. 'turnx _'. $ fileinfo ['filename']. date ('ymdhis '). mt_rand (1, 10002.16.'.jpeg ';} // draw an image and save the imagejpeg ($ dstImg, $ dstFile); imagedestroy ($ dstImg); imagedestroy ($ srcImg); return false ;} // test $ srcFile = 'G: \ wamp \ www \ html \ image \ p311.jpg '; turnY ($ srcFile );

Effect: after turning

It is mainly for loop, get the source file width $ srcWidth, if the source file on the coordinate is ($ I, 0), then corresponding to the target image coordinate ($ srcWidth-$ I-1, 0), and then copy the resource whose width is 1 pixel, and the height is the entire height of the source file $ srcHeight. After the loop is completed, all the resources are copied to an image, it is equivalent to drawing a line.

I have no intention of browsing the manual. I can see that the imageflip function guessed this function. I think it is true, but I have been reading the book for a few years. The prototype is bool imageflip (resource $ image, int $ mode). The first parameter is the target image resource, and the second parameter is the flip mode. You can use the enumeration variable that comes with php, including IMG_FLIP_HORIZONTAL (horizontal) and IMG_FLIP_HORIZONTAL (vertical), IMG_FLIP_BOTH (horizontal and vertical), one function can be implemented.

It's quite simple, so it's better to be a trainer:-D

 


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.