首頁 > 後端開發 > php教程 > ThinkPHP打浮水印及設定浮水印位置的方法(實例分析)

ThinkPHP打浮水印及設定浮水印位置的方法(實例分析)

墨辰丷
發布: 2023-03-28 20:12:02
原創
1641 人瀏覽過

這篇文章主要介紹了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_h 的一部分拷貝到dst_im 影像中座標為dst_x 和dst_y 的位置上。兩個影像將根據 pct 來決定合併程度,其值範圍從 0 到 100。當 pct = 0 時,實際上什麼也沒做,當為 100 時對於調色板圖像本函數和 imagecopy() 完全一樣,它對真彩色圖像實現了 alpha 透明。

水印demo圖:

我需要把水印打到圖片的真中間,查看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[&#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中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
為什麼thinkphp比laravel效能好?
來自於 1970-01-01 08:00:00
0
0
0
php6支援的thinkphp版本
來自於 1970-01-01 08:00:00
0
0
0
mscms.qishouzhijia.com,自學thinkphp做的網站
來自於 1970-01-01 08:00:00
0
0
0
ThinkPHP什麼要用composer?
來自於 1970-01-01 08:00:00
0
0
0
thinkphp上傳文件
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板