ThinkPHP打水印及设置水印位置的方法(实例分析)

墨辰丷
发布: 2023-03-28 20:12:02
原创
1470 人浏览过

这篇文章主要介绍了ThinkPHP打水印及设置水印位置的方法,结合实例形式分析了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代码。发现,作者居然是写死了,我只能做一个修改

/**
* 为图片添加水印
* @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[&#39;height&#39;] < $wInfo[&#39;height&#39;]) return false; //建立图像 $sCreateFun = "imagecreatefrom" . $sInfo[&#39;type&#39;]; $sImage = $sCreateFun($source); $wCreateFun = "imagecreatefrom" . $wInfo[&#39;type&#39;]; $wImage = $wCreateFun($water); //设定图像的混色模式 imagealphablending($wImage, true); //图像位置,默认为右下角右对齐 $posArr = $this->WaterPostion($postion,$sInfo,$wInfo); //新增
  //生成混合图像
  imagecopymerge($sImage, $wImage, $posArr[0], $posArr[1], 0, 0, $wInfo[&#39;width&#39;], $wInfo[&#39;height&#39;], $alpha);
  //输出图像
  $ImageFun = &#39;Image&#39; . $sInfo[&#39;type&#39;];
 //如果没有给出保存文件名,默认为原图像名
 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;
  }
}
登录后复制

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP实现魔术方法以及关于独立实例与相连实例

PHP给前端返回一个JSON对象

php实现文件上传、下载和删除的方法

以上是ThinkPHP打水印及设置水印位置的方法(实例分析)的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!