PHP Add a watermark to a picture _php tutorial

Source: Internet
Author: User
/**
* Image plus watermark (for 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 station left, 5: Bottom of Right
* @param $alpha Transparency--0: Completely transparent, 100: completely opaque
*
* @return Success--The new image address after adding watermark
* Failure---1: The original file does not exist,-2: Watermark picture does not exist,-3: Original file image object failed to build
*-4: Watermark file Image Object build failed-5: New picture saved after Watermark failed
*/
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; The original file image object failed to build
}
$WATERIMGOBJ = Image_create_from_ext ($WATERIMG);
if (! $waterImgObj) {
return-4; Failed to build Watermark file image Object
}
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 centering
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;
}

http://www.bkjia.com/PHPjc/486057.html www.bkjia.com true http://www.bkjia.com/PHPjc/486057.html techarticle PHP/** * Image watermark (for png/jpg/gif format) * * @author FLYNETCN * * @param $srcImg Original image * @param $waterImg watermark image * @param $savep Ath Save path * @para ...

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