PHP imagecopy () and Imagecopymerge () image add watermark

Source: Internet
Author: User
Tags imagecopy imagejpeg

The Imagecopymerge () function copies and merges part of an image, returns TRUE successfully, or returns FALSE.

GD Library support for PHP under Windows

Find php.ini, open content, find:

; Extension=php_gd2.dll

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

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 source image being copied
Dst_x target image start x coordinates
Dst_y target image start Y coordinate, x,y same 0 starts at upper left corner
src_x copy image start x coordinates
Src_y copy image start Y coordinate, x,y same 0 to start 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
PCT image merge degree, take value 0-100, when pct=0, actually did not do anything, on the contrary completely merge.

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

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

The code is as follows Copy Code

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

Merging Watermark Pictures
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 Imagecopymerge function is almost not used, we can directly use Imagecopy to generate watermark Two function is 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 pictures
$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 original upload file
Imagedestroy ($nimage);
Imagedestroy ($simage);
}

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


/***
Want to manipulate the picture
Get the size and type information of the picture first.

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

Thumbnail: Just copy the big picture 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 the info is parsed 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, replace the original diagram by default instead of filling
*/
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, make sure the watermark is not larger than the image 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, it may be 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 canvas
$dim = $dfunc ($DST);
Create a canvas to be manipulated
$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:
Top 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 Artwork
}

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

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

return true;
}

/**
Thumb Generate thumbnails
Equal scale, white on both sides
**/
public static function thumb ($dst, $save = NULL, $width =, $height = 200) {
First, decide that the picture you want to work on doesn't exist.
$dinfo = Self::imageinfo ($DST);
if ($dinfo = = False) {
return false;
}

Calculate 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 pictures  
         if (! $save) { 
             $save = $dst; 
            Unlink ($DST); 
       } 
 
         $createfunc = ' image '. $dinfo [' ext ']; 
        $createfunc ($tim, $save); 
 
        Imagedestroy ($dim); 
         Imagedestroy ($tim); 
 
        return true; 
& nbsp
   } 
 

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.