Afternoon nothing dry, wrote a picture and watermark processing class, this class to achieve a picture with text watermark, add picture watermark, and achieve the function of transparency, for you to learn and exchange
/**
* Add watermark class, support text, picture watermark and the setting of transparency, watermark picture background transparent.
* @author LITX date:2011-12-05 3 o'clock in the afternoon in the fast-seeding research and development center of Kelon
*/
Class Watermask
{
/**
* Watermark Type
* @var int $waterType 0 for text watermark; 1 for picture watermark
*/
Private $waterType = 1;
/**
* Watermark Location Type
* @var int $pos default to 9 (lower right)
*/
Private $pos = 9;
/**
* Watermark Transparency
* @var int $transparent watermark Transparency (the smaller the value the more transparent)
*/
Private $transparent = 20;
/**
* If it is a text watermark, you need to add the watermark text
* @var string $waterStr default value (Li Tiexiong personal folio)
*/
Private $waterStr = ' personal folio ';
/**
* Text font size
* @var int $fontSize font size
*/
Private $fontSize = 14;
/**
* Watermark Text color (RGB)
* @var array $fontColor watermark text color (RGB)
*/
Private $fontColor = Array (255, 255, 255);
/**
* Font File
* @var Unknown_type
*/
Private $fontFile = ' Ahgbold.ttf ';
/**
* Watermark Image
* @var String $WATERIMG
*/
Private $waterImg = ' logo.png ';
/**
* Need to add a watermark picture
* @var String $SRCIMG
*/
Private $srcImg = ';
/**
* Picture handle
* @var String $im
*/
Private $im = ';
/**
* Watermark Picture Handle
* @var string $water _im
*/
Private $water _im = ";
/**
* Picture Information
* @var Array $srcImg _info
*/
Private $SRCIMG _info = ";
/**
* Watermark Picture Information
* @var Array $waterImg _info
*/
Private $WATERIMG _info = ";
/**
* Watermark Text width
* @var int $str _w
*/
Private $str _w = ";
/**
* Watermark Text Height
* @var int $str _h
*/
Private $str _h = ";
/**
* Watermark X coordinate
* @var int $x
*/
Private $x = ';
/**
* Watermark Y coordinate
* @var int $y
*/
Private $y = ';
/**
* constructor to initialize the source image by passing in a source image that needs to be watermarked
* @param string $img The source image to be watermarked
*/
Public function __construct ($img)
{
if (file_exists ($img)) {//source file exists
$this-srcimg = $img;
}else{//source file does not exist
echo ' source file '. $img. ' does not exist, please check to see if the file path is correct ';
Exit ();
}
}
/**
* Get information about images you need to add watermarks to and load images
*/
Public Function Imginfo ()
{
$this-Srcimg_info = getimagesize ($this-srcimg);
Var_dump ($this-Srcimg_info); exit ();
Switch ($this-srcimg_info[2]) {
Case 3://png
$this im = Imagecreatefrompng ($this-srcimg);
Break 1;
Case 2://Jpeg/jpg
$this im = Imagecreatefromjpeg ($this-srcimg);
Break 1;
Case 1://gif
$this im = Imagecreatefromgif ($this-srcimg);
Break 1;
Default:
echo ' source picture file '. Srcimg, $this. ' The format is not correct, currently this function only supports PNG, JPEG, GIF image watermark function ';
Exit ();
}
}
/**
* Get information about the watermark image and load it into pictures
*/
Private Function Waterimginfo ()
{
$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:
echo ' source picture file '. Srcimg, $this. ' The format is not correct, currently this function only supports PNG, JPEG, GIF image watermark function ';
Exit ();
}
}
/**
* Watermark Location algorithm
*/
Private Function Waterpos ()
{
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 = 20;
$this, y = 20;
Break 1;
Case 2://Kaminaka
$this x = ($this srcimg_info[0]-$this-waterimg_info[0])/2;
$this, y = 20;
Break 1;
Case 3://upper Right
$this x = $this, srcimg_info[0]-$this-waterimg_info[0];
$this, y = 20;
Break 1;
Case 4://Middle Left
$this x = 20;
$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]-20;
$this y = ($this, srcimg_info[1]-$this-waterimg_info[1])/2;
Break 1;
Case 7://Lower Left
$this x = 20;
$this y = $this, srcimg_info[1]-$this-waterimg_info[1]-20;
Break 1;
Case 8://www.2cto.com
$this x = ($this srcimg_info[0]-$this-waterimg_info[0])/2;
$this y = $this, srcimg_info[1]-$this-waterimg_info[1]-20;
Break 1;
Case 9://Lower Right
$this x = $this, srcimg_info[0]-$this-waterimg_info[0]-20;
$this y = $this, srcimg_info[1]-$this-waterimg_info[1]-20;
Break 1;
Default://Down Right
$this x = $this, srcimg_info[0]-$this-waterimg_info[0]-20;
$this y = $this, srcimg_info[1]-$this-waterimg_info[1]-20;
Break 1;
}
}
/**
* Plus picture watermark
*/
Private Function Waterimg ()
{
if ($this, srcimg_info[0] <= $this, waterimg_info[0] | | $this, srcimg_info[1] <= $this waterimg _info[1]) {
echo ' picture size is too small to add watermarks, please upload a large picture ';
Exit ();
}
Calculate Watermark Location
$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 = transparent, $this;
Imagecopy ($cut, $this-water_im, 0, 0, 0, 0, $this-waterimg_info[0],
$this, waterimg_info[1]);
Compositing a picture with a watermark picture
Imagecopymerge ($this im, $cut, $this, X, $this-y, 0, 0, $this-waterimg_info[0], $this-Waterim G_INFO[1], $pct);
}
/**
* Add text watermark
*/
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 = FontSize, $this;
$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 ();
}
/**
* Watermark Image Output
*/
Public 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
}
Post-image-synthesis destruction processing
Imagedestroy ($this, IM);
Imagedestroy ($this-Water_im);
}
}
Examples of Use methods:
Instantiating an Object
$obj = new Watermask (' img/10451.jpg ');
Type: 0 for text watermark, 1 for picture watermark
$obj->watertype = 0;
Watermark transparency, the smaller the value, the greater the transparency
$obj->transparent = 15;
Watermark Text
$obj->waterstr = ' Happy Birthday ';
Watermark Picture
$obj->waterimg = ";//Watermark Picture
Text font size
$obj->fontsize = 14;
Watermark text color (RGB)
$obj->fontcolor = Array (255,255,100);
Font files
$obj->fontfile = ' Stcaiyun.ttf ';
Output watermark image file overlay into the input picture file
$obj->output ();
The use of the method is relatively simple, but also very practical.
Author ltx851201
http://www.bkjia.com/PHPjc/478502.html www.bkjia.com true http://www.bkjia.com/PHPjc/478502.html techarticle afternoon nothing to do, wrote a picture and watermark processing class, this class to achieve a picture with text watermark, add picture watermark, and achieve the function of transparency, for you to learn and ...