PHP imagecopy () and Imagecopymerge () image add Watermark _php Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
Image watermark in PHP There are many ways to achieve, he these features are based on the GD library in PHP, if there is no opening of the GD library is not allowed to use the watermark function.

The Imagecopymerge () function is used to copy and merge part of the image, which returns TRUE successfully, otherwise FALSE.

GD Library support for PHP open under Windows

Locate php.ini, open the content, find:

; Extension=php_gd2.dll

Put the front of the semicolon ";" Remove, and then save, if there is no semicolon, it is already open

The basic syntax

BOOL Imagecopymerge (Resource Dst_im, resource src_im, int dst_x,
int dst_y, int src_x, int src_y, int src_w, int src_h, int pct)

Parameter Description: Parameter description

Dst_im Target Image
Src_im the copied source image
Dst_x target image start X coordinate
Dst_y the target image starts with the Y coordinate, and x, Y and 0 start at the top left corner.
src_x copy image start X coordinate
Src_y copy image start y coordinate, x, y same as 0 copy from upper left corner
Src_w (starting from src_x) The width of the copy
Src_h (starting from src_y) The height of the copy
The PCT image merge degree, value 0-100, when pct=0, actually did nothing, reverse completely merges.

This function is exactly the same as imagecopy () for the palette image when pct = 100

Know the usage, to achieve our function is simple, with the following code can be easily implemented

The code is as follows Copy Code

Header ("Content-type:image/jpeg");

Original image
$DST = "Images/flower_1.jpg";

Get the original picture information
$DST _im = Imagecreatefromjpeg ($DST);
$DST _info = getimagesize ($DST);

Watermark Image
$SRC = "Images/logo.gif";
$src _im = imagecreatefromgif ($SRC);
$src _info = getimagesize ($SRC);

Watermark Transparency
$alpha = 30;

Merge watermark Picture
Imagecopymerge ($dst _im, $src _im, $DST _info[0]-$src _info[0], $dst _info[1]-$src _info[1],0,0, $src _info[0],
$SRC _info[1], $alpha);

Output merged watermark Picture
Imagejpeg ($dst _im);
Imagedestroy ($dst _im);
Imagedestroy ($src _im);
?>

After the new version of the Imagecopymerge function is almost unused, we can directly use the imagecopy to generate a watermark two functions are exactly the same.

Add watermark
$watermark = 1;
$watertype = 2;
$waterstring = ";
$waterimg = "Z.png"; Watermark Picture
$sFilePath = ' aa.jpg ';
if ($watermark ==1)
{
$image _size = getimagesize ($sFilePath); Uploaded images
$water _size = getimagesize ($waterimg); Watermark Picture
$iinfo =getimagesize ($sFilePath, $iinfo);
$nimage =imagecreatetruecolor ($image _size[0], $image _size[1]);
$white =imagecolorallocate ($nimage, 255,255,255);
$black =imagecolorallocate ($nimage, 0,0,0);
$red =imagecolorallocate ($nimage, 255,0,0);
Imagefill ($nimage, 0,0, $white);
Switch ($iinfo [2])
{
Case 1:
$simage =imagecreatefromgif ($sFilePath);
Break
Case 2:
$simage =imagecreatefromjpeg ($sFilePath);
Break
Case 3:
$simage =imagecreatefrompng ($sFilePath);
Break
Case 6:
$simage =imagecreatefromwbmp ($sFilePath);
Break
Default
Die ("Unsupported file type");
Exit
}

Imagecopy ($nimage, $simage, 0,0,0,0, $image _size[0], $image _size[1]);

Switch ($watertype)
{
Case 1://Add watermark String
Imagestring ($nimage, 2,3, $image _size[1]-15, $waterstring, $black);
Break
Case 2://Add watermark Picture
$simage 1 =imagecreatefrompng ($waterimg);
$x = $image _size[0]-$water _size[0];
$y = $image _size[1]-$water _size[1];
Imagecopy ($nimage, $simage 1, $x, $y, 0,0,240,65);
Imagedestroy ($simage 1);
Break
}

Switch ($iinfo [2])
{
Case 1:
Imagegif ($nimage, $sFilePath);
Imagejpeg ($nimage, $sFilePath);
Break
Case 2:
Imagejpeg ($nimage, $sFilePath);
Break
Case 3:
Imagepng ($nimage, $sFilePath);
Break
Case 6:
Imagewbmp ($nimage, $sFilePath);
Imagejpeg ($nimage, $sFilePath);
Break
}

Overwrite the original upload file
Imagedestroy ($nimage);
Imagedestroy ($simage);
}

A better feature that can generate thumbnails and can also add watermarks to pictures


