<?php
class WaterGener{
private
$default_text= "Just for test" ;
private
$default_waterpic= "girl.jpg" ;
private
$default_qulity= 75 ;
//默认使用的字体
private
$font = ‘simhei.ttf‘ ; //定义字体
//默认的padding 的值
private
$padding= 5 ;
/**
构造函数
**/
function
__construct(){
}
//获取图片类型
private
function getImage($path){
if (!empty($path) && file_exists($path)) {
$water_info = @getimagesize($path);
$water_im;
switch ($water_info[ 2 ]) { //取得水印图片的格式
case
1 :$water_im = @imagecreatefromgif($path); break ;
case
2 :$water_im = @imagecreatefromjpeg($path); break ;
case
3 :$water_im = @imagecreatefrompng($path); break ;
default : return
1 ;
}
return
$water_im;
}
return
- 1 ;
}
public
function buildWaterImage($picture,$logo= "" ,$savePath= "demo.jpg" ){
//需要判断图片的类型,水印图片的类型
if (!empty($path) && file_exists($path)) return
- 1 ;
$logoImage =$ this ->getImage($logo=== "" ?$ this ->default_waterpic:$logo);
$photoImage =$ this ->getImage($picture);
if ($photoImage==- 1 ){
echo "没有找到图片" ;
return ;
}
imagealphablending($photoImage, true );
$logo_size = getimagesize($logo);
$logoW = $logo_size[ 0 ];
$logoH = $logo_size[ 1 ];
$picture_size = getimagesize($picture);
ImageCopy($photoImage, $logoImage, $picture_size[ 0 ]-$logoW-$ this ->padding, $picture_size[ 1 ]-$logoH-$ this ->padding, 0 , 0 , $logoW, $logoH);
ImageJPEG($photoImage,$savePath,$ this ->default_qulity); // output to browser or file
ImageDestroy($photoImage);
ImageDestroy($logoImage);
echo "success..." ;
}
public
function buildWaterText($picture,$text= "" ,$savePath= "demo.jpg" ){
//需要判断
$photoImage = $ this ->getImage($picture);
ImageAlphaBlending($photoImage, true );
$picture_size = getimagesize($picture);
$textcolor = imagecolorallocate($photoImage, 255 , 255 , 255 );
//解决乱码问题
//$text = iconv("GB2312", "UTF-8", $text); //将中文字转换为UTF8
imagettftext($photoImage, 20 , 0 , $ this ->padding, $picture_size[ 1 ]-($ this ->padding* 4 ), $textcolor, $ this ->font, $text); //将文字写到图片中
//imagestring($photoImage, 5, 0, 0,$text, $textcolor);
ImageJPEG($photoImage,$savePath,$ this ->default_qulity); // output to browser
ImageDestroy($photoImage);
}
}
?>
|