TP comes with an image class and can be used to add watermarks to an image.
Here we have improved:
1. PNG watermark transparency
2. Adjust the watermark quality (only in JPG format)
The Code is as follows:
Red indicates the original system
Green indicates the modified
/**
+ --------------------
* Add a watermark to an image
+ --------------------
* @ Static public
+ --------------------
* @ Param string $ source original file name
* @ Param string $ water watermark image
* @ Param string $ savename watermark image name
* @ Param string $ transparency of the Alpha watermark
+ --------------------
* @ Return string
+ --------------------
* @ Throws thinkexecption
+ --------------------
*/
Static public function water ($ source, $ water, $ savename = NULL, $ alpha = 80 ){
// Check whether the file exists
If (! File_exists ($ source) |! File_exists ($ water ))
Return false;
// Image Information
$ SINFO = self: getimageinfo ($ source );
$ Winfo = self: getimageinfo ($ water );
// If the image is smaller than the watermark image, no image is generated.
If ($ SINFO ["width"] <$ winfo ["width"] | $ SINFO ['height'] <$ winfo ['height'])
Return false;
// Create an image
$ Screatefun = "imagecreatefrom". $ SINFO ['type'];
$ Simage = $ screatefun ($ source );
$ Wcreatefun = "imagecreatefrom". $ winfo ['type'];
$ Wimage = $ wcreatefun ($ water );
// Set the mixed color mode of the image
Imagealphablending ($ wimage, true );
// Image position, right-aligned by default
$ Posy = $ SINFO ["height"]-$ winfo ["height"];
$ Posx = $ SINFO ["width"]-$ winfo ["width"];
/* Use imagecopy */
Imagecopy ($ simage,
$ Wimage, $ posx, $ Posy, 0, 0, $ winfo ['width'], $ winfo ['height']);
// Generate a hybrid image. This is the system's
//
Imagecopymerge ($ simage, $ wimage, $ posx, $ Posy, 0, 0, $ winfo ['width'], $ winfo ['height'], $ alpha );
// Output image
$ Imagefun = 'image'. $ SINFO ['type'];
// If no save file name is provided, the original image name is used by default.
If (! $ Savename ){
$ Savename = $ source;
@ Unlink ($ source );
}
// Save the image. If it is JPG, set the watermark quality.
If
($ SINFO ['type'] = "jpg" | $ SINFO ['type'] = "Jpeg "){
Imagejpeg ($ simage,
$ Savename, 90); // 3rd parameters, even if the quality is large, because only imagejpeg supports this parameter
}
Else {
$ Imagefun ($ simage,
$ Savename );
}
// $ Imagefun ($ simage,
$ Savename );
Imagedestroy ($ simage );
}
Finally, we would like to thank konakona for providing it.