TP5使用加水印功能(加圖片水印和文字水印)
由於項目需要圖片加水印,正好使用TP5框架,下面記錄一下使用方法:
1、安裝擴展
使用Composer安裝ThinkPHP5的圖像處理類庫:
composer require topthink/think-image
2:、使用:
/**
* 圖片加水印 文字水印和圖片水印 \think\Image類添加tilewater方法,平鋪水印
*/
public function water()
{
$image = \think\Image::open('./addWater.png'); //要加水印的圖片
// 返回圖片的寬度
$width = $image->width();
// 返回圖片的高度
$height = $image->height();
// 返回圖片的類型
$type = $image->type();
// 返回圖片的mime類型
$mime = $image->mime();
// 返回圖片的尺寸數組 0 圖片寬度 1 圖片高度
$size = $image->size();
$image->water('./water.png',\think\Image::WATER_NORTHWEST,50)->save('water_image.png'); //加圖片水印後保存爲 water_image.png
$image->tilewater('./water.png',50)->save('water_image2.png'); //圖片平鋪水印
$image = \think\Image::open('./addWater.png'); //要加水印的圖片
$image->text('十年磨一劍 - 爲API開發設計的高性能框架','simkai.ttf',20,'#ffffff')->save('text_image.png'); //文字水印
}
/**
* 圖片加多個文字水印
*/
public function test()
{
$filename = './zhengshu.jpg';
$image = \think\Image::open($filename); //要加水印的圖片
$image->text('僅適用於信安在線企業認證0','simkai.ttf',50,'#CCCCCC',\think\Image::WATER_SOUTHEAST,0,50)->save('./down/text_image2.png');
// unlink($filename); //刪除文件 unlink($filename); //刪除文件
$image = \think\Image::open('./down/text_image.png'); //要加水印的圖片
$image->text('僅適用於信安在線企業認證1','simkai.ttf',50,'#CCCCCC',\think\Image::WATER_SOUTHWEST,0,50)->save('./down/text_image2.png');
$image->text('僅適用於信安在線企業認證2','simkai.ttf',50,'#CCCCCC',\think\Image::WATER_NORTHWEST,0,50)->save('./down/text_image2.png');
$image->text('僅適用於信安在線企業認證3','simkai.ttf',50,'#CCCCCC',\think\Image::WATER_NORTHEAST,0,50)->save('./down/text_image2.png');
$image->text('僅適用於信安在線企業認證4','simkai.ttf',50,'#CCCCCC',\think\Image::WATER_CENTER,0,50)->save('./down/text_image2.png');
// $filename = './down/text_image.png';
// echo $filename;
// $image->text('僅適用於信安在線企業認證5','simkai.ttf',50,'#8B7B8B',\think\Image::WATER_SOUTH,0,50)->save('./down/text_image.png');
// $image->text('僅適用於信安在線企業認證6','simkai.ttf',50,'#8B7B8B',\think\Image::WATER_EAST,0,50)->save('./down/text_image.png');
//
// $image->text('僅適用於信安在線企業認證7','simkai.ttf',50,'#8B7B8B',\think\Image::WATER_NORTH,0,50)->save('./down/text_image.png');
// $image->text('僅適用於信安在線企業認證8','simkai.ttf',50,'#8B7B8B',\think\Image::WATER_WEST,0,50)->save('./down/text_image.png');
}
3、添加平鋪圖片水印方法
在\think\Image類添加tilewater方法,平鋪水印
/**
* 添加圖片水印平鋪
*
* @param string $source 水印圖片路徑
* @param int $alpha 透明度
* @return $this
*/
public function tilewater($source, $alpha = 100)
{
if (!is_file($source)) {
throw new ImageException('水印圖像不存在');
}
//獲取水印圖像信息
$info = getimagesize($source);
if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
throw new ImageException('非法水印文件');
}
//創建水印圖像資源
$fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
$water = $fun($source);
//設定水印圖像的混色模式
imagealphablending($water, true);
do {
//添加水印
$src = imagecreatetruecolor($info[0], $info[1]);
// 調整默認顏色
$color = imagecolorallocate($src, 255, 255, 255);
imagefill($src, 0, 0, $color);
//循環平鋪水印
for ($x = 0; $x < $this->info['width']-10; $x) {
for ($y = 0; $y < $this->info['height']-10; $y) {
imagecopy($src, $this->im, 0, 0, $x, $y, $info[0], $info[1]);
imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha);
$y += $info[1];
}
$x += $info[0];
}
//銷燬零時圖片資源
imagedestroy($src);
} while (!empty($this->gif) && $this->gifNext());
//銷燬水印資源
imagedestroy($water);
return $this;
}
4、效果:
(1)圖片多個文字水印
(2)圖片平鋪水印
(完)
圖片加多個文字水印應該有更好的方法,目前這一種太笨了。發現更好的方法後再分享。