PHP text watermark and PHP image watermark Implementation code (two kinds of watermark method) _php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg transparent color
Text Watermark

Text watermark is to add text on the picture, the main use of GD library Imagefttext method, and the need for font files. As follows:

The implementation code is as follows:

Copy the Code code as follows:
$dst _path = ' dst.jpg ';

Create an instance of a picture
$DST = imagecreatefromstring (file_get_contents ($DST _path));

Make a text.
$font = './SIMSUN.TTC ';//font
$black = Imagecolorallocate ($DST, 0x00, 0x00, 0x00);//Font Color
Imagefttext ($DST, 0, $black, $font, ' happy programming ');

Output picture
List ($dst _w, $dst _h, $dst _type) = getimagesize ($dst _path);
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
}

Imagedestroy ($DST);

Image watermark

Image watermark is to add a picture on another picture, mainly using the GD library imagecopy and Imagecopymerge. As follows:

The implementation code is as follows:

Copy the Code code as follows:
$dst _path = ' dst.jpg ';
$src _path = ' src.jpg ';

Create an instance of a picture
$DST = imagecreatefromstring (file_get_contents ($DST _path));
$SRC = imagecreatefromstring (file_get_contents ($src _path));

Get the width height of the watermark picture
List ($src _w, $src _h) = getimagesize ($src _path);

The watermark picture is copied to the target image, and the last parameter 50 is the set transparency, which enables the translucent effect
Imagecopymerge ($DST, $SRC, ten, 0, 0, $src _w, $src _h, 50);
If the watermark picture itself has a transparent color, use the Imagecopy method
Imagecopy ($DST, $SRC, ten, 0, 0, $src _w, $src _h);

Output picture
List ($dst _w, $dst _h, $dst _type) = getimagesize ($dst _path);
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
}

Imagedestroy ($DST);
Imagedestroy ($SRC);

http://www.bkjia.com/PHPjc/621661.html www.bkjia.com true http://www.bkjia.com/PHPjc/621661.html techarticle Text watermark Text watermark is to add text on the picture, the main use of GD library Imagefttext method, and need font files. As follows: The implementation code is as follows: Copy code generation ...

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