Home >Backend Development >PHP Tutorial >Add watermark to images in php_PHP tutorial
/* ClassName: ImageHandler
* Filename: ImageHandler.phpcls
* Author: Tony Don
* Date: 2008-03-27
* @Copyright txdnet .com
* Function: Handler Images
*/
class ImageHandler
{
var $dst_img;// Target file
var $h_src; // Image resource handle
var $h_dst; // New image handle
var $h_mask; // Watermark handle
var $img_create_quality = 100; // Image generation quality
var $img_display_quality = 80; // Image display Quality, the default is 75
var $img_scale = 0;// Image scaling
var $src_w = 0;// Original image width
var $src_h = 0;// Original image height
var $dst_w = 0;//Total width of new image
var $dst_h = 0;//Total height of new image
var $fill_w;//Width of filled graphics
var $fill_h;//Filled graphics Height
var $copy_w;// Copy graphic width
var $copy_h;// Copy graphic height
var $src_x = 0;// Original drawing starting abscissa
var $src_y = 0;// The starting ordinate of the original picture drawing
var $start_x;// The starting abscissa of the new picture drawing
var $start_y;//The starting ordinate of the new picture drawing
var $mask_word; // Watermark text
var $mask_img; // Watermark image
var $mask_pos_x = 0; // Watermark abscissa
var $mask_pos_y = 0; // Watermark ordinate
var $mask_offset_x = 5;//Watermark horizontal offset
var $mask_offset_y = 5;//Watermark vertical offset
var $font_w;//Watermark font width
var $font_h;//Watermark font height
var $mask_w;// Watermark width
var $mask_h;// Watermark height
var $mask_font_color = "#000000";// Watermark text color
var $mask_font = 5;// Watermark font
var $font_size; // Size
var $mask_position = 0; // Watermark position
var $mask_img_pct = 50; // Image merging degree, the larger the value, the lower the merging process
var $ mask_txt_pct = 50;//The degree of text merging, the smaller the value, the lower the merging process
var $img_border_size = 0;//Picture border size
var $img_border_color;//Picture border color
var $_flip_x =0;//Number of horizontal flips
var $_flip_y=0;//Number of vertical flips
var $cut_type=0;//Cut or reduce type
var $img_type;//File type
// File type definition, and points out the function to output the image
var $all_type = array(
"jpg" => array("output"=>"imagejpeg"),
"gif" => array("output"=>"imagegif"),
"png" => array("output"=>"imagepng"),
"wbmp" = > array("output"=>"image2wbmp"),
"jpeg" => array("output"=>"imagejpeg"));
/**
* Constructor
*/
function ImageHandler()
{
$this->mask_font_color = "#ffffff";
$this->font = 2;
$this->font_size = 12;
}
/**
* Get the width of the image
*/
function getImgWidth($src)
{
return imagesx($src);
}
/**
* Get the height of the picture
*/
function getImgHeight($src)
{
return imagesy($src);
}
/**
* Set the image generation path
*
* @param string $src_img Image generation path
*/
function setSrcImg($src_img, $img_type=null)
{
if(! file_exists($src_img))
🎜> $this ->img_type = $img_type;
}
}
$this->_checkValid($this->img_type);
"file_get_contents"))
= fopen ($src_img, "r");
while (!feof ($handle))
fclose ($handle);
}
; this->src_w = $this->getImgWidth($this->h_src);
$this->src_h = $this->getImgHeight($this->h_src);
}
/**
* Set the image generation path
*
* @param string $dst_img Image generation path
*/
function setDstImg($dst_img)
{
$arr = explode('/',$dst_img);
$last = array_pop ($arr);
$path = implode('/',$arr);
$this->_mkdirs($path);
$this->dst_img = $dst_img;
}
/**
* Set the display quality of the image
*
* @param string $n Quality
*/
function setImgDisplayQuality($n)
{
$this->img_display_quality = (int)$n;
}
/**
* Set the image generation quality
*
* @param string $n Quality
*/
function setImgCreateQuality($n)
{
$this->img_create_quality = (int)$n;
}
/**
* Set text watermark
*
* @param string $word Watermark text
* @param integer $font Watermark font
* @param string $color Watermark font color
*/
function setMaskWord($word)
{
$this->mask_word .= $word;
}
/**
* Set font color
*
* @param string $color Font color
*/
function setMaskFontColor($color="#ffffff")
{
$this->mask_font_color = $color;
}
?>