编程语言
首页 > 编程语言> > thinkphp5-图片处理

thinkphp5-图片处理

作者:互联网

安装图片处理类库

composer require topthink/think-image

打开图片

$image = \think\Image::open('./image.png');
$image = \think\Image::open(request()->file('image'));

获取图像信息

 $image = \think\Image::open(ROOT_PATH.'public/static/image/image.png');
// 返回图片的宽度
$width = $image->width();
// 返回图片的高度
$height = $image->height();
// 返回图片的类型
$type = $image->type();
// 返回图片的mime类型
$mime = $image->mime();
// 返回图片的尺寸数组 0 图片宽度 1 图片高度
$size = $image->size();

裁剪图片

$image = \think\Image::open(ROOT_PATH.'public/static/image/image.png');
$image->crop(300, 300)->save(ROOT_PATH.'public/static/image/crop.png');

缩略图

$image = \think\Image::open(ROOT_PATH.'public/static/image/image.png');
$image->thumb(150, 150)->save(ROOT_PATH.'public/static/image/thumb.png');

添加水印

$image = \think\Image::open(ROOT_PATH.'public/static/image/image.png');
$image->water(ROOT_PATH.'public/static/image/logo.png',\think\Image::WATER_SOUTHEAST)->save(ROOT_PATH.'public/static/image/water_image.png');

添加文本水印(字体文件ttf需要准备)

$image = \think\Image::open(ROOT_PATH.'public/static/image/image.png');
$image->text('This is text water',ROOT_PATH.'public/static/font/Apple Symbols.ttf',50,'#000000')->save(ROOT_PATH.'public/static/image/text_image.png');

标签:处理,image,thinkphp5,static,PATH,ROOT,public,png,图片
来源: https://www.cnblogs.com/hu308830232/p/15491846.html