Php _ PHP Tutorial for adding watermarks to images

Source: Internet
Author: User
Tags imagecopy imagejpeg
Php adds the image watermark instance function. Many functions are used to add watermarks to images in php, such as imagecreatefromjpeg, imagecreatefrompng, and getimagesize. these functions belong to the phpGD Library, therefore, we need to use many functions to add watermarks to images in php, such as imagecreatefromjpeg, imagecreatefrompng, and getimagesize. these functions belong to the php GD library, so we must add watermarks in php. open the GD Library in ini to allow php to use these functions to generate image watermarks.

The watermark function is implemented by using these functions.

1. imagecreatefromjpeg // open the jpg image. 2. imagecreatefromgif // open the GIF image.
3. imagecreatefrompng // open the PNG image
4. imagecreatefromwbmp // Open The WBMP image (rarely used)
5. getimagesize // Obtain the image size information
6. imagecopymerge // integrate multiple images (the main function for adding watermarks)
7. imagejpeg // Save the jpg image
8. imagegif // Save the GIF image
9. imagepng // Save the PNG image

The code is as follows:


Echo img_water_mark ("1.jpg"," walter.gif ", null," 2.jpg", 5, 80 );

/**
* Watermarks images (for png, jpg, and gif formats)
*
* @ Author flynetcn
*
* @ Param $ srcImg original image
* @ Param $ waterImg watermark image
* @ Param $ savepath: save path
* @ Param $ savename save the name
* @ Param $ position of the positon watermark
* 1: Top left, 2: top right, 3: center, 4: bottom left, 5: bottom right
* @ Param $ alpha transparency -- 0: completely transparent, 100: completely opaque
*
* @ Return success -- the new image address after the watermark is added
* Failed ---1: The original file does not exist.-2: The watermark image does not exist.-3: The original file Image object creation failed.
*-4: failed to create the watermark File Image object-5: failed to save the new image after adding the watermark
*/
Function img_water_mark ($ srcImg, $ waterImg, $ savepath = null, $ savename = null, $ positon = 5, $ alpha = 30)
{
$ Temp = pathinfo ($ srcImg );
$ Name = $ temp [basename];
$ Path = $ temp [dirname];
$ Exte = $ temp [extension];
$ Savename = $ savename? $ Savename: $ name;
$ Savepath = $ savepath? $ Savepath: $ path;
$ Savefile = $ savepath. "/". $ savename;
$ Srcinfo = @ getimagesize ($ srcImg );
If (! $ Srcinfo ){
Return-1; // The original file does not exist.
}
$ Waterinfo = @ getimagesize ($ waterImg );
If (! $ Waterinfo ){
Return-2; // The watermark image does not exist.
}
$ SrcImgObj = image_create_from_ext ($ srcImg );
If (! $ SrcImgObj ){
Return-3; // failed to create the original file Image object
}
$ WaterImgObj = image_create_from_ext ($ waterImg );
If (! $ WaterImgObj ){
Return-4; // watermark File Image object creation failed
}
Switch ($ positon ){
// 1 top left
Case 1: $ x = $ y = 0; break;
// 2. top right
Case 2: $ x = $ srcinfo [0]-$ waterinfo [0]; $ y = 0; break;
// 3 center
Case 3: $ x = ($ srcinfo [0]-$ waterinfo [0])/2; $ y = ($ srcinfo [1]-$ waterinfo [1]) /2; break;
// 4 the bottom is left
Case 4: $ x = 0; $ y = $ srcinfo [1]-$ waterinfo [1]; break;
// 5 bottom right
Case 5: $ x = $ srcinfo [0]-$ waterinfo [0]; $ y = $ srcinfo [1]-$ waterinfo [1]; break;
Default: $ x = $ y = 0;
}
Imagecopymerge ($ srcImgObj, $ waterImgObj, $ x, $ y, 0, 0, $ waterinfo [0], $ waterinfo [1], $ alpha );
Switch ($ srcinfo [2]) {
Case 1: imagegif ($ srcImgObj, $ savefile); break;
Case 2: imagejpeg ($ srcImgObj, $ savefile); break;
Case 3: imagepng ($ srcImgObj, $ savefile); break;
Default: return-5; // save failed
}
Imagedestroy ($ srcImgObj );
Imagedestroy ($ waterImgObj );
Return $ savefile;
}

Function image_create_from_ext ($ imgfile)
{
$ Info = getimagesize ($ imgfile );
$ Im = null;
Switch ($ info [2]) {
Case 1: $ im = imagecreatefromgif ($ imgfile); break;
Case 2: $ im = imagecreatefromjpeg ($ imgfile); break;
Case 3: $ im = imagecreatefrompng ($ imgfile); break;
}
Return $ im;
}

