Two kinds of implementation code _php example of PHP to image watermark

Source: Internet
Author: User
Tags image identifier imagecopy imagejpeg
the simplest way to add watermarks to PHP

<?php$img = Imagecreatefromjpeg ($filename); $logo = Imagecreatefromjpeg ($filename);/* imagecraetefromjpeg-A new image is created by a file or URL imagecreatefromjpeg (string $filename) if the fopen wrapper is enabled, the URL can be used as 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 the background image, is the need to add a watermark image $src_im is a watermark picture; $dst _x, #dst_y需要把水印放到背景图片的 (x, y) coordinates; $src _x, $src _y is the starting coordinate of the image that intercepts the watermark $width,$ Height is the captured picture is the length and width of the watermark */$url = ' http://www.stchat.cn/data/attachment/forum/201506/12/100759pidbdaydh8dy7iby.jpg '; $content = 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 everything in the filename variable, the first one to hold the background image/*file_put_contents (string $ Filename,mixed $data) writes a string to a file filename to be written to data the data to be written to, the type can be String,array or stream resource */$url = '; file_put_ Contents (' Logo.png ', file_get_contents ($url));//The second is a picture of the watermark $img = Imagecreatefromjpeg ($filename); $logo = Imagecreatefrompng (' logo.png '); $size = GetImageSize (' Logo.png ');/*getimagesize () Gets the image size */imagecopy ($img, $logo, 15,15,0,0, $size [0], $size [1]); Header ("Centent-type: Image/jpeg "); Imagejpeg (IMG);? >

PHP To add a text watermark to a picture

<?php/* the way to add text watermark to the picture */$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, It expresses the image Image format obtained from a 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, 0, ten, $black, $font, ' Hello world! '); *imagefttext ($img, $size, $angle, $x, $y, $color, $fontfile, $text) $img the image resource returned by the image creation function size to use the font size of the watermark angle (angle) The tilt angle of the text, If the 0 degree represents the text from left to right, if it is 90 degrees from the beginning of the first text of the x, y watermark text, color is the color of the watermark text fontfile, you want to use the path of the TrueType font */list ($dst _w, $dst _h, $dst _ Type) = GetImageSize ($dst _path);/*list (mixed $varname [, mixed $ ...] --Assigning values in an array to some variables like array (), this is not a real function, but a language structure, and List () assigns a set of variables in one step *//*getimagesize () what information can be obtained?    The GetImageSize function returns all information about the image, including 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 in PNG format to a browser or file Imagepng () exports the GD image Stream (image) in PNG format to the callout output (usually a browser), or if filename is given with filename, it is output to a file */} Imagedestroy ($DST);? >

The above is PHP to the image Watermark implementation code, I hope you can master.

  • 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.