ThinkPHP打水印及设置水印位置的示例分析

发布时间:2021-08-30 15:27:53 作者:小新
来源:亿速云 阅读:110

这篇文章给大家分享的是有关ThinkPHP打水印及设置水印位置的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体如下:

最近在用Thinkphp的打水印的功能,发现只能打在左下角。 PHP打水印功还是很容易的,最要是用到

bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )

将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。

水印demo图:

ThinkPHP打水印及设置水印位置的示例分析

我需要把水印打到图片的真中间,查看Thinkphp代码。发现,作者居然是写死了,我只能做一个修改

/**
* 为图片添加水印
* @static public
* @param string $source 原文件名
* @param string $water 水印图片
* @param string $$savename 添加水印后的图片名
* @param string $postion 水印的具体位置 leftbottom rightbottom lefttop righttop center <新增>
* @param string $alpha 水印的透明度
* @return void
*/
static public function water($source, $water, $savename=null,$postion="center", $alpha=80) {
//检查文件是否存在
if (!file_exists($source) || !file_exists($water))
return false;
//图片信息
$sInfo = self::getImageInfo($source);
$wInfo = self::getImageInfo($water);
//如果图片小于水印图片,不生成图片
if ($sInfo["width"] < $wInfo["width"] || $sInfo['height'] < $wInfo['height']) return false; //建立图像 $sCreateFun = "imagecreatefrom" . $sInfo['type']; $sImage = $sCreateFun($source); $wCreateFun = "imagecreatefrom" . $wInfo['type']; $wImage = $wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage, true); //图像位置,默认为右下角右对齐 $posArr = $this->WaterPostion($postion,$sInfo,$wInfo); //新增
  //生成混合图像
  imagecopymerge($sImage, $wImage, $posArr[0], $posArr[1], 0, 0, $wInfo['width'], $wInfo['height'], $alpha);
  //输出图像
  $ImageFun = 'Image' . $sInfo['type'];
 //如果没有给出保存文件名,默认为原图像名
 if (!$savename) {
   $savename = $source;
   @unlink($source);
  }
 //保存图像
  $ImageFun($sImage, $savename);
   imagedestroy($sImage);
 }
 private function WaterPostion($postion,$sInfo,$wInfo)
 {
   $posY = $sInfo["height"] - $wInfo["height"];
   $posX = $sInfo["width"] - $wInfo["width"];
  switch($postion)
 {
   case "rightbottom":
    return array($posX,$posY);
   break;
   case "leftbottom":
    return array($wInfo["width"],$posY);
   break;
   case "lefttop":
    return array($wInfo["width"],$wInfo["height"]);
   break;
   case "righttop":
    return array($posX,$wInfo["height"]);
   break;
   case "center":
    return array($posX/2,$posY/2);
  break;
  }
}

感谢各位的阅读!关于“ThinkPHP打水印及设置水印位置的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. C# 添加Word水印(文本水印、图片水印)_基于Spire.Cloud.Word
  2. wuzhicms水印的设置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

thinkphp

上一篇:ppt中图片空白无法显示

下一篇:OSWatcher的安装、配置以及使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》