PHP image processing, PHP image text watermark, PHP how to add a watermark, PHP watermark function, PHP generate a watermark
Today, I will share with you the source code of the function that is frequently used in PHP website development to add watermarks to images. it was written by the PHP blog and tested in person, you can add watermarks to images in two ways: Image and text.
Images can be in GIF, PNG, or JPG formats. watermarks can be in PNG or GIF formats. In other words, paste my PHP image watermark function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
Function setWater ($ imgSrc, $ markImg, $ markText, $ TextColor, $ markPos, $ fontType, $ markType) {
$ SrcInfo = @ getimagesize ($ imgSrc ); $ SrcImg_w = $ srcInfo [0]; $ SrcImg_h = $ srcInfo [1];
Switch ($ srcInfo [2]) { Case 1: $ Srcim = imagecreatefromgif ($ imgSrc ); Break; Case 2: $ Srcim = imagecreatefromjpeg ($ imgSrc ); Break; Case 3: $ Srcim = imagecreatefrompng ($ imgSrc ); Break; Default: Die ("unsupported image file types "); Exit; }
If (! Strcmp ($ markType, "img ")) { If (! File_exists ($ markImg) | empty ($ markImg )) { Return; }
$ MarkImgInfo = @ getimagesize ($ markImg ); $ MarkImg_w = $ markImgInfo [0]; $ MarkImg_h = $ markImgInfo [1];
If ($ srcImg_w <$ markImg_w | $ srcImg_h <$ markImg_h) { Return; }
Switch ($ markImgInfo [2]) { Case 1: $ Markim = imagecreatefromgif ($ markImg ); Break; Case 2: $ Markim = imagecreatefromjpeg ($ markImg ); Break; Case 3: $ Markim = imagecreatefrompng ($ markImg ); Break; Default: Die ("unsupported watermark image file types "); Exit; }
$ Logow = $ markImg_w; $ Logoh = $ markImg_h; }
If (! Strcmp ($ markType, "text ")) { $ FontSize = 16; If (! Empty ($ markText )) { If (! File_exists ($ fontType )) { Return; } } Else { Return; }
$ Box = @ imagettfbbox ($ fontSize, 0, $ fontType, $ markText ); $ Logow = max ($ box [2], $ box [4])-min ($ box [0], $ box [6]); $ Logoh = max ($ box [1], $ box [3])-min ($ box [5], $ box [7]); }
If ($ markPos = 0) { $ MarkPos = rand (1, 9 ); }
Switch ($ markPos) { Case 1: $ X = + 5; $ Y = + 5; Break; Case 2: $ X = ($ srcImg_w-$ logow)/2; $ Y = + 5; Break; Case 3: $ X = $ srcImg_w-$ logow-5; $ Y = + 15; Break; Case 4: $ X = + 5; $ Y = ($ srcImg_h-$ logoh)/2; Break; Case 5: $ X = ($ srcImg_w-$ logow)/2; $ Y = ($ srcImg_h-$ logoh)/2; Break; Case 6: $ X = $ srcImg_w-$ logow-5; $ Y = ($ srcImg_h-$ logoh)/2; Break; Case 7: $ X = + 5; $ Y = $ srcImg_h-$ logoh-5; Break; Case 8: $ X = ($ srcImg_w-$ logow)/2; $ Y = $ srcImg_h-$ logoh-5; Break; Case 9: $ X = $ srcImg_w-$ logow-5; $ Y = $ srcImg_h-$ logoh-5; Break; Default: Die ("This location is not supported "); Exit; }
$ Dst_img = @ imagecreatetruecolor ($ srcImg_w, $ srcImg_h );
Imagecopy ($ dst_img, $ srcim, 0, 0, 0, 0, $ srcImg_w, $ srcImg_h );
If (! Strcmp ($ markType, "img ")) { Imagecopy ($ dst_img, $ markim, $ x, $ y, 0, 0, $ logow, $ logoh ); Imagedestroy ($ markim ); }
If (! Strcmp ($ markType, "text ")) { $ Rgb = explode (',', $ TextColor );
$ Color = imagecolorallocate ($ dst_img, $ rgb [0], $ rgb [1], $ rgb [2]); Imagettftext ($ dst_img, $ fontSize, 0, $ x, $ y, $ color, $ fontType, $ markText ); }
Switch ($ srcInfo [2]) { Case 1: Imagegif ($ dst_img, $ imgSrc ); Break; Case 2: Imagejpeg ($ dst_img, $ imgSrc ); Break; Case 3: Imagepng ($ dst_img, $ imgSrc ); Break; Default: Die ("unsupported watermark image file types "); Exit; }
Imagedestroy ($ dst_img ); Imagedestroy ($ srcim ); } |
Parameter description:
$ ImgSrc: target image with a relative directory address,
$ MarkImg: a watermark image with a relative directory address. it supports PNG and GIF formats. for example, if a watermark image is under the mark directory of an execution file, it can be written as: mark/mark.gif.
$ MarkText: watermark text added to an image
$ TextColor: the font color of the watermark text
$ MarkPos: Position of the image Watermark. value range: 0 ~ 9
0: random position, ranging from 1 ~ Select a random location between 8
1: Top center left 2: Top Center 3: Top center right 4: Left Center
5: Image Center 6: Center on the right 7: Center on the bottom 8: Center on the bottom 9: Center on the right
$ FontType: the specific library with the relative directory address
$ MarkType: the watermark method for an image. img indicates adding a watermark as an image, and text indicates adding a watermark as a text.
Code comment:
4th ~ Row 6: obtain the width and height of the target image.
8th ~ Row 22: call different functions based on the image type to obtain the operation Image identifier.
Knowledge Point of the GetImageSize function: GetImageSize can be used without the GD degree installed. the returned value array has four elements. The index value 0 indicates the image height. Index value 1 indicates the image width. The index value 2 is the image file format. the value 1 is GIF, 2 is JPEG/JPG, and 3 is PNG. The index value 3 is the string of the image height and width, and the height is xxx width = yyy. The returned image width and height are measured in pixels)
24th ~ Row 58: When you select the image method to watermark the target image, you can obtain the width and height of the watermark image. generally, it is the website logo. If the target image is smaller than the width or height of the watermark image or the watermark image does not exist, this function is displayed.
Return statement knowledge point: direct return indicates that nothing is returned and this function is directly ended. It can also be interpreted as returning NULL.
60th ~ Row 77: when selecting the text method to flat the target image with watermarks, first set the watermark text size. the default value is 16px. you can adjust the font size as needed. If the font file does not exist, jump out of the function and use the imagettfbbox function to obtain the virtual length and width of the text in the specified format.
Imagettfbbox function knowledge point: This function returns an array containing eight units to represent the four corners of the text box. The index value meaning: 0 represents the position X in the lower left corner, and 1 represents the position Y in the lower right corner, 2 represents the position X in the lower right corner, 3 represents the position Y in the lower right corner, 4 represents the position X in the upper right corner, 5 represents the position Y in the upper right corner, 6 represents the position X in the upper left corner, and 7 represents the position Y in the upper left corner. This function must be supported by both the GD Library and the FreeType library.
The max function returns the maximum value of a parameter.
79th ~ Row 125: calculates the coordinate value based on the Watermark Position. you can refine the watermark position based on the effect.
127th ~ Row 3: create an image with the same size as the target image.
Note: Because the range of the imagecreatetruecolor function is a black image, if your target image is transparent, the new image will not be transparent.
131st ~ Row 3: the watermark is generated based on the image or text format.
Call description:
You can call the function as a function call. of course, you can encapsulate the function as a class, or you can further segment the module as needed. Of course, you can use it now without any problems. I have tested it. please feel free to use it.
Other instructions:
The imagettftext and imagettfbbox functions require the support of the GD library and FreeType library. if your runtime environment does not support the GD library and FreeType library, the text mode cannot be implemented, you can use the imagestring function to add a text watermark to the image and set the $ logow and $ logoh values in the text mode.
The imagejpeg function can also set the quality of the merged image.
PHP image watermarking function ideas summary:
First, calculate the width and height of the target image, watermark image, and text, and calculate the position information of the final watermark based on the specific position, that is, the X and Y values. After merging the image, the watermark is added to the new image.