How to Use PHP to add watermarks to images,

Source: Internet
Author: User
Tags image identifier imagecopy imagejpeg

How to Use PHP to add watermarks to images,

To prevent image theft, many photos are added with watermarks. You can use image tools to add watermarks before uploading. However, you can use PHP 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 $ 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, $ h Eight is the length and width of the captured image. */$ url = 'HTTP: // watermark = 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 the 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 the file name to be written into data the data to be written to the data type can be stri Ng, 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 () Get image size */imagecopy ($ img, $ logo, 15, 15, 0, $ size [0], $ size [1]); header ("centent-type: image/jpeg"); imagejpeg (img);?>

Method 2: Add a text watermark to the image using php

<? Php/* method for adding a text watermark to an image */$ dst_path = 'HTTP: // response = imagecreatefromstring (file_get_contents ($ dst_path);/* imagecreatefromstring () -- create an image from the image stream in the string and return an image identifier, which 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 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);?>

I hope this article will help you with PHP programming.

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.