//图片水印 function img_water_mark() { $dst_path = '011.jpg'; $src_path = 'dianchao.png'; $new_img_name = '012.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); $src = imagecreatefromstring(file_get_contents($src_path)); //获取水印图片的宽高 list($src_w, $src_h) = getimagesize($src_path); //获取原图的宽高 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果 $pos=2; /* * 10种水印效果位置 * 0:默认是随机位置 * 1:顶端居左 * 2:顶端居中 * 3:顶端居右 * 4:中部居左 * 5:中部居中 * 6:中部居右 * 7:底端居左 * 8:底端居中 * 9:底端居右 */ switch ($pos) { case 0: $pos_x = rand(0, ($dst_w - $src_w)); $pos_y = rand(0, ($dst_h - $src_h)); break; case 1: $pos_x = 0; $pos_y = 0; break; case 2: $pos_x = ($dst_w - $src_w) / 2; $pos_y = 0; break; case 3: $pos_x = $dst_w - $src_w; $pos_y = 0; break; case 4: $pos_x = 0; $pos_y = ($dst_h - $src_h) / 2; break; case 5: $pos_x = ($dst_w - $src_w) / 2; $pos_y = ($dst_h - $src_h) / 2; break; case 6: $pos_x = $dst_w - $src_w; $pos_y = ($dst_h - $src_h) / 2; break; case 7: $pos_x = 0; $pos_y = $dst_h - $src_h; break; case 8: $pos_x = ($dst_w - $src_w) / 2; $pos_y = $dst_h - $src_h; break; case 9: $pos_x = $dst_w - $src_w; $pos_y = $dst_h - $src_h; break; default: $pos_x = rand(0, ($dst_w - $src_w)); $pos_y = rand(0, ($dst_h - $src_h)); break; } imagecopymerge($dst, $src, $pos_x, $pos_y, 0, 0, $src_w, $src_h, 50); //如果水印图片本身带透明色,则使用imagecopy方法 // imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h); //输出图片 switch ($dst_type) { case 1://GIF //header('Content-Type: image/gif'); imagegif($dst,$new_img_name); //第二个参数为可选,$new_img_name为生成水印后的图片名,省略则原始图像流将被直接输出 //header使 PHP 脚本直接输出图像 break; case 2://JPG //header('Content-Type: image/jpeg'); imagejpeg($dst,$new_img_name); break; case 3://PNG header('Content-Type: image/png'); imagepng($dst,$new_img_name); break; default: break; } imagedestroy($dst); imagedestroy($src); } //文字水印 function word__water_mark() { $dst_path = 'dst.jpg'; //创建图片的实例 $dst = imagecreatefromstring(file_get_contents($dst_path)); //打上文字 $font = './simsun.ttc';//字体路径 $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色 imagefttext($dst, 13, 0, 20, 20, $black, $font, '快乐编程'); //输出图片 list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path); switch ($dst_type) { case 1://GIF header('Content-Type: image/gif'); imagegif($dst); break; case 2://JPG header('Content-Type: image/jpeg'); imagejpeg($dst); break; case 3://PNG header('Content-Type: image/png'); imagepng($dst); break; default: break; } imagedestroy($dst); }
php 生成水印图片
最新推荐文章于 2024-10-01 16:09:28 发布