安装扩展
composer require intervention/image
发布配置
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
基本使用
1.可以修改config/image选择驱动,默认是GD库,可以修改为Imagick
2.实例化一张图片
$img = Image::make('/path/to/image')
3.获取图片的信息
//图片高度
$height = Image::make('public/foo.jpg')->height();
//图片宽度
$width = Image::make('public/foo.jpg')->width();
4.修改指定图片的大小
$img = $img->resize(200, 200);
5.图片加水印
$img->insert('/path/to/watermark', 'bottom-right', 15, 10);
6.将处理后的图片保存
$img->save('/save/to/path');
7.以上各种操作可以连贯操作
$img = Image::make('/path/to/image')->resize(200, 200)->insert('/path/to/watermark', $position, 15, 10);->save('save/to/path')
/*
参数说明
$position:top-left (default), top, top-right, left, center, right, bottom-left, bottom, bottom-right
*/
8.裁剪图片
$img->crop(100, 100, 25, 25);
9.垂直翻转图片
$img->flip('v');
10.改变图片的编码,例如:png->jpg
$jpg = (string) Image::make('/path/to/png')->encode('jpg', 75);
11.旋转图片
//顺时针旋转45度
$img->rotate(-45);
12.指定位置添加文字
//简单写文字
$img->text('text', 120, 100);
// 自定义字体,字体颜色,字体大小,字体位置
$img->text('foo', 0, 0, function($font) {
$font->file('foo/bar.ttf');
$font->size(24);
$font->color('#fdf6e3');
$font->align('center');
$font->valign('top');
$font->angle(45);
});
// 有透明度的文字
$img->text('foo', 0, 0, function($font) {
$font->color(array(255, 255, 255, 0.5));
});
更多用法请参考官方网站提供的接口文档:http://image.intervention.io