服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - ThinkPHP打水印及设置水印位置的方法

ThinkPHP打水印及设置水印位置的方法

2021-03-11 16:24dengwz7788 PHP教程

这篇文章主要介绍了ThinkPHP打水印及设置水印位置的方法,结合实例形式分析了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打水印及设置水印位置的方法

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* 为图片添加水印
* @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框架的php程序设计有所帮助。

延伸 · 阅读

精彩推荐