Currently, jpg, gif, and png formats are supported.

Example:

The code is as follows:

If ($ pic = watermark ('./image.jpg', './watermark.png '))
{
Echo '';
}
Else
{
Echo '';
}

The following shows a function for adding a complete full watermark.

The code is as follows:

/*************************************** ***********************

Parameter description:
$ Max_file_size: size limit of the uploaded file, in bytes
$ Destination_folder: File Upload path
$ Watermark: whether to add a watermark. (1 indicates adding a watermark; others indicates not adding a watermark );

Instructions for use:
1. remove the number before the line "extension = php_gd2.dll" in the PHP. ini file because the GD library is used;
2. change extension_dir = to the directory where your php_gd2.dll is located;
**************************************** **********************/

// Upload file type list
$ Uptypes = array (
'Image/jpg ',
'Image/jpeg ',
'Image/png ',
'Image/pjpeg ',
'Image/GIF ',
'Image/bmp ',
'Image/x-png'
);

$ Max_file_size = 2000000; // size limit of uploaded files, in bytes
$ Destination_folder = "uploadimg/"; // file Upload path
$ Watermark = 1; // whether to add a watermark. (1 indicates adding a watermark. otherwise, no watermark is added );
$ Watertype = 1; // watermark type (1 is text, 2 is image)
$ Waterposition = 1; // watermark position (1 indicates the lower left corner, 2 indicates the lower right corner, 3 indicates the upper left corner, 4 indicates the upper right corner, and 5 indicates the center );
$ Waterstring = "http://www.bKjia. c0m/"; // watermark string
$ Waterimg = "xplore.gif"; // watermark image
$ Imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated );
$ Imgpreviewsize = 1/1; // Thumbnail ratio
?>


Image watermark program demonstration! WWW. MOP8.COM




This demo space is provided by TuWoo. This program uses text watermarks.
If ($ _ SERVER ['request _ method'] = 'post ')
{
If (! Is_uploaded_file ($ _ FILES ["upfile"] [tmp_name])
// Whether a file exists
{
Echo "the image does not exist! ";
Exit;
}

$ File = $ _ FILES ["upfile"];
If ($ max_file_size <$ file ["size"])
// Check the file size
{
Echo "the file is too large! ";
Exit;
}

If (! In_array ($ file ["type"], $ uptypes ))
// Check the file type
{
Echo "file type does not match! ". $ File [" type "];
Exit;
}

If (! File_exists ($ destination_folder ))
{
Mkdir ($ destination_folder );
}

$ Filename = $ file ["tmp_name"];
$ Image_size = getimagesize ($ filename );
$ Pinfo = pathinfo ($ file ["name"]);
$ Ftype = $ pinfo ['extension'];
$ Destination = $ destination_folder.time (). ".". $ ftype;
If (file_exists ($ destination) & $ overwrite! = True)
{
Echo "a file with the same name already exists ";
Exit;
}

If (! Move_uploaded_file ($ filename, $ destination ))
{
Echo "an error occurred while moving the file ";
Exit;
}

$ Pinfo = pathinfo ($ destination );
$ Fname = $ pinfo [basename];
Echo "uploaded successfully
File name: ". $ destination_folder. $ fname ."
";
Echo "width:". $ image_size [0];
Echo "length:". $ image_size [1];
Echo"
Size: ". $ file [" size "]." bytes ";

If ($ watermark = 1)
{
$ Iinfo = getimagesize ($ destination, $ 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 ($ destination );
Break;
Case 2:
$ Simage = imagecreatefromjpeg ($ destination );
Break;
Case 3:
$ Simage = imagecreatefrompng ($ destination );
Break;
Case 6:
$ Simage = imagecreatefromwbmp ($ destination );
Break;
Default:
Die ("unsupported file types ");
Exit;
}

Imagecopy ($ nimage, $ simage, 0, 0, 0, $ image_size [0], $ image_size [1]);
Imagefilledrectangle ($ nimage, 1, $ image_size [1]-15, 80, $ image_size [1], $ white );

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 = imagecreatefromgif ("xplore.gif ");
Imagecopy ($ nimage, $ simage1 );
Imagedestroy ($ simage1 );
Break;
}

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

// Overwrite the original uploaded File
Imagedestroy ($ nimage );
Imagedestroy ($ simage );
}

If ($ imgpreview = 1)
{
Echo"
Image preview:
";
Echo "echo" alt = "image preview: r File name:". $ destination. "r Upload Time:"> ";
}
}
?>

The function of the GD library, so...

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.