class Image { /* * 文字水印 * $img 图片路径 * $typeFace 字体路径 * */ public function textWatermark($img,$typeFace ) { $dst_path = $img; // 获取图片 $dst = imagecreatefromstring(file_get_contents($dst_path));//创建图片的实例 $font = $typeFace;//字体路径 $black = imagecolorallocatealpha($dst, 0, 0, 255, 0); imagefttext($dst, 20, 0, 500, 800, $black, $font, '这是文字水印ces'); 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); } /* * 图片水印 * $img 图片路径 * $watermarkPicture 水印图片 * **/ public function pictureWatermark ($img, $watermarkPicture) { $dst = imagecreatefromstring(file_get_contents($img)); $src = imagecreatefromstring(file_get_contents($watermarkPicture)); //获取水印图片的宽高 list($src_w, $src_h) = getimagesize($watermarkPicture); //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果 imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 10); //如果水印图片本身带透明色,则使用imagecopy方法 // imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h); //输出图片 list($dst_w, $dst_h, $dst_type) = getimagesize($img); 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); imagedestroy($src); } }
php 图片添加水印
最新推荐文章于 2022-05-17 13:45:38 发布