/***
Want to manipulate pictures
You have to get the size, type information of the picture first.

Watermark: is to copy the specified watermark to the target, and add transparency effect

Thumbnail: The big picture is copied to the small size screen
***/

The code is as follows Copy Code

Class Imagetool {
Imageinfo Analysis of picture information
Return Array ()
public static function Imageinfo ($image) {
Determine if a picture exists
if (!file_exists ($image)) {
return false;
}

$info = getimagesize ($image);

if ($info = = False) {
return false;
}

At this point, info is parsed out and is an array
$img [' width '] = $info [0];
$img [' height '] = $info [1];
$img [' ext '] = substr ($info [' MIME '], Strpos ($info [' MIME '], '/') + 1);

return $img;
}

/*
Add Watermark function
Parm String $DST Operation picture
Parm String $water Watermark Small Image
Parm String $save, do not fill default replace original diagram
*/
public static function water ($DST, $water, $save = NULL, $pos = 2, $alpha = 50) {
2 images are guaranteed to exist first
if (!file_exists ($DST) | |!file_exists ($water)) {
return false;
}

First, ensure that the watermark is not larger than the picture to be manipulated
$dinfo = Self::imageinfo ($DST);
$winfo = Self::imageinfo ($water);

if ($winfo [' height '] > $dinfo [' height '] | | $winfo [' width '] > $dinfo [' width ']) {
return false;
}

Two pictures, read on the canvas! But the picture may be PNG, maybe jpeg, what function to read?
$dfunc = ' Imagecreatefrom '. $dinfo [' ext '];
$wfunc = ' Imagecreatefrom '. $winfo [' ext '];

if (!function_exists ($dfunc) | |!function_exists ($WFUNC)) {
return false;
}

Dynamic load function to create a canvas
$dim = $dfunc ($DST);
Create a canvas to manipulate
$wim = $wfunc ($water);
Create a watermark Canvas

Calculates the pasted coordinates based on the location of the watermark
Switch ($pos) {
Case 0:
Upper left corner
$POSX = 0;
$posy = 0;
Break

Case 1:
upper right corner
$POSX = $dinfo [' width ']-$winfo [' width '];
$posy = 0;
Break

Case 3:
Lower left corner
$POSX = 0;
$posy = $dinfo [' height ']-$winfo [' height '];
Break

Default:
$POSX = $dinfo [' width ']-$winfo [' width '];
$posy = $dinfo [' height ']-$winfo [' height '];
}

Add watermark
Imagecopymerge ($dim, $wim, $posx, $posy, 0, 0, $winfo [' width '], $winfo [' height '], $alpha);

Save
if (! $save) {
$save = $DST;
Unlink ($DST);
Delete Original
}

$createfunc = ' image '. $dinfo [' ext '];
$createfunc ($dim, $save);

Imagedestroy ($dim);
Imagedestroy ($wim);

return true;
}

/**
Thumb Generate thumbnail image
Proportional scaling, left white on both sides
**/
public static function thumb ($dst, $save = NULL, $width = $, $height = 200) {
First, the image to be processed does not exist.
$dinfo = Self::imageinfo ($DST);
if ($dinfo = = False) {
return false;
}

Calculating the zoom ratio
$calc = min ($width/$dinfo [' width '], $height/$dinfo [' height ']);

Create a canvas for the original diagram
$dfunc = ' Imagecreatefrom '. $dinfo [' ext '];
$dim = $dfunc ($DST);

Create a thumbnail canvas
$tim = Imagecreatetruecolor ($width, $height);

Create a white filled thumbnail canvas
$white = Imagecolorallocate ($tim, 255, 255, 255);

Fill the thumbnail canvas
Imagefill ($tim, 0, 0, $white);

Copy and thumbnail
$dwidth = (int) $dinfo [' width '] * $CALC;
$dheight = (int) $dinfo [' Height '] * $CALC;

$PADDINGX = (int) ($width-$dwidth)/2;
$paddingy = (int) ($height-$dheight)/2;

Imagecopyresampled ($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $dinfo [' width '],

$dinfo [' height ']);

Save picture
if (! $save) {
$save = $DST;
Unlink ($DST);
}

$createfunc = ' image '. $dinfo [' ext '];
$createfunc ($tim, $save);

Imagedestroy ($dim);
Imagedestroy ($tim);

return true;

}

}

http://www.bkjia.com/PHPjc/632955.html www.bkjia.com true http://www.bkjia.com/PHPjc/632955.html techarticle image Watermark in PHP There are many ways to achieve, he these features are based on the GD library in PHP, if there is no opening of the GD library is not allowed to use the watermark function. Imagecopymerge ...

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