四时宝库

程序员的知识宝库

thinkphp5图片水印的自适应方法(php 图片加水印)

本人后端程序员小学毕业, 水平有限,贻笑大方。

修改

vendor/topthink/think-image/src/Image.php 378行

/**

* 添加水印

*

* @param string $source 水印图片路径

* @param int $locate 水印位置

* @param int $alpha 透明度

* @return $this

*/

public function water($source, $locate = self::WATER_SOUTHEAST, $alpha = 100)

{

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);

$b = $info[0]/$this->info['width'];

if ($b<1) {

$b = 1;

}

$croped=imagecreatetruecolor($this->info['width'], $this->info['height']); //新建一个真彩色图像

$color = imagecolorallocatealpha($croped, 0, 0, 0,127);

imagefill($croped, 0, 0, $color);

imagecopyresized($croped, $water, 0, ($this->info['height']-$info[1]/$b)/2, 0, 0,$info[0]/$b, $info[1]/$b,$info[0], $info[1]); //拷贝图像的一部分

imagesavealpha($croped, true);

$water = $croped;

$info[0]= $this->info['width'];

$info[1]= $this->info['height'];

// header("Content-Type:image/jpeg");

// imagejpeg($croped);

// die;

//设定水印图像的混色模式

imagealphablending($water, true);

/* 设定水印位置 */

switch ($locate) {

/* 右下角水印 */

case self::WATER_SOUTHEAST:

$x = $this->info['width'] - $info[0];

$y = $this->info['height'] - $info[1];

break;

/* 左下角水印 */

case self::WATER_SOUTHWEST:

$x = 0;

$y = $this->info['height'] - $info[1];

break;

/* 左上角水印 */

case self::WATER_NORTHWEST:

$x = $y = 0;

break;

/* 右上角水印 */

case self::WATER_NORTHEAST:

$x = $this->info['width'] - $info[0];

$y = 0;

break;

/* 居中水印 */

case self::WATER_CENTER:

$x = ($this->info['width'] - $info[0]) / 2;

$y = ($this->info['height'] - $info[1]) / 2;

break;

/* 下居中水印 */

case self::WATER_SOUTH:

$x = ($this->info['width'] - $info[0]) / 2;

$y = $this->info['height'] - $info[1];

break;

/* 右居中水印 */

case self::WATER_EAST:

$x = $this->info['width'] - $info[0];

$y = ($this->info['height'] - $info[1]) / 2;

break;

/* 上居中水印 */

case self::WATER_NORTH:

$x = ($this->info['width'] - $info[0]) / 2;

$y = 0;

break;

/* 左居中水印 */

case self::WATER_WEST:

$x = 0;

$y = ($this->info['height'] - $info[1]) / 2;

break;

default:

/* 自定义水印坐标 */

if (is_array($locate)) {

list($x, $y) = $locate;

} else {

throw new ImageException('不支持的水印位置类型');

}

}

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, $water, 0, 0, 0, 0, $info[0], $info[1]);

imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], $alpha);

// header("Content-Type:image/jpeg");

// imagejpeg($this->im);

// die;

//销毁零时图片资源

imagedestroy($src);


} while (!empty($this->gif) && $this->gifNext());

//销毁水印资源

imagedestroy($water);

return $this;

}

发布日期:2020/07/24 分类:技术分享 关键字: thinkphp5 图片水印 自适应

只会写代码,不会聊天的直男。

zmq2088 小程序 公众号 企业官网 商城 CRM 等多种系统开发。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接