Skip to content

Commit 9ac548b

Browse files
committed
生成水印时,支持从string字符串的图片中获取水印资源
1 parent 87c2946 commit 9ac548b

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/Image.php

+19-11
Original file line numberDiff line numberDiff line change
@@ -377,24 +377,32 @@ public function thumb($width, $height, $type = self::THUMB_SCALING)
377377
/**
378378
* 添加水印
379379
*
380-
* @param string $source 水印图片路径
380+
* @param string $source 水印图片路径,或者string格式的图片字符串
381381
* @param int|array $locate 水印位置
382382
* @param int $alpha 透明度
383383
* @return $this
384384
*/
385385
public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100)
386386
{
387-
if (!is_file($source)) {
388-
throw new ImageException('水印图像不存在');
389-
}
390-
//获取水印图像信息
391-
$info = getimagesize($source);
392-
if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
393-
throw new ImageException('非法水印文件');
387+
if(strlen($source) < 200){
388+
if (!is_file($source)) {
389+
throw new ImageException('水印图像不存在');
390+
}
391+
//获取水印图像信息
392+
$info = getimagesize($source);
393+
if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
394+
throw new ImageException('非法水印文件');
395+
}
396+
//创建水印图像资源
397+
$fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
398+
$water = $fun($source);
399+
}else{
400+
$water = imagecreatefromstring($source);
401+
$info = [
402+
imagesx($water),
403+
imagesy($water),
404+
];
394405
}
395-
//创建水印图像资源
396-
$fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
397-
$water = $fun($source);
398406
//设定水印图像的混色模式
399407
imagealphablending($water, true);
400408
/* 设定水印位置 */

0 commit comments

Comments
 (0)