PHP to add a watermark to the picture

Source: Internet
Author: User
Tags rand

Public function water ($filename, $water, $pos =0, $pct =80) {
Load watermark Picture
$info = $this->getimageinfo ($water);
if (!empty ($info [0])) {
$water _w = $info [0];
$water _h = $info [1];
$type = $info [' type '];
$fun = ' imagecreatefrom '. $type;
$waterimg = $fun ($water);
} else{
return false;
}
Load background picture
$info = $this->getimageinfo ($this->img);
if (!empty ($info [0])) {
$old _w = $info [0];
$old _h = $info [1];
$type = $info [' type '];
$fun = ' imagecreatefrom '. $type;
$oldimg = $fun ($this->img);
} else{
return false;
}
Clipping watermark
$water _w > $old _w && $water _w = $old _w;
$water _h > $old _h && $water _h = $old _h;

Watermark Location
Switch ($pos) {
Case 0://Random
$posX = rand (0, $old _w-$water _w));
$posY = rand (0, $old _h-$water _h));
Break
Case 1://1 for top left
$posX = 0;
$posY = 0;
Break
Case 2://2 for top center
$posX = ($old _w-$water _w)/2;
$posY = 0;
Break
Case 3://3 for top right
$posX = $old _w-$water _w;
$posY = 0;
Break
Case 4://4 for Central left
$posX = 0;
$posY = ($old _h-$water _h)/2;
Break
Case 5://5 centered in middle
$posX = ($old _w-$water _w)/2;
$posY = ($old _h-$water _h)/2;
Break
Case 6://6 for Middle right
$posX = $old _w-$water _w;
$posY = ($old _h-$water _h)/2;
Break
Case 7://7 to bottom left
$posX = 0;
$posY = $old _h-$water _h;
Break
Case 8://8 centered at bottom
$posX = ($old _w-$water _w)/2;
$posY = $old _h-$water _h;
Break
Case 9://9 to bottom right
$posX = $old _w-$water _w;
$posY = $old _h-$water _h;
Break
Default://Random
$posX = rand (0, $old _w-$water _w));
$posY = rand (0, $old _h-$water _h));
Break
}
Set the blending mode of the image
Imagealphablending ($oldimg, true);
Add watermark
Imagecopymerge ($oldimg, $waterimg, $posX, $posY, 0, 0, $water _w, $water _h, $pct);
$fun = ' image '. $type;
!@ $fun ($oldimg, $filename, MB) && die (' Save failed! ') check if directory exists and writable? '
Imagedestroy ($OLDIMG);
Imagedestroy ($WATERIMG);
return $filename;
       }

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.