Php adds a text watermark to an image.

Source: Internet
Author: User
Tags image identifier imagecopy imagejpeg unsupported
To add watermarks to images in php, we need to install the GD library for php. here we will not introduce the installation of the GD Library, this section only describes how to use php to add a text watermark to an image. If you have any need, refer. 1: Process-oriented compiling method

// Specify the image path $ src = '001.png '; // Obtain the image information $ info = getimagesize ($ src ); // Obtain the image extension $ type = image_type_to_extension ($ info [2], false); // dynamically import the image into the Memory $ fun = "imagecreatefrom {$ type }"; $ image = effecfun('001.png '); // specify the font color $ col = imagecolorallocatealpha ($ image, 255,255,255, 50); // specify the font content $ content = 'helloworld '; // add the image text imagestring ($ image, 5, 20, 30, $ content, $ col); // specify the input type header ('content-type :'. $ info ['Mime ']); // dynamically output the image to the browser $ func = "image {$ type}"; $ func ($ image ); // destroy the image imagedestroy ($ image );

2: Object-oriented implementation

Class Image_class {private $ image; private $ info;/*** @ param $ src: The image path * loads the image to the memory */function _ construct ($ src) {$ info = getimagesize ($ src); $ type = image_type_to_extension ($ info [2], false); $ this-> info = $ info; $ this-> info ['type'] = $ type; $ fun = "imagecreatefrom ". $ type; $ this-> image = $ fun ($ src);}/*** @ param $ fontsize: fontsize * @ param $ x: the x position of the font in the image * @ param $ y: the y position of the font in the image * @ param $ color: the color of the font is an array containing rgba * @ param $ text: content to be added * operate on images in memory, add text watermarks to images */public function fontMark ($ fontsize, $ x, $ y, $ color, $ text) {$ col = imagecolorallocatealpha ($ this-> image, $ color [0], $ color [1], $ color [2], $ color [3]); imagestring ($ this-> image, $ fontsize, $ x, $ y, $ text, $ col);}/** output image to the browser */public function show () {header ('content-type :'. $ this-> info ['Mime ']); $ fun = 'image '. $ this-> info ['type']; $ fun ($ this-> image);}/*** destroy image */function _ destruct () {imagedestroy ($ this-> image) ;}// call the class $ obj = new Image_class('001.png '); $ obj-> fontMark (20, 20, 30, array (255,255,255, 60), 'Hello'); $ obj-> show ();

3. supports adding watermarks to images and text.. Images can be in GIF, PNG, or JPG formats. watermarks can be in PNG or GIF formats.

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 type"); 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 type"); 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.

4. Add text watermarks to images

<? Php/* Add a text watermark to an image */$ dst_path =' http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg '; $ Dst = imagecreatefromstring (file_get_contents ($ dst_path);/* imagecreatefromstring () -- creates an image from the image stream in the string and returns an image identifier, it indicates that the image format obtained from the given string will be automatically monitored, as long as php supports jpeg, png, gif, wbmp, gd2. */$ font = '. /t1.ttf '; $ black = imagecolorallocate ($ dst, 0, 0, 0); imagefttext ($ dst, 20, 0, 10, 30, $ black, $ font, 'Hello world! ');/* Imagefttext ($ img, $ size, $ angle, $ x, $ y, $ color, $ fontfile, $ text) $ img the font size of the watermark to be used for the image resource size returned by the image creation function angle (angle) text skew angle. if it is 0 degrees, it indicates that the text is left to right, if it is 90 degrees, the starting position of the first text of the y watermark text is from top to bottom. color is the color of the watermark text fontfile, path */list ($ dst_w, $ dst_h, $ dst_type) = getimagesize ($ dst_path);/* list (mixed $ varname [, mixed $ ......]) -- assign values in the array to some variables like array (). This is not a real function, but a language structure, List () assign a value to a group of variables in one step * // * what information can be obtained by getimagesize? The getimagesize function returns all information about the image, including the size, type, and so on. */switch ($ dst_type) {case 1: // GIF header ("content-type: image/gif "); imagegif ($ dst); break; case 2: // JPG header (" content-type: image/jpeg "); imagejpeg ($ dst ); break; case 3: // PNG header ("content-type: image/png"); imagepng ($ dst); break; default: break; /* imagepng -- output the image to the browser or file imagepng () in PNG format. the GD image stream (image) is output to the annotation output in png format (usually the browser ), or, if filename is used to provide a file name, output it to the file */} imagedestroy ($ Dst);?>

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.