今天是:2025年4月7日 星期一
记住用户名密码
2:、使用:
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 | /** * 图片加水印 文字水印和图片水印 \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'); } |
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 | /** * 添加图片水印平铺 * * @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 ; } |
目前有 0 条留言 其中:访客:0 条, 博主:0 条