Yesterday afternoon colleague asked me a question of PHP, is to add watermark to the picture, PHP I also smattering, online information to find a pass, I wrote a watermark PHP class.
The specific code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?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);
}
} ?>
|
The above code has some flaws:
1, the location of watermark generation, such as can be in the middle, upper right corner, lower right corner, and so on. needs to be perfected.
2, if it is added text watermark, how to calculate the location of the text watermark? Calculate the width and height of each text? This still needs to be perfected.
3, exception handling. PHP inside processing exception, I seem to have not touched.
The above code call method:
1 2 3 4 5 6 |
<?php include_once "water.class.php" ; $water= new WaterGener(); $water->buildWaterImage( "girl.jpg" , "logo.gif" , "demo1.jpg" ); $water->buildWaterText( "girl.jpg" , "开源中国" ); ?> |