Php shares two ways to add watermarks to images,
This article introduces the two ways php can add watermarks to images. For more information, see the source code of this article.
Method 1: PHP's simplest watermarking method
<? Php // http://www.manongjc.com $ img = imagecreatefromjpeg ($ filename); $ logo = imagecreatefromjpeg ($ filename);/* imagecraetefromjpeg-create a new image imagecreatefromjpeg (string $ filename) by file or URL) if the fopen package is enabled, the URL can be used as the file name */imagecopy ($ img, $ logo, 15, 15, 0, $ width, $ height);/* imagecopy ($ dst_im, $ src_im, $ dst_x, $ dst_y, $ src_x, $ src_y, $ src_w, $ src_h) $ dst_im is the background image, that is, the image to add a watermark $ src_im is the watermark image; $ dst_x, # dst_y: place the watermark in the (x, y) coordinates of the background image; $ src_x, $ s Rc_y is the starting coordinate of the image that captures the watermark $ width, $ height is the length and width of the captured image */$ url = 'HTTP: // response = file_get_contents ($ url); // write the url into the content Variable/* file_get_contents -- read the entire file into a string */$ filename = 'tmp.jpg '; file_put_contents ($ filename, $ content); // put all content in the filename variable. The first one is the background image/* file_put_contents (string $ filename, mixed $ data) write a string into a file filename to be written Data File Name the data to be written. The data type can be string, array, or stream resource */$ url = 'your file_put_contents('logo.png ', file_get_contents ($ url )); // The second is the watermark image $ img = imagecreatefromjpeg ($ filename); $ logo = imagecreatefrompng('logo.png '); $ size = getimagesize('logo.png');/* getimagesize () obtain the image size */imagecopy ($ img, $ logo, 15, 15, $ size [0], $ size [1]); header ("centent-type: image/jpeg "); imagejpeg (img);?>
Method 2: Add a text watermark to the image using php
<? Php // response = 'HTTP: // response = imagecreatefromstring (file_get_contents ($ dst_path);/* imagecreatefromstring () -- create an image from the image stream in the string, returns an image identifier indicating that the image format obtained from a given string is 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 http://www.manongjc.com/article/1302.html */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 values to a group of variables in one step *//* What information does getimagesize () obtain? 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);?>
Address: http://www.manongjc.com/article/593.html