Php: Function code for adding watermarks and thumbnails to images _ PHP Tutorial

Source: Internet
Author: User
Php adds the watermark function and thumbnail function code to the image. Php: Function code used to add watermarks to images and thumbnails *** used to add watermarks to images ** @paramstringincludesimgtarget image format: .imagespic.jpg * @ paramstring $ waterImg watermark php: Function code used to add watermarks and thumbnails to images
/**
* Watermarking an image
* @ Param string $ desImg the target image parameter format is./images/pic.jpg.
* @ Param string $ the watermark image parameter format is the same as above. the watermark image format is png and the background is transparent.
* @ Param int positon Watermark Position 1: Top left 2: Top Right 3: Center 4: bottom left 5: bottom right
* @ Param bool $ whether to save saveas as. the default value is false, indicating that the source image is overwritten.
* @ Param int $ opacity of the alpha watermark image
* @ Return string $ savepath path of the new image
***/
Function watermark ($ desImg, $ waterImg, $ positon = 1, $ saveas = false, $ alpha = 30)
{
// Obtain the basic information of the object.
$ Temp = pathinfo ($ desImg );
$ Name = $ temp ["basename"]; // file name
$ Path = $ temp ["dirname"]; // folder where the file is located
$ Extension = $ temp ["extension"]; // file extension
If ($ saveas)
{
// Save
$ Name = rtrim ($ name, ". $ extension"). "_ 2."; // rename
$ Savepath = $ path. "/". $ name. $ extension;
}
Else
{
// Overwrite the source image without saving it as a record
$ Savepath = $ path. "/". $ name;
}
$ Info = getImageInfo ($ desImg); // Obtain the target image information
$ Info2 = getImageInfo ($ waterImg); // Obtain watermark image information

$ DesImg = create ($ desImg); // create from source image
$ WaterImg = create ($ waterImg); // create a watermark image
// Position 1: Top left
If ($ positon = 1)
{
$ X = 0;
$ Y = 0;
}
// Position 2: top to right
If ($ positon = 2)
{
$ X = $ info [0]-$ info2 [0];
$ Y = 0;
}
// Position 3: Center
If ($ positon = 3)
{
$ X = ($ info [0]-$ info2 [0])/2;
$ Y = ($ info [1]-$ info2 [1])/2;
}
// Position 4: bottom left
If ($ positon = 4)
{
$ X = 0;
$ Y = $ info [1]-$ info2 [1];
}
// Position 5: bottom right
If ($ positon = 5)
{
$ X = $ info [0]-$ info2 [0];
$ Y = $ info [1]-$ info2 [1];
}
Imagecopymerge ($ desImg, $ waterImg, $ x, $ y, 0, $ info2 [0], $ info2 [1], $ alpha );
Imagejpeg ($ desImg, $ savepath );
Imagedestroy ($ desImg );
Imagedestroy ($ waterImg );
Return $ savepath;
}
/**
* Obtain the image information, such as width, height, and image/type.
* @ Param string $ src Image path
* @ Return array
***/
Function getImageInfo ($ src)
{
Return getimagesize ($ src );
}
/**
* Create an image and return the resource type
* @ Param string $ src Image path
* @ Return resource $ im: returned resource type
***/
Function create ($ src)
{
$ Info = getImageInfo ($ src );
Switch ($ info [2])
{
Case 1:
$ Im = imagecreatefromgif ($ src );
Break;
Case 2:
$ Im = imagecreatefromjpeg ($ src );
Break;
Case 3:
$ Im = imagecreatefrompng ($ src );
Break;
}
Return $ im;
}
/**
* Thumbnail main function
* @ Param string $ src Image path
* @ Param int $ w thumbnail width
* @ Param int $ h Thumbnail height
* @ Return mixed return the thumbnail path
***/

Function resize ($ src, $ w, $ h)
{
$ Temp = pathinfo ($ src );
$ Name = $ temp ["basename"]; // file name
$ Dir = $ temp ["dirname"]; // folder where the file is located
$ Extension = $ temp ["extension"]; // file extension
$ Savepath = "{$ dir}/your saved name).thumb.jpg"; // saved path of the thumbnail. the new file name is * .thumb.jpg.

// Obtain basic image information
$ Info = getImageInfo ($ src );
$ Width = $ info [0]; // get the image width
$ Height = $ info [1]; // get the image height
$ Per1 = round ($ width/$ height, 2); // calculate the aspect ratio of the original image.
$ Per2 = round ($ w/$ h, 2); // calculate the thumbnail aspect ratio.

// Calculate the scaling ratio
If ($ per1> $ per2 | $ per1 = $ per2)
{
// If the aspect ratio of the source image is greater than or equal to the aspect ratio of the thumbnail, the width prevails.
$ Per = $ w/$ width;
}
If ($ per1 <$ per2)
{
// If the aspect ratio of the source image is smaller than the aspect ratio of the thumbnail, the height prevails.
$ Per = $ h/$ height;
}
$ Temp_w = intval ($ width * $ per); // calculates the width of the scaled source image.
$ Temp_h = intval ($ height * $ per); // calculate the scaled height of the source image.
$ Temp_img = imagecreatetruecolor ($ temp_w, $ temp_h); // Create a canvas
$ Im = create ($ src );
Imagecopyresampled ($ temp_img, $ im, 0, 0, 0, $ temp_w, $ temp_h, $ width, $ height );
If ($ per1> $ per2)
{
Imagejpeg ($ temp_img, $ savepath );
Return addBg ($ savepath, $ w, $ h, "w ");
// The width takes precedence and the background is added when the height is insufficient after scaling.
}
If ($ per1 = $ per2)
{
Imagejpeg ($ temp_img, $ savepath );
Return $ savepath;
// Proportional scaling
}
If ($ per1 <$ per2)
{
Imagejpeg ($ temp_img, $ savepath );

Return addBg ($ savepath, $ w, $ h, "h ");
// The height takes precedence and the background is added when the width is insufficient after scaling.
}
}
/**
* Add background
* @ Param string $ src Image path
* @ Param int $ w background image width
* @ Param int $ h background image height
* @ Param String $ first determines the final position of the image. w width first h height first wh: Proportional
* @ Return returns the image with the background added.
***/
Function addBg ($ src, $ w, $ h, $ fisrt = "w ")
{
$ Bg = imagecreatetruecolor ($ w, $ h );
$ White = imagecolorallocate ($ bg, 255,255,255 );
Imagefill ($ bg, $ white); // fill the background

// Obtain the target image information
$ Info = getImageInfo ($ src );
$ Width = $ info [0]; // target image width
$ Height = $ info [1]; // target Image height
$ Img = create ($ src );
If ($ fisrt = "wh ")
{
// Proportional scaling
Return $ src;
}
Else
{
If ($ fisrt = "w ")
{
$ X = 0;
$ Y = ($ h-$ height)/2; // center vertically
}
If ($ fisrt = "h ")
{
$ X = ($ w-$ width)/2; // horizontally centered
$ Y = 0;
}
Imagecopymerge ($ bg, $ img, $ x, $ y, 100, $ width, $ height );
Imagejpeg ($ bg, $ src );
Imagedestroy ($ bg );
Imagedestroy ($ img );
Return $ src;
}

}


Http://www.bkjia.com/PHPjc/444990.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/444990.htmlTechArticlephp for the image watermarking function and thumbnail function code/*** for the image watermarking * @ param string $ desImg target image parameter format is. /images/pic.jpg * @ param string $ waterImg watermark...

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.