PHPimagecopy () and imagecopymerge () image watermark _ PHP Tutorial

Source: Internet
Author: User
Tags imagecopy imagejpeg
Add watermarks to PHPimagecopy () and imagecopymerge () images. There are many ways to add watermarks to images in php. these functions are based on the GD Library in php. if you do not have an account, you cannot use the watermark function. There are many ways to add watermarks to imagecopymerge images in php. these functions are based on the GD Library in php. if you do not open a GD library, you cannot use the watermark function.

The imagecopymerge () function is used to copy and merge part of an image. If the image is successfully copied, TRUE is returned. otherwise, FALSE is returned.

Enable GD library support for PHP in Windows

Find php. ini, open the content, and find:

; Extension = php_gd2.dll

Remove the semicolon ";" at the beginning and save it. if there is no semicolon, it is enabled.

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
SOURCE image copied by src_im
Dst_x target Image start x coordinate
Dst_y: the y coordinate of the target image. if x and y are both 0, they start from the upper left corner.
Src_x copy image start x coordinate
Src_y: Copy the y coordinate of the image. if x and y are both 0, copy the image from the upper left corner.
Src_w (starting from src_x) copy width
Src_h (starting from src_y) copying height
Pct image merging degree. The value ranges from 0 to 100. when pct is set to 0, nothing is actually done, and vice versa.

This function is identical to imagecopy () for the color palette image when the value is pct = 100.

Once we know the usage, it is easy to implement our functions, and the following code can be easily implemented.

The code is as follows:

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

// Original image
$ Dst = "images/flower_1.jpg ";

// Obtain the original image 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 the watermark image
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 the merged watermark image
Imagejpeg ($ dst_im );
Imagedestroy ($ dst_im );
Imagedestroy ($ src_im );
?>

After the new version, the imagecopymerge function is almost useless. we can directly use imagecopy to generate watermarks. the functions of the two functions are exactly the same.

// Add a watermark
$ Watermark = 1;
$ Watertype = 2;
$ Waterstring = '';
$ Waterimg = "z.png"; // watermark image
$ SFilePath named 'aa.jpg ';
If ($ watermark = 1)
{
$ Image_size = getimagesize ($ sFilePath); // uploaded image
$ Water_size = getimagesize ($ waterimg); // watermark image
$ Iinfo = getimagesize ($ sFilePath, $ iinfo );
$ Nimage = imagecreatetruecolor ($ image_size [0], $ image_size [1]);
$ White = imagecolorallocate ($ nimage, 255,255,255 );
$ Black = imagecolorallocate ($ nimage, 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 types ");
Exit;
}

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

Switch ($ watertype)
{
Case 1: // add a watermark string
Imagestring ($ nimage, 2, 3, $ image_size [1]-15, $ waterstring, $ black );
Break;
Case 2: // add a watermark image
$ Simage1 = imagecreatefrompng ($ waterimg );
$ X = $ image_size [0]-$ water_size [0];
$ Y = $ image_size [1]-$ water_size [1];
Imagecopy ($ nimage, $ simage1, $ x, $ y, 65 );
Imagedestroy ($ simage1 );
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 uploaded File
Imagedestroy ($ nimage );
Imagedestroy ($ simage );
}

A better function can generate scaling and add watermarks to images


/***
Image operations
First, you must obtain the image size and type information.

Watermark: copies the specified watermark to the target and adds the transparency effect.

Thumbnail: copies a large image to a small image.
***/

The code is as follows:

Class ImageTool {
// ImageInfo analyzes image information
// Return array ()
Public static function imageInfo ($ image ){
// Determine whether an image exists
If (! File_exists ($ image )){
Return false;
}

$ Info = getimagesize ($ image );

If ($ info = false ){
Return false;
}

// Info is analyzed as an array.
$ Img ['width'] = $ info [0];
$ Img ['height'] = $ info [1];
$ Img ['ext '] = substr ($ info ['Mime'], strpos ($ info ['Mime '],'/') + 1 );

Return $ img;
}

/*
Watermarking
Parm String $ dst and other operation images
Parm String $ water watermark thumbnail
Parm String $ save. If this parameter is not specified, the original graph is replaced by default.
*/
Public static function water ($ dst, $ water, $ save = NULL, $ pos = 2, $ alpha = 50 ){
// Ensure that two images exist first
If (! File_exists ($ dst) |! File_exists ($ water )){
Return false;
}

// First, ensure that the watermark cannot be larger than the image to be operated
$ Dinfo = self: imageInfo ($ dst );
$ Winfo = self: imageInfo ($ water );

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

// Read the two images on the canvas! However, the image may be png or jpeg. what function is used to read the image?
$ Dfunc = 'imagecreatefrom '. $ dinfo ['ext'];
$ Wfunc = 'imagecreatefrom '. $ winfo ['ext'];

If (! Function_exists ($ dfunc) |! Function_exists ($ wfunc )){
Return false;
}

// Dynamically load the function to create the canvas
$ Dim = $ dfunc ($ dst );
// Create the canvas to be operated
$ Wim = $ wfunc ($ water );
// Create a watermark canvas

// Calculate the pasted coordinates based on the Watermark Position
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 a watermark
Imagecopymerge ($ dim, $ wim, $ posx, $ posy, 0, 0, $ winfo ['width'], $ winfo ['height'], $ alpha );

// Save
If (! $ Save ){
$ Save = $ dst;
Unlink ($ dst );
// Delete the source image
}

$ Createfunc = 'image'. $ dinfo ['text'];
$ Createfunc ($ dim, $ save );

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

Return true;
}

/**
Create a thumbnail using thumb
Proportional scaling, left white on both sides
**/
Public static function thumb ($ dst, $ save = NULL, $ width = 200, $ height = 200 ){
// First, determine whether the image to be processed does not exist.
$ Dinfo = self: imageInfo ($ dst );
If ($ dinfo = false ){
Return false;
}

// Calculate the scaling ratio
$ Calc = min ($ width/$ dinfo ['width'], $ height/$ dinfo ['height']);

// Create the canvas of the original graph
$ Dfunc = 'imagecreatefrom '. $ dinfo ['ext'];
$ Dim = $ dfunc ($ dst );

// Create a thumbnail
$ Tim = imagecreatetruecolor ($ width, $ height );

// Create a white fill thumbnail
$ White = imagecolorallocate ($ tim, 255,255,255 );

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

// Copy and scale down
$ 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 the image
If (! $ Save ){
$ Save = $ dst;
Unlink ($ dst );
}

$ Createfunc = 'image'. $ dinfo ['text'];
$ Createfunc ($ tim, $ save );

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

Return true;

}

}

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