Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

丰富水印及文字功能 #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 51 additions & 16 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ public function save($pathname, $type = null, $quality = 80, $interlace = true)
return $this;
}

/**
* 获取到image资源
* @return resource
*/
public function getIm()
{
return $this->im;
}

/**
* 返回图像宽度
* @return int 图像宽度
Expand Down Expand Up @@ -377,24 +386,33 @@ public function thumb($width, $height, $type = self::THUMB_SCALING)
/**
* 添加水印
*
* @param string $source 水印图片路径
* @param string $source 水印图片路径,或者string格式的图片字符串
* @param int|array $locate 水印位置
* @param int $alpha 透明度
* @param int|array $offset 偏移量
* @return $this
*/
public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100)
public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100, $offset = 0)
{
if (!is_file($source)) {
throw new ImageException('水印图像不存在');
}
//获取水印图像信息
$info = getimagesize($source);
if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
throw new ImageException('非法水印文件');
if(strlen($source) < 200){
if (!is_file($source)) {
throw new ImageException('水印图像不存在');
}
//获取水印图像信息
$info = getimagesize($source);
if (false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))) {
throw new ImageException('非法水印文件');
}
//创建水印图像资源
$fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
$water = $fun($source);
}else{
$water = imagecreatefromstring($source);
$info = [
imagesx($water),
imagesy($water),
];
}
//创建水印图像资源
$fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
$water = $fun($source);
//设定水印图像的混色模式
imagealphablending($water, true);
/* 设定水印位置 */
Expand Down Expand Up @@ -451,15 +469,24 @@ public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100)
throw new ImageException('不支持的水印位置类型');
}
}

/* 设置偏移量 */
if (is_array($offset)) {
$offset = array_map('intval', $offset);
list($ox, $oy) = $offset;
} else {
$offset = intval($offset);
$ox = $oy = $offset;
}
do {
//添加水印
$src = imagecreatetruecolor($info[0], $info[1]);
// 调整默认颜色
$color = imagecolorallocate($src, 255, 255, 255);
imagefill($src, 0, 0, $color);
imagecopy($src, $this->im, 0, 0, $x, $y, $info[0], $info[1]);
imagecopy($src, $this->im, 0, 0, $x + $ox, $y + $oy, $info[0], $info[1]);
imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha);
imagecopymerge($this->im, $src, $x + $ox, $y + $oy, 0, 0, $info[0], $info[1], $alpha);
//销毁临时图片资源
imagedestroy($src);
} while (!empty($this->gif) && $this->gifNext());
Expand Down Expand Up @@ -545,8 +572,16 @@ public function text($text, $font, $size, $color = '#00000000',
/* 自定义文字坐标 */
if (is_array($locate)) {
list($posx, $posy) = $locate;
$x += $posx;
$y += $posy;
if('middle' == $posx){
$x += ($this->info['width'] - $w) / 2;
}else{
$x += $posx;
}
if('middle' == $posy){
$y += ($this->info['height'] - $h) / 2;
}else{
$y += $posy;
}
} else {
throw new ImageException('不支持的文字位置类型');
}
Expand Down