• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP创建水印

php 搞代码 3年前 (2022-01-22) 10次浏览 已收录 0个评论

这篇文章主要介绍了PHP创建水印,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

1.文字水印添加 使用imagefttext 函数

<?php/** * 为图片添加文字水印 * @param  string $dst_path    原图路径 * @param  string $font_path   字体存放路径 * @param  string $string_font 欲添加的文字 */function textwatermark($dst_path,$font_path,$string_font){    //创建图片的实例    $dst = imagecreatefromstring(file_get_contents($dst_path));    //添加文字    $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);    imagefilledrectangle($dst, 0, 0, 79, 49, 0x0000FF);    imagefilledrectangle($dst, 9, 9, 70, 40, 0xFFFFFF);    imagefttext($dst, 13, 0, 20, 20, $black, $font_path, $string_font);    //输出图片    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);    switch ($dst_type) {        case 1://IMAGETYPE_GIF            header('Content-Type: image/gif');            imagegif($dst);            break;        case 2://IMAGETYPE_JPEG            header('Content-Type: image/jpeg');            imagejpeg($dst);            break;        case 3://IMAGETYPE_PNG            header('Content-Type: image/png');            imagepng($dst);            break;        default:            break;    }    imagedestroy($dst);}header('charset=utf-8');$dst_path = './uploads/1.jpg';//选择的字体需支持中文 arial.ttf不支持中文$font_path = 'C:/Windows/Fonts/simhei.ttf';        //当文件编码为utf-8时 不需转换 $string_font = '剑liang';       textwatermark($dst_path,$font_path,$string_font);?>

2.图片水印 使用imagecopymerge 函数

<?php/** * 添加图片水印功能 * @param resource $dst_path 原图路径 * @param resource $src_path 水印图片路径 * @param int $pact 水印合并效果,默认为50  * @param int $postion 添加水印位置,默认为右下角 */function watermark($dst_path,$src_path, $pct = 50,$postion = 5){    //创建图片的实例    $dst = imagecreatefromstring(file_get_contents($dst_path));    $src = imagecreatefromstring(file_get_contents($src_path));    // 从数组中获取原图和水印图片的宽和高    list($dst_w, $dst_h) = getimagesize($dst_path);    list($src_w, $src_h) = getimagesize($src_path);    switch ($postion) {       case 1: // 左上            $src_x = $src_y = 0;                            break;        case 2: // 右上            $src_x = $dst_w - $src_w;            $src_y = 0;                                    break;       case 3: // 中间           $src_x = ($dst_w - $src_w) / 2;           $src_y = ($dst_h - $src_h) / 2;               break;       case 4: // 左下            $src_x = 0;            $src_y = $dst_h - $src_h;                   break;       case 5: // 右下            $src_x = $dst_w - $src_w;            $src_y = $dst_h - $src_h;                    break;       default:            break;    }    //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果    imagecopymerge($dst, $src, $src_x, $src_y, 0, 0, $src_w, $src_h, $pct);    //如果水印图片本身带透明色,则使用imagecopy方法    // imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);    //输出图片    list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);    switch ($dst_type) {        case 1://IMAGETYPE_GIF            header('Content-Type: image/gif');            imagegif($dst);            break;        case 2://IMAGETYPE_JPEG            header('Content-T<strong style="color:transparent">本文来源gaodai#ma#com搞@@代~&码*网/</strong><strong>搞gaodaima代码</strong>ype: image/jpeg');            imagejpeg($dst);            break;        case 3://IMAGETYPE_PNG            header('Content-Type: image/png');            imagepng($dst);            break;        default:            break;    }    imagedestroy($dst);    imagedestroy($src);}$source = './uploads/1.jpg';$water = './uploads/6.jpg';watermark($source, $water, 50, 5);?>

相关推荐:

php创建session方法步奏详解

PHP创建或导出Excel数据表格的方法

PHP创建透明PNG图的方法

以上就是PHP创建水印的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP创建水印
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址