PHP图片水印_PHP平铺水印_PHP图片多水印_PHP图片倾斜水印
下面介绍的是php实现图片多个水印,平铺水印,倾斜水印
1 //$img => 图片,$source => 水印 2 $img = 'test.jpg'; 3 $source = 'source.png'; 4 5 //参数设置,值越大水印越稀(水印平铺的越少),相反... 6 $ww = 0; //每个水印的左右间距 7 $hh = 0; //每个水印的上下间距 8 9 //水印图片旋转角度 10 $angle = 30; 11 12 //水印透明度 13 $opacity = 20; 14 15 //获取图片和水印的信息 16 $imgInfo = getimagesize($img); 17 $sourceInfo = getimagesize($source); 18 19 //创建水印图像资源 20 $fun = 'imagecreatefrom' . image_type_to_extension($sourceInfo[2], false); 21 $water = $fun($source); 22 //水印图片旋转 23 $water = imagerotate($water,$angle, imageColorAllocateAlpha($water, 0, 0, 0, 127)); 24 //获取水印图片旋转后的宽度和高度 25 $sourceInfo[0] = imagesx($water); 26 $sourceInfo[1] = imagesy($water); 27 28 //设定水印图像的混色模式 29 imagealphablending($water, true); 30 //添加水印 31 $src = imagecreatetruecolor($sourceInfo[0], $sourceInfo[1]); 32 // 调整默认颜色 33 $color = imagecolorallocate($src, 255, 255, 255); 34 imagefill($src, 0, 0, $color); 35 36 //创建图片图像资源 37 $fun = 'imagecreatefrom' . image_type_to_extension($imgInfo[2], false); 38 $thumb = $fun($img); 39 40 //定义平铺数据 41 $x_length = $imgInfo[0] - 10; //x轴总长度 42 $y_length = $imgInfo[1] - 10; //y轴总长度 43 //循环平铺水印 44 for ($x = 0; $x < $x_length; $x) { 45 for ($y = 0; $y < $y_length; $y) { 46 imagecopy($src, $thumb, 0, 0, $x, $y, $sourceInfo[0], $sourceInfo[1]); 47 imagecopy($src, $water, 0, 0, 0, 0, $sourceInfo[0], $sourceInfo[1]); 48 imagecopymerge($thumb, $src, $x, $y, 0, 0, $sourceInfo[0], $sourceInfo[1], $opacity); 49 $y += $sourceInfo[1] + $hh; 50 } 51 $x += $sourceInfo[0] + $ww; 52 } 53 54 header("Content-type:image/jpeg"); 55 imagejpeg($thumb); 56 57 //销毁零时图片资源 58 imagedestroy($src); 59 //销毁水印资源 60 imagedestroy($water);
效果展示