laravel 使用 Intervention/image 进行图片处理

1.安装

使用Composer在命令行安装最新版本的Intervention Image:

composer require intervention/image

2.集成到Laravel

安装好Intervention Image后,打开config/app.php,注册如下服务提供者到$providers数组:

Intervention\Image\ImageServiceProvider::class

然后添加如下门面到$aliaes数组:

'Image' => Intervention\Image\Facades\Image::class

3.配置

默认情况下,Intervention Image使用PHP的GD库扩展处理所有图片,如果你想要切换到Imagick,你可以将配置文件拉到应用中:

php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"

这样对应的配置文件会被拷贝到config/image.php,你可以在该配置文件中修改图片处理驱动配置。

4.使用

public static function upload($request)
    {
        $img = $request->file('file');

        $images_path = 'upload/' . $request->path;
        $dir = base_path()."/public/upload/".$request->path;
        //检验目录是否存在
        if(!is_dir($dir)){
            @mkdir($dir, 0777, true);
        }
        //获取上传图片
        $score_file = $img;
        //str_random
        $ext = $score_file->getClientOriginalExtension();
        //src/Illuminate/Support/Str.php
        $upload_file_name = time() . Str::random(10) . '.' . $ext;
        //图片压缩上传 20220107
        $image = Image::make($img);
        // 尺寸等比压缩,最大宽度800
        if (($width = $image->getWidth()) > 800) {
            // 等比缩放,需要计算宽度缩放的比例,再计算出缩放后的图片高度
            $proportion = $width / 800;
            $height = ceil($image->getHeight() / $proportion);
            $image = $image->resize(800, $height);
        }
        // 文件绝对路径且用save保存
        // 保存图片,并设置质量压缩为60
        $image->save($images_path .'/'. $upload_file_name, 60);
        //最终的图片路径
        $images_path = "/" . $images_path .'/'. $upload_file_name;
        //返回图片路径
        return $images_path;
    }

最后调用静态方法返回图片路径就可以了。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要在 Laravel 项目中安装 intervention/image 库。可以通过 Composer 命令来安装: ``` composer require intervention/image ``` 安装完成后,你可以在控制器中编写以下代码来实现图片上传功能: ```php use Illuminate\Http\Request; use Intervention\Image\Facades\Image; public function upload(Request $request) { $image = $request->file('image'); $filename = time() . '.' . $image->getClientOriginalExtension(); $path = public_path('uploads/' . $filename); // 裁剪并保存图片 Image::make($image->getRealPath())->resize(200, 200)->save($path); return response()->json(['status' => 'success', 'url' => '/uploads/' . $filename]); } ``` 这段代码做了以下几件事: 1. 从 Request 中获取上传的图片文件; 2. 生成文件名,并保存到 Laravel 项目的 public/uploads 目录下; 3. 使用 intervention/image 库对图片进行裁剪,并保存到指定位置; 4. 返回上传成功的 JSON 响应,包括上传后的图片 URL。 最后,你需要在 Blade 模板中添加表单以及 JavaScript 代码来实现图片上传功能。可以参考以下代码: ```html <form id="upload-form" enctype="multipart/form-data"> <input type="file" name="image" id="image-input"> <button type="submit">上传图片</button> </form> <script> $(function() { $('#upload-form').submit(function(e) { e.preventDefault(); var formData = new FormData($(this)[0]); $.ajax({ url: '/upload', type: 'POST', data: formData, processData: false, contentType: false, success: function(data) { console.log(data); // 处理上传成功后的逻辑 }, error: function(xhr, status, error) { console.error(error); // 处理上传失败后的逻辑 } }); }); }); </script> ``` 这段代码做了以下几件事: 1. 显示一个包含文件上传表单的 HTML 表单; 2. 在表单提交时,使用 jQuery.ajax() 方法发送 POST 请求; 3. 将表单数据封装成 FormData 对象,并设置 processData 和 contentType 选项为 false; 4. 在上传成功时,处理服务器返回的 JSON 响应,例如显示上传的图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值