laravel 基于intervention/image拓展实现图片上传和图像处理
发布时间:2018-12-14 16:16:25作者:wangjian浏览量:828点赞量:0
一:安装intervention/image拓展
composer require intervention/image
在安装intervention/image时可能会报
Using version ^2.4 for intervention/image
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- intervention/image 2.4.2 requires ext-fileinfo * -> the requested PHP exte
nsion fileinfo is missing from your system.
- intervention/image 2.4.1 requires ext-fileinfo * -> the requested PHP exte
nsion fileinfo is missing from your system.
- intervention/image 2.4.0 requires ext-fileinfo * -> the requested PHP exte
nsion fileinfo is missing from your system.
- Installation request for intervention/image ^2.4 -> satisfiable by interve
ntion/image[2.4.0, 2.4.1, 2.4.2].
To enable extensions, verify that they are enabled in your .ini files:
- E:\phpStudy\PHPTutorial\php\php-7.0.12-nts\php.ini
You can also run `php --ini` inside terminal to see which files are used by PH
P in CLI mode.
Installation failed, reverting ./composer.json to its original content.
这是由于安装intervention/image时php环境必须开启php_fileinfo.dll,修改php.ini文件
extension=php_fileinfo.dll
二:laravel配置
修改config/app.php文件
在providers数组中添加
// 将下面代码添加到 providers 数组中
'providers' => [
// ...
Intervention\Image\ImageServiceProvider::class,
// ...
],
在aliases数组中添加
// 将下面代码添加到 aliases 数组中
'aliases' => [
// ...
'Image' => Intervention\Image\Facades\Image::class,
// ...
],
使用下面命令生成intervention/image配置文件
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
执行上面命令后会在config目录下生成一个image.php文件
<?php
return [
/*
|--------------------------------------------------------------------------
| Image Driver
|--------------------------------------------------------------------------
|
| Intervention Image supports "GD Library" and "Imagick" to process images
| internally. You may choose one of them according to your PHP
| configuration. By default PHP's "GD Library" implementation is used.
|
| Supported: "gd", "imagick"
|
*/
'driver' => 'gd'
];
driver表示使用什么库来进行图像出库,intervention/image一般有两种gd和imagick库来进行图像处理,如果使用imagick库来进行图像处理的话需要安装 PHP的Imagick扩展
三:使用intervention/image拓展
1:实现上传
Image::make(Input::file('image'))->save('upload.jpg');//image为上传表单的name名
Image::make(Input::file('image'))->resize(300, 200)->save('upload.jpg');//image为上传表单的name名,并将上传的图片压缩成300,200
同时实现单图上传和多图上传
if (Input::file('image')){
if(is_array(Input::file('image'))){
$image = Input::file('image');
foreach ($image as $k=>$item){
Image::make($item)->save('upload'.$k.'.jpg');
}
}else{
Image::make(Input::file('image'))->save('upload.jpg');
}
}
2:图像处理
(1)生成缩略图
Image::make('test.jpg')->resize(300,200)->save('resize.jpg');//将test.jpg图片生成300*200的图片并保存为resize.jpg
(2)添加水印
//在test.jpg插入water.jpg水印, 水印位置在原图片的右下角, 距离下边距 10 像素, 距离右边距 15 像素,并保存为savewater.jpg'
Image::make('test.jpg')->insert('water.jpg', 'bottom-right', 15, 10)->save('savewater.jpg');
(3)翻转图片
//将test.jpg水平翻转并保存为flip.jpg
Image::make('test.jpg')->flip('v')->save('flip.jpg');
(4)旋转图片
//将test.jpg逆时针旋转45度并保存为rotate.jpg,rotate一共有两个参数第一个为旋转角度,第二个为旋转后未覆盖区域的背景颜色,默认值:#ffffff
Image::make('test.jpg')->rotate(-45)->save('rotate.jpg');
上面的方法可以使用链式连接执行多个方法
其他图像处理方法可以参考 拓展包的官方网站
上一篇:PHP的Imagick拓展安装
下一篇:laravel 队列系统