PHP implementation Picture Add watermark function _php instance

Source: Internet
Author: User

Copy Code code as follows:

<?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;
}

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.