In order to prevent the hard work of the pictures being stolen, a lot of photos will be added watermark, you can add a watermark directly with the image upload, but PHP can be implemented to the image watermark function, this article to the Code farmers introduce PHP to the picture of two methods of watermark, interested in the code farmers can refer to the source code of this article.
Method One: The simplest way to add a watermark to PHP
<?php $img = Imagecreatefromjpeg ($filename);
$logo = Imagecreatefromjpeg ($filename); /*imagecraetefromjpeg-creates a new image from a file or URL imagecreatefromjpeg (string $filename) if the fopen wrapper is enabled, the URL can be the file name/imagecopy ($
IMG, $logo, 15,15,0,0, $width, $height); /*imagecopy ($dst _im, $src _im, $dst _x, $dst _y, $src _x, $src _y, $src _w, $src _h) $dst _im is a background image, a picture that needs to be added a watermark $src _im is a watermark picture; $
Dst_x, #dst_y需要把水印放到背景图片的 (x,y) coordinates;
$src _x, $src _y is to intercept the image of the beginning of the coordinates $width, $height is the interception of the image is the length and width of the watermark/$url = ' yun_qi_img/100759pidbdaydh8dy7iby.jpg '; $content = file_get_contents ($url);//write URL to content variable/*file_get_contents--read the entire file into a string/$filename = ' tmp.jpg '
;
File_put_contents ($filename, $content);
Put all the content into the variable of filename, the first one is the background picture/*file_put_contents (String $filename, mixed $data) writes a string to a file
FileName to be written to the data file name data to be written, the type can be string,array or stream resources/$url = ';
File_put_contents (' Logo.png ', file_get_contents ($url));
The second is the image of the watermark $img = Imagecreatefromjpeg ($filename);
$logo = imagecreatefrompng (' logo.png ');
$size = getimagesize (' logo.png '); /*GetImageSize () Get the image size * * imagecopy ($img, $logo, 15,15,0,0, $size [0], $size [1]);
Header ("Centent-type:image/jpeg"); Imagejpeg (IMG);?>
Method Two: Php to add text watermark to the picture
<?php/* To the picture Plus text watermark method * * * $dst _path = ' http://f4.topitme.com/4/15/11/1166351597fe111154l.jpg ';
$DST = imagecreatefromstring (file_get_contents ($DST _path)); /*imagecreatefromstring ()--Creates a new image from the image stream in the string, returns an image identifier that expresses an image Image format from a given string that will automatically be monitored as long as PHP supports jpeg,png,gif,wbmp,gd2.*/$
Font = './t1.ttf ';
$black = imagecolorallocate ($dst, 0, 0, 0);
Imagefttext ($DST, 0, $black, $font, ' Hello world! '); /*imagefttext ($img, $size, $angle, $x, $y, $color, $fontfile, $text) $img the image resource size returned by the image creation function The font size of the watermark to be used angle (angle) text tilt angle , if it is 0 degrees to represent the text from left to right, if it is 90 degrees from the top down x,y watermark text from the beginning of the first text color is watermark text colors Fontfile, you want to use the path of TrueType fonts http://www.manongjc.com/
article/1302.html/List ($dst _w, $dst _h, $dst _type) = getimagesize ($dst _path); /*list (mixed $varname [, mixed $ ...])
--Assign the values in the array to some variables like array (), which is not a real function, but a language structure, and the List () is assigned a set of variables in one operation.//*getimagesize () What information can be obtained?
The GetImageSize function returns all information about the image, including size, type, etc. */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--the image to the browser or file Imagepng () in PNG format to output the GD image stream (image) in PNG format to the callout output (usually the browser), or if the filename is given a file name, it is exported to the document/}
Imagedestroy ($DST);?>
I hope this article will help you with your PHP programming.