Two php implementation codes for adding watermarks to images. _ PHP Tutorial

Source: Internet
Author: User
Tags image identifier imagecopy imagejpeg
Two php implementation codes for adding watermarks to images ,. Two php implementation codes for adding watermarks to images. PHP's simplest watermarking method php $ imgimagecreatefromjpeg ($ filename); $ logoimagecreatefromjpeg ($ filename ); * imagecraetefromjpeg-the implementation code of adding watermarks to images by using two php methods,

PHP's simplest watermarking method

<? Php $ img = imagecreatefromjpeg ($ filename); $ logo = imagecreatefromjpeg ($ filename);/* imagecraetefromjpeg-create a new image imagecreatefromjpeg (string $ filename) from a 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, $ src_y is the starting coordinate of the image where the watermark is captured $ width, $ height indicates the length and width of the captured image. */$ url =' http://www.stchat.cn/data/attachment/forum/201506/12/100759pidbdaydh8dy7iby.jpg '; $ Content = file_get_contents ($ url); // write the url to 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 the file filename the file name data to be written into the data. 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 = imagecrea Tefromjpeg ($ filename); $ logo = comment'); $ size = getimagesize('logo.png ');/* getimagesize () get image size */imagecopy ($ img, $ logo, 15, 15, 0, 0, $ size [0], $ size [1]); header ("centent-type: image/jpeg"); imagejpeg (img);?>

Add text watermarks to images using php

<? 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);?>

The above is the php implementation code for adding watermarks to images.

Watermark: PHP's simplest watermarking method: php $ img = imagecreatefromjpeg ($ filename); $ logo = imagecreatefromjpeg ($ filename);/* imagecraetefromjpeg-...

Related Article

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.