php 生成多个水印,PHP 生成水印圖片

{$data_set = array("text" => "這是測試文字");//項目根目錄

$path = __DIR__."/路徑/";//背景圖

$tem_bg = __DIR__."/項目存放背景圖得路徑/背景圖.jpg";//最終合成圖片得路徑和名字

$final_path = "/img/.../...之類得路徑/用戶ID-bg.jpg";$bg = $path.$final_path;copy($tem_bg, $bg); //復制圖片

list($bgWidth, $bgHeight) = getimagesize($bg); //獲取圖片寬高

// 字體文件路徑

$path_src = __DIR__."/字體文件路徑";//如果是需要生成文字圖片,則調用下面方法,生成文字圖片,再合成

$text_image = $this->text_image_png($path, $path_src, $data_set);//獲取文字圖片得寬高

list($textWidth, $textHeight) = getimagesize($text_image);//將背景圖和文字圖合成

$this->synthetic($bg, 'jpg', $bgWidth, $bgHeight, $text_image, $textHeight, $textWidth, "bottom-center", 0, 210);unlink($text_image); //刪除臨時合成得文字圖片

// 如果不需要生成文字圖片,直接使用現有的圖片,則執行下面的,這2種方式可共存

$image = __DIR__."/圖片路徑/xxx.png";list($logoWidth, $logoHeight) = getimagesize($image);//將背景圖和文字圖合成

$this->synthetic($bg, 'jpg', $bgWidth, $bgHeight, $image, $logoHeight, $logoWidth, "bottom-center", 0, 210, 0, 10);//如果需要合成多個圖片,那就執行多次$this->synthetic()方法

return array("width" => $bgWidth, "height" => $bgHeight, "path" => $bg_name); //path就是合成后圖片的路徑

}/**

* 生成文字圖片

* @param $path 項目根目錄

* @param $font_path 字體文件路徑

* @param $data_set 需要生成文字圖片的文字

* @return string*/

public function text_image_png($path, $font_path, $data_set)

{$bgwidth = 640; //文字圖片的寬

$bgheight = 500; //文字圖片的高

$block = imagecreatetruecolor($bgwidth, $bgheight);//建立一個畫板

$bg = imagecolorallocatealpha($block , 255 , 255 , 255 , 127);//拾取一個完全透明的顏色,不要用imagecolorallocate拾色

$color = imagecolorallocate($block,137,54,20); //字體拾色

imagealphablending($block , false);//關閉混合模式,以便透明顏色能覆蓋原畫板

imagefill($block , 0 , 0 , $bg);//填充

$font_file = $font_path.'/fonts/simhei.ttf'; //字體文件

$color = imagecolorallocate($block,255,228,116); //字體拾色

// 文字

$text = $data_set["text"];

imagettftext($block,50,0,260,150,$color,$font_file,$text); //向圖像寫入文本

imagesavealpha($block , true);//設置保存png時保留透明通道信息

$image_path = $path."/臨時文件路徑/text.png";

imagepng($block, $image_path);//生成圖片

imagedestroy($block); //銷毀畫板

return $image_path;

}/**

* 合並圖片

*

* @param $image // 背景圖

* @param $mimeType // 合成后的圖片類型

* @param $imgWidth // 背景的寬

* @param $imgHeight // 背景的高

* @param $watermark // 需要合成的圖

* @param $watermarkHeight // 需要合成的高

* @param $watermarkWidth // 需要合成的寬

* @param string $position // 居中類型

* @param int $mt // 設置margin-top值

* @param int $mb // 設置margin-bottom值

* @param int $ml // 設置margin-left值

* @param int $mr // 設置margin-right值

* @param string $watermakMimeType // 合成后圖片的類型

* @throws \Exception*/

public function synthetic($image, $mimeType, $imgWidth, $imgHeight, $watermark, $watermarkHeight, $watermarkWidth, $position = "center", $mt = 0, $mb = 0, $ml = 0, $mr = 0, $watermakMimeType = "png")

{//Calculate the watermark position

switch ($position) {case "center":

$marginBottom = round($imgHeight / 2);$marginRight = round($imgWidth / 2) - round($watermarkWidth / 2);break;case "top-left":

$marginBottom = round($imgHeight - $watermarkHeight);$marginRight = round($imgWidth - $watermarkWidth);break;case "bottom-left":

$marginBottom = 5;$marginRight = round($imgWidth - $watermarkWidth);break;case "top-right":

$marginBottom = round($imgHeight - $watermarkHeight);$marginRight = 5;break;case "bottom-center":

$marginBottom = $mb;$marginRight = round($imgWidth / 2) - round($watermarkWidth / 2) + $mr;break;default:

$marginBottom = 2;$marginRight = 2;break;

}if($watermakMimeType == "png") {$watermark = imagecreatefrompng($watermark); //生成png圖片

} else{$watermark = imagecreatefromjpeg($watermark); //生成jpg圖片

}switch ($mimeType) {case "jpeg":

case "jpg":

$createImage = imagecreatefromjpeg($image);break;case "png":

$createImage = imagecreatefrompng($image);break;case "gif":

$createImage = imagecreatefromgif($image);break;default:

$createImage = imagecreatefromjpeg($image);break;

}$sx = imagesx($watermark);$sy = imagesy($watermark);

imagecopy($createImage,

$watermark,imagesx($createImage) - $sx - $marginRight,imagesy($createImage) - $sy - $marginBottom,

0,

0,imagesx($watermark),imagesy($watermark)

);//拷貝圖像的一部分

switch ($mimeType) {case "jpeg":

case "jpg":imagejpeg($createImage, $image); //以 jpeg 格式將圖像輸出到瀏覽器或文件

break;case "png":imagepng($createImage, $image); //以 PNG 格式將圖像輸出到瀏覽器或文件

break;case "gif":imagegif($createImage, $image); //以 gif 格式將圖像輸出到瀏覽器或文件

break;default:

throw new \Exception("A watermark can only be applied to: jpeg, jpg, gif, png images ");break;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值