PHP Practical Image Watermark Effect code
<?php
Define (' Water ', 1);//Watermark mode (0, text watermark, 1 image watermark)
Define (' waterimg ', './water.jpg ');//Watermark Picture Address
Define (' Waternum ', 0);/watermark location, 0 is random, 1-9 is the top left middle right middle left middle left center bottom right
Define (' Waterx ', 81)//Watermark length
Define (' watery ', 81);//Watermark High
Define (' Watertype ', ' image/jpeg ')/Watermark picture type
Define (' Watertext ', ' love to collect '); Watermark text (must first convert to UTF-8 to achieve Chinese watermark)
Define (' Watertextcolor ', ' #000000 ');/watermark Text color
Define (' Watertextsize ', 50);/Watermark Text size px
Define (' Watertextfont ', ' C:windowsfontssimfang.ttf ')/Watermark text font (imitation)
Define (' WATERPTC ', 50);/watermark Transparency
Watermark Default Settings complete
Class water
{
private $img;//Picture address
Private $img _type;//Picture type
Private $img _mime;//Picture actual type
Private $img _x;//Picture length
Private $img _y;//Picture high
Private $is _water;//Whether you can add watermarks, Boolean
Private $water _img;//watermark picture
Private $water _text;//Picture text (UTF-8)
Private $water _num;//Watermark location (may not be needed)
Private $water _w = width of waterx;//water
Private $water _h = watery;//water Long
Private $water The x position of the _x;//watermark
Private $water The Y position of the _y;//watermark
Private $src The length difference between the _x;//canvas and the water two graph
Private $SRC The height difference between the _y;//canvas and the water two graph
Private $err _msg;//Picture address
Public function __construct ($img, $img _type)
{
if (file_exists ($img) = = FALSE)
{
$this->err_img (' no_img ');
}
$this->img = $img;
$this->img_type = write picture type $img _type;//
$this->CHECK_GD ()//Check the GD library
$this->do_water ()//To Judge Watermark mode
$this->type_img ()//To determine the type of picture
}
/*
* Check the GD library
*/
Public Function check_gd () {
if (function_exists ("gd_info") = = FALSE)
{
$this->err_img (' no_gd ');
}
}
/*
* Processing watermark mode and water file type
*/
Public Function Do_water ()
{
if (Water = 1)//Picture watermark
{
$this->img_water ()//processing Water pictures
}else{//text watermark
$this->text_xy ()///Processing text watermark Picture
}
}
/*
* Processing Water pictures
*/
Public Function Img_water ()
{
if (file_exists (waterimg) = = FALSE)
{
$this->err_img (' no_waterimg ');
Using text watermarks
}
Switch (watertype)
{
Case ' Image/jpeg ':
$this->water_img = Imagecreatefromjpeg (waterimg);
Break
}
}
/*
* Get the long width of the text and calculate the long width difference of the two graph
*/
Public Function Text_xy ()
{
$str = Iconv (' GB2312 ', ' UTF-8 ', watertext);
$temp = Imagettfbbox (50,0,watertextfont, $STR);//Get the range of text that uses TrueType fonts
$w = $temp [2]-$temp [6];
$h = $temp [3]-$temp [7];
$this->src_x = $this->img_x-$w;
$this->src_y = $this->img_y-$h;
}
/*
* To determine whether the image can be added watermark, compare size
*/
Public Function Is_water ()
{
if (Water = 1)//Picture watermark
{
$this->src_x = $this->img_x-waterx;
$this->src_y = $this->img_y-watery;
if ($this->src_x <= 0 && $this->src_y <= 0)
{
$this->err_img (' Too_small ');
}
}else{//text watermark
if ($this->src_x <= 0 && $this->src_y <=0)
{
$this->err_img (' Too_small ');
}
}
}
/*
* Processing Watermark Location
* Get the coordinates of the watermark position
*/
Public Function Water_num ()
{
Switch (waternum)
{
Case 0://Random
$this->water_x = rand (0, ($this->img_x-$this->water_w));
$this->water_y = rand (0, ($this->img_y-$this->water_h));
Break
Case 1://Top Left
$this->water_x = 0;
$this->water_y = 0;
Break
Case 2://Top Center
$this->water_x = ($this->img_x-$this->water_w)/2;
$this->water_y = 0;
Break
Case 3://Top Right
$this->water_x = $this->img_x-$this->water_w;
$this->water_y = 0;
Break
Case 4://Middle Left
$this->water_x = 0;
$this->water_y = ($this->img_y-$this->water_h)/2;
Break
Case 5://Middle Center
$this->water_x = ($this->img_x-$this->water_w)/2;
$this->water_y = ($this->img_y-$this->water_h)/2;
Break
Case 6://Center Right
$this->water_x = $this->img_x-$this->water_w;
$this->water_y = ($this->img_y-$this->water_h)/2;
Break
Case 7://Bottom Left
$this->water_x = 0;
$this->water_y = $this->img_y-$this->water_h;
Break
Case 8://Bottom Center
$this->water_x = ($this->img_x-$this->water_w)/2;
$this->water_y = $this->img_y-$this->water_h;
Break
Case 9://Bottom Right
$this->water_x = $this->img_x-$this->water_w;
$this->water_y = $this->img_y-$this->water_h;
Break
default://Random
$this->water_x = rand (0, ($this->img_x-$this->water_w));
$this->water_y = rand (0, ($this->img_y-$this->water_h));
}
}
/*
* Judge Picture Type
*/
Public Function type_img ()
{
Switch ($this->img_type)
{
Case ' Image/jpeg ':
Header ("Content-type:image/jpeg");
$this->img = imagecreatefromjpeg ($this->img);
Break
}
$this->img_water ()//processing Water pictures
$this->img_x = imagesx ($this->img);
$this->img_y = Imagesy ($this->img);
$this->water_num ()//To determine watermark location
$this->is_water ();
$this->make_img ();
}
/*
* Make image watermark
*/
Public Function make_img () {
Imagecopymerge ($this->img, $this->water_img, $this->water_x, $this->water_y,0,0,waterx,watery, WATERPTC);
Imagejpeg ($this->img);
}
/*
* Make text watermark
*/
Public Function Make_text ()
{
echo ' Test ';
}
/*
* Return error message
*/
Public Function err_img ($msg)
{
Switch ($msg)
{
Case ' NO_GD ':
$this->err_msg = ' No GD library installed ';
Break
Case ' no_img ':
$this->err_msg = ' picture does not exist ';
Break
Case ' no_waterimg ':
$this->err_msg = ' water picture does not exist ';
Break
Case ' Too_small ':
$this->err_msg = ' picture too small ';
Break
}
Die ($this->err_msg);//Output error message
}
Public Function __destruct ()
{
Imagedestroy ($this->img);
Imagedestroy ($this->img_water);
}
}
$img = new Water ('./100.jpg ', ' image/jpeg ');
?>