PHP image Watermark Class Code _php example

Source: Internet
Author: User
Tags abs imagecopy rand
Support text watermark, image watermark
Watermark is supported at random or fixed position (nine Sudoku)
Watermark Transparency Settings (both image watermark and text watermark are supported)
font, color, size settings for text watermarks
The background of a picture watermark is transparent
Copy Code code as follows:

<?php
/**
* Add watermark class, support the transparency setting of text picture watermark, the watermark picture background is 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 Image 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 picture file overlay to the input picture file
*/
Class watermask{
public $waterType = 1; Watermark Type: 0 for text watermark, 1 for Image 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 you want to add a watermark to 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 ('. $this->srcimg. ') is not in the correct format, 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 ('. $this->srcimg. ') is not in the correct format, only PNG, JPEG, GIF are supported. ');
}
}
Private Function Waterpos () {//Watermark location 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://Up Left
$this->x = 0;
$this->y = 0;
Break 1;
Case 2://Ober Chong
$this->x = ($this->srcimg_info[0]-$this->waterimg_info[0])/2;
$this->y = 0;
Break 1;
Case 3://Up 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://Medium
$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://Down 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 (' watermark is bigger than 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);
}
}
?>

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.