PHP image Watermark Class Code _php tutorial

Source: Internet
Author: User
Tags imagecopy
Support text watermark, image watermark
Support for watermark location random or fixed (ix Gongge)
Watermark Transparency Settings (both image watermark and text watermark support)
font, color, and size settings for text watermarks
Background transparency of picture watermark
Copy CodeThe code is as follows:
/**
* Add watermark class, support the text image watermark Transparency settings, watermark picture background transparent.
* Date: 2011-09-27
* Author: www.jb51.net
Use
* $obj = new Watermask ($imgFileName); Instantiating an Object
* $obj $waterType = 1; Type: 0 for text watermark, 1 for picture watermark
* $obj $transparent = 45; Watermark Transparency
* $obj $waterStr = ' www.jb51.net '; Watermark Text
* $obj $fontSize = 16; Text font size
* $obj $fontColor = array (255,0255); Watermark text color (RGB)
* $obj $fontFile = = ' Ahgbold.ttf '; Font files
* $obj->output (); Output watermark image file overlay into the input picture file
*/
Class watermask{
public $waterType = 1; Watermark Type: 0 for text watermark, 1 for picture watermark
Public $pos = 0; Watermark Location
Public $transparent = 45; Watermark Transparency

Public $waterStr = ' www.jb51.net '; Watermark Text
public $fontSize = 16; Text font size
Public $fontColor = Array (255,0,255); Watermark text color (RGB)
Public $fontFile = ' Ahgbold.ttf '; Font files

Public $waterImg = ' logo.png '; Watermark Picture

Private $srcImg = '; Picture to add a watermark
Private $im = '; Picture handle
Private $water _im = "; Watermark Picture Handle
Private $SRCIMG _info = "; Picture information
Private $WATERIMG _info = "; Watermark Picture Information
Private $str _w = "; Watermark Text Width
Private $str _h = "; Watermark Text Height
Private $x = '; Watermark X Coordinate
Private $y = '; Watermark y-coordinate

function __construct ($img) {//destructor
$this->srcimg = file_exists ($img)? $img: Die (' ". $img. '" Source file does not exist!) ');
}
Private Function Imginfo () {//Get information about the picture that needs to be added to the watermark and load the picture.
$this->srcimg_info = getimagesize ($this->srcimg);
Switch ($this->srcimg_info[2]) {
Case 3:
$this->im = imagecreatefrompng ($this->srcimg);
Break 1;
Case 2:
$this->im = imagecreatefromjpeg ($this->srcimg);
Break 1;
Case 1:
$this->im = imagecreatefromgif ($this->srcimg);
Break 1;
Default
Die (' Original picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF are supported. ');
}
}
Private Function Waterimginfo () {//Get the information of the watermark picture and load the picture.
$this->waterimg_info = getimagesize ($this->waterimg);
Switch ($this->waterimg_info[2]) {
Case 3:
$this->water_im = imagecreatefrompng ($this->waterimg);
Break 1;
Case 2:
$this->water_im = imagecreatefromjpeg ($this->waterimg);
Break 1;
Case 1:
$this->water_im = imagecreatefromgif ($this->waterimg);
Break 1;
Default
Die (' Watermark picture ('. $this->srcimg. ') is not formatted, only PNG, JPEG, GIF are supported. ');
}
}
Private Function Waterpos () {//Watermark position algorithm
Switch ($this->pos) {
Case 0://Random position
$this->x = rand (0, $this->srcimg_info[0]-$this->waterimg_info[0]);
$this->y = rand (0, $this->srcimg_info[1]-$this->waterimg_info[1]);
Break 1;
Case 1://upper Left
$this->x = 0;
$this->y = 0;
Break 1;
Case 2://Kaminaka
$this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;
$this->y = 0;
Break 1;
Case 3://upper Right
$this->x = $this->srcimg_info[0]-$this->waterimg_info[0];
$this->y = 0;
Break 1;
Case 4://Middle Left
$this->x = 0;
$this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;
Break 1;
Case 5://Middle
$this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;
$this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;
Break 1;
Case 6://Middle Right
$this->x = $this->srcimg_info[0]-$this->waterimg_info[0];
$this->y = ($this->srcimg_info[1]-$this->waterimg_info[1])/2;
Break 1;
Case 7://Lower Left
$this->x = 0;
$this->y = $this->srcimg_info[1]-$this->waterimg_info[1];
Break 1;
Case 8://Down
$this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;
$this->y = $this->srcimg_info[1]-$this->waterimg_info[1];
Break 1;
Default://Down Right
$this->x = $this->srcimg_info[0]-$this->waterimg_info[0];
$this->y = $this->srcimg_info[1]-$this->waterimg_info[1];
Break 1;
}
}
Private Function waterimg () {
if ($this->srcimg_info[0] <= $this->waterimg_info[0] | | $this->srcimg_info[1] <= $this->waterimg_ Info[1]) {
Die (' The watermark is bigger than the original! ');
}
$this->waterpos ();
$cut = Imagecreatetruecolor ($this->waterimg_info[0], $this->waterimg_info[1]);
Imagecopy ($cut, $this->im,0,0, $this->x, $this->y, $this->waterimg_info[0], $this->waterimg_info[1]) ;
$pct = $this->transparent;
Imagecopy ($cut, $this->water_im,0,0,0,0, $this->waterimg_info[0], $this->waterimg_info[1]);
Imagecopymerge ($this->im, $cut, $this->x, $this->y,0,0, $this->waterimg_info[0], $this->waterimg_ INFO[1], $pct);
}
Private Function Waterstr () {
$rect = Imagettfbbox ($this->fontsize,0, $this->fontfile, $this->waterstr);
$W = ABS ($rect [2]-$rect [6]);
$h = ABS ($rect [3]-$rect [7]);
$fontHeight = $this->fontsize;
$this->water_im = Imagecreatetruecolor ($w, $h);
Imagealphablending ($this->water_im,false);
Imagesavealpha ($this->water_im,true);
$white _alpha = Imagecolorallocatealpha ($this->water_im,255,255,255,127);
Imagefill ($this->water_im,0,0, $white _alpha);
$color = Imagecolorallocate ($this->water_im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor [2]);
Imagettftext ($this->water_im, $this->fontsize,0,0, $this->fontsize, $color, $this->fontfile, $this WATERSTR);
$this->waterimg_info = Array (0=> $w,1=> $h);
$this->waterimg ();
}
function output () {
$this->imginfo ();
if ($this->watertype = = 0) {
$this->waterstr ();
}else {
$this->waterimginfo ();
$this->waterimg ();
}
Switch ($this->srcimg_info[2]) {
Case 3:
Imagepng ($this->im, $this->srcimg);
Break 1;
Case 2:
Imagejpeg ($this->im, $this->srcimg);
Break 1;
Case 1:
Imagegif ($this->im, $this->srcimg);
Break 1;
Default
Die (' Add watermark failed! ');
Break
}
Imagedestroy ($this->im);
Imagedestroy ($this->water_im);
}
}
?>

http://www.bkjia.com/PHPjc/325912.html www.bkjia.com true http://www.bkjia.com/PHPjc/325912.html techarticle support text watermark, image watermark support watermark location random or fixed (nine Gongge) Watermark transparency settings (both image watermark and text watermark support) text watermark font, color 、...

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