<?php
/**
* Image watermark (applicable to png/jpg/gif format)
*
* @author FLYNETCN
*
* @param $srcImg Original picture
* @param $waterImg watermark Picture
* @param $savepath Save path
* @param $savename Save name
* @param $positon Watermark Location
* 1: Top left, 2: Top right, 3: center, 4: Bottom Bureau left, 5: bottom of the right
* @param $alpha Transparency-0: Fully transparent, 100: completely opaque
*
* @return Success--New image address after watermark
* Failed---1: Original file does not exist,-2: Watermark picture does not exist,-3: Original file image Object build failed
*-4: Watermark file Image Object build failed-5: New picture after Watermark failed to save
*/
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; Watermark picture does not exist
}
$SRCIMGOBJ = Image_create_from_ext ($SRCIMG);
if (! $srcImgObj) {
return-3; Original file image object failed to build
}
$WATERIMGOBJ = Image_create_from_ext ($WATERIMG);
if (! $waterImgObj) {
return-4; Watermark file Image Object build failed
}
Switch ($positon) {
1 Top Left
Case 1: $x = $y = 0; Break
2 Top of 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 bottom 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;
}