PHP thumbnails and image watermark creation,
1. Start
Thumbnails are often used in the process of uploading images to websites. Here I wrote an Image class for Image processing, which can generate thumbnails and add watermarks.
2. How to generate a thumbnail
The key to generating a thumbnail is how to calculate the zoom ratio.
Here, based on several common changes in image proportional scaling and width height, I come up with an algorithm for calculating the scaling ratio by dividing the width and height of the new image (that is, the thumbnail) by the width and height of the original image, if the value is large, use it as the zoom ratio:
Zoom ratio = Max ({New Image Height/Original Image Height, new image width/Original Image Width })
That is:
If (New Image Height/Original Image Height)> (new image width/original image width )){
Zoom ratio = New Image Height/original image height;
} ELSE {
Zoom ratio = new image width/original image width;
}
Here we will list the image scaling scenarios of the scenario and the processing method:
E. g
Scenario 1: when the source image is larger than the new image, the scaling ratio = the width of the new image/the width of the original image:
Scenario 2: when the source image is larger than the new image, B. Zoom ratio = New Image Height/source Image Height:
Scenario 3: When the source image is larger than the new image, and the width and height of the new image are equal, that is, the shape of the new image is square, the scaling algorithm above is also applicable.
Scenario 4: If "new image width> = original image width" and "New Image Height> = Original Image Height", the image is not scaled or enlarged to keep the original image.
Scenario 5: If "new image width <original image width" and "New Image Height> = Original Image Height" are selected, set "New Image Height = Original Image Height" and then cut the image.
Scenario 6: If "New Image height <Original Image Height" and "new image width> = original image width" are selected, set "new image width = original image width" before cutting.
3. How to add a watermark image
It is easy to add a watermark. I didn't consider it as complicated here. It mainly controls the watermark position in the bottom right corner of the image and the watermark size in the image. For example, when the target image is close to the watermark image size, you need to scale the watermark image proportionally and then add the watermark image.
The two images on the left are the original images, and the following is the watermark image. A new watermark is added after the scaling on the right.
4. Class Diagram
5. PHP code
5.1. construct ()
In the Image class, except that the construct _ construct () is public, all other functions are private. that is, in function _ construct (), you can directly generate a thumbnail and add a watermark image. If you only generate a thumbnail without adding a watermark, set the parameter $ markPath in _ construct () to null.
Where, "$ this-> quality = $ quality? $ Quality: 75; "controls the image quality (0-100) when the output is a jpg image. The default value is 75;
/*** Image constructor. * @ param string $ imagePath image path * @ param string $ markPath watermark image path * @ param int $ new_width thumbnail width * @ param int $ new_thumbnail height * @ param int $ quality jpg image grid output quality */public function _ construct (string $ imagePath, string $ markPath = null, int $ new_width = null, int $ new_height = null, int $ quality = 75) {$ this-> imgPath = $ _ SERVER ['document _ root']. $ imagePath; $ this-> waterMarkP Ath = $ markPath; $ this-> newWidth = $ new_width? $ New_width: $ this-> width; $ this-> newHeight = $ new_height? $ New_height: $ this-> height; $ this-> quality = $ quality? $ Quality: 75; list ($ this-> width, $ this-> height, $ this-> type) = getimagesize ($ this-> imgPath ); $ this-> img = $ this-> _ loadImg ($ this-> imgPath, $ this-> type); // generate a thumbnail $ this-> _ thumb (); // Add a watermark image if (! Empty ($ this-> waterMarkPath) $ this-> _ addWaterMark (); // output image $ this-> _ outputImg ();}
Note: Use a thumbnail to add a watermark image to the new image.
5.2. Create a thumbnail function _ thumb ()
/*** Thumbnail (crop based on the specified width and height in an equal proportion) */private function _ thumb () {// if the source image itself is smaller than the thumbnail, if ($ this-> newWidth> $ this-> width) $ this-> newWidth = $ this-> width; if ($ this-> newHeight> $ this-> height) $ this-> newHeight = $ this-> height; // The height of the background image. $ gd_width = $ this-> newWidth; $ gd_height = $ this-> newHeight; // If the width and height of the thumbnail are the same as the width and height of the source image, crop if ($ gd_width = $ this-> width | $ gd_height = $ this-> height) {$ this-> newWidth = $ this-> width; $ this-> newHeight = $ this-> height;} else {// calculate the zoom ratio $ per = 1; if ($ this-> newHeight/$ this-> height)> ($ this-> newWidth/$ this-> width )) {$ per = $ this-> newHeight/$ this-> height;} else {$ per = $ this-> newWidth/$ this-> width ;} if ($ per <1) {$ this-> newWidth = $ this-> width * $ per; $ this-> newHeight = $ this-> height * $ per ;}} $ this-> newImg = $ this-> _ CreateImg ($ gd_width, $ gd_height, $ this-> type); imagecopyresampled ($ this-> newImg, $ this-> img, 0, 0, 0, 0, $ this-> newWidth, $ this-> newHeight, $ this-> width, $ this-> height );}
The thumbnail function _ thumb () is encoded according to the preceding analysis.
5.3. Add the watermark image function _ addWaterMark ()
/*** Add watermark */private function _ addWaterMark () {$ ratio = 1/5; // watermark zoom ratio $ Width = imagesx ($ this-> newImg ); $ Height = imagesy ($ this-> newImg); $ n_width = $ Width * $ ratio; $ n_height = $ Width * $ ratio; list ($ markWidth, $ markHeight, $ markType) = getimagesize ($ this-> waterMarkPath); if ($ n_width> $ markWidth) $ n_width = $ markWidth; if ($ n_height> $ markHeight) $ n_height = $ markHeight; $ Img = $ this-> _ loadImg ($ this-> waterMarkPath, $ markType); $ Img = $ this-> _ thumb1 ($ Img, $ markWidth, $ markHeight, $ markType, $ n_width, $ n_height); $ markWidth = imagesx ($ Img); $ markHeight = imagesy ($ Img ); imagecopyresampled ($ this-> newImg, $ Img, $ Width-$ markWidth-10, $ Height-$ markHeight-10, 0, 0, $ markWidth, $ markHeight, $ markWidth, $ markHeight); imagedestroy ($ Img );}
To add a watermark image, use the _ thumb1 () function to scale the watermark image:
/*** Thumbnails (equal proportion) * @ param resource $ img image stream * @ param int $ width * @ param int $ height * @ param int $ type * @ param int $ new_width * @ param int $ new_height *@ return resource */private function _ thumb1 ($ img, $ width, $ height, $ type, $ new_width, $ new_height) {if ($ width <$ height) {$ new_width = ($ new_height/$ height) * $ width ;} else {$ new_height = ($ new_width/$ width) * $ height;} $ newImg = $ this-> _ CreateImg ($ new_width, $ new_height, $ type ); imagecopyresampled ($ newImg, $ img, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); return $ newImg ;}
5.4. complete code:
<? Php/*** image processing, generating thumbnails and adding watermark images * Created by PhpStorm. * User: andy * Date: 17-1-3 * Time: am */class Image {// original Image private $ imgPath; // Image address private $ width; // Image Width private $ height; // Image height private $ type; // image type private $ img; // image (image stream) // thumbnail private $ newImg; // Thumbnail (image stream) private $ newWidth; private $ newHeight; // watermark image path private $ waterMarkPath; // output image quality, jpg valid private $ quality; /*** Image constructor. * @ par Am string $ imagePath image path * @ param string $ markPath watermark image path * @ param int $ new_width thumbnail width * @ param int $ new_height thumbnail height * @ param int $ quality jpg image format output quality */public function _ construct (string $ imagePath, string $ markPath = null, int $ new_width = null, int $ new_height = null, int $ quality = 75) {$ this-> imgPath = $ _ SERVER ['document _ root']. $ imagePath; $ this-> waterMarkPath = $ markPath; $ this-> newWid Th = $ new_width? $ New_width: $ this-> width; $ this-> newHeight = $ new_height? $ New_height: $ this-> height; $ this-> quality = $ quality? $ Quality: 75; list ($ this-> width, $ this-> height, $ this-> type) = getimagesize ($ this-> imgPath ); $ this-> img = $ this-> _ loadImg ($ this-> imgPath, $ this-> type); // generate a thumbnail $ this-> _ thumb (); // Add a watermark image if (! Empty ($ this-> waterMarkPath) $ this-> _ addWaterMark (); // output image $ this-> _ outputImg ();} /*** image output */private function _ outputImg () {switch ($ this-> type) {case 1: // GIF imagegif ($ this-> newImg, $ this-> imgPath); break; case 2: // JPG if (intval ($ this-> quality) <0 | intval ($ this-> quality)> 100) $ this-> quality = 75; imagejpeg ($ this-> newImg, $ this-> imgPath, $ this-> quality); break; case 3: // PNG imagepng ($ this-> newImg, $ this-> imgPath); break;} imagedestroy ($ this-> newImg); imagedestroy ($ this-> img );} /*** add watermark */private function _ addWaterMark () {$ ratio = 1/5; // watermark zoom ratio $ Width = imagesx ($ this-> newImg ); $ Height = imagesy ($ this-> newImg); $ n_width = $ Width * $ ratio; $ n_height = $ Width * $ ratio; list ($ markWidth, $ markHeight, $ markType) = getimagesize ($ this-> waterMarkPath); if ($ n_width> $ markWidth) $ n_width = $ markWidth; if ($ n_height> $ markHeight) $ n_height = $ markHeight; $ Img = $ this-> _ loadImg ($ this-> waterMarkPath, $ markType); $ Img = $ this-> _ thumb1 ($ Img, $ markWidth, $ markHeight, $ markType, $ n_width, $ n_height); $ markWidth = imagesx ($ Img); $ markHeight = imagesy ($ Img ); imagecopyresampled ($ this-> newImg, $ Img, $ Width-$ markWidth-10, $ Height-$ markHeight-10, 0, 0, $ markWidth, $ markHeight, $ markWidth, $ markHeight); imagedestroy ($ Img);}/*** Thumbnail (crop based on the specified width and height in an equal proportion) */private function _ thumb () {// if the source image itself is smaller than the thumbnail, press the height of the source image if ($ this-> newWidth> $ this-> width) $ this-> newWidth = $ this-> width; if ($ this-> newHeight> $ this-> height) $ this-> newHeight = $ this-> height; // The height of the background image. $ gd_width = $ this-> newWidth; $ gd_height = $ this-> newHeight; // If the width and height of the thumbnail are the same as the width and height of the source image, crop if ($ gd_width = $ this-> width | $ gd_height = $ this-> height) {$ this-> newWidth = $ this-> width; $ this-> newHeight = $ this-> height;} else {// calculate the zoom ratio $ per = 1; if ($ this-> newHeight/$ this-> height)> ($ this-> newWidth/$ this-> width )) {$ per = $ this-> newHeight/$ this-> height;} else {$ per = $ this-> newWidth/$ this-> width ;} if ($ per <1) {$ this-> newWidth = $ this-> width * $ per; $ this-> newHeight = $ this-> height * $ per ;}} $ this-> newImg = $ this-> _ CreateImg ($ gd_width, $ gd_height, $ this-> type); imagecopyresampled ($ this-> newImg, $ this-> img, 0, 0, 0, 0, $ this-> newWidth, $ this-> newHeight, $ this-> width, $ this-> height );} /*** thumbnails (equal proportion) * @ param resource $ img image stream * @ param int $ width * @ param int $ height * @ param int $ type * @ param int $ new_width * @ param int $ new_height *@ return resource */private function _ thumb1 ($ img, $ width, $ height, $ type, $ new_width, $ new_height) {if ($ width <$ height) {$ new_width = ($ new_height/$ height) * $ width ;} else {$ new_height = ($ new_width/$ width) * $ height;} $ newImg = $ this-> _ CreateImg ($ new_width, $ new_height, $ type ); imagecopyresampled ($ newImg, $ img, 0, 0, 0, $ new_width, $ new_height, $ width, $ height); return $ newImg ;} /*** load image ** @ param string $ imgPath * @ param int $ type * @ return resource */private function _ loadImg ($ imgPath, $ type) {switch ($ type) {case 1: // GIF $ img = imagecreatefromgif ($ imgPath); break; case 2: // JPG $ img = imagecreatefromjpeg ($ imgPath ); break; case 3: // PNG $ img = imagecreatefrompng ($ imgPath); break; default: // other types of Tool: alertBack ('does not support the current image type. '. $ type); break;} return $ img ;} /*** create a background image * @ param int $ width * @ param int $ height * @ param int $ type * @ return resource */private function _ CreateImg ($ width, $ height, $ type) {$ img = imagecreatetruecolor ($ width, $ height); switch ($ type) {case 3: // png imagecolortransparent ($ img, 0 ); // set the background to transparent imagealphablending ($ img, false); imagesavealpha ($ img, true); break; case 4: // gif imagecolortransparent ($ img, 0 ); break;} return $ img ;}}
6. Call
The call is very simple. After the class is introduced, you can directly add new and enter the corresponding parameters:
E.g.
New Image ($ _ path, MARK, 400,200,100 );
7. Summary
This Image class can generate thumbnails without black edges, add a watermark Image, and scale the watermark Image according to the Image size. Of course, there is a disadvantage, that is, it is difficult to zoom in or out GIF animation because frame processing is involved.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.