Laravel 5干预图片上传多个尺寸 [英] Laravel 5 intervention image upload multiple size

查看:188
本文介绍了Laravel 5干预图片上传多个尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5和干预工具,并希望通过表单上传图像时存储多种尺寸的图像.谁能指导我

I am using laravel 5 and intervention, and would like to store multiple sizes of a image when it is uploaded via a form. Can anybody guide me

推荐答案

所以我不知道您已经做了什么.因此,让我们从头开始.

So i don't know what you already did. So let's start from the beginning.

首先,您需要干预库.因此,切换到主文件夹(包含composer.json文件) 然后输入

First of all you need the Intervention Library. So switch to your main Folder (containing your composer.json file) And type

composer.phar require intervention/image

或者只需在composer.json中的您的require数组中添加"intervention/image":〜2.1". (并在此之后进行作曲家更新)

Or just add "intervention/image": "~2.1" to your require array in composer.json. ( And do a composer update after that )

"require": {
    "laravel/framework": "5.0.*",
    "intervention/image": "~2.1"
},

现在您必须添加

'Intervention\Image\ImageServiceProvider',

到providers数组

to the providers array

'Image' => 'Intervention\Image\Facades\Image'

到您的别名数组.两者都在config/app.php

to your aliases Array. Both in config/app.php

现在您可以在类似的控制器中的某处创建上传功能"

Now you could create a "upload function" somewhere in a controller like

public function upload() {
    $image = \Image::make(\Input::file('image'));
    $path = storage_path('app')."/";

    // encode image to png
    $image->encode('png');
    // save original
    $image->save($path."original.png");
    //resize
    $image->resize(300,200);
    // save resized
    $image->save($path."resized.png");
}

这会将两个图像保存到storage/app文件夹.一种是原始尺寸,另一种尺寸则调整为300x200.

This would save two images to the storage/app folder. One in the original size and one resized to 300x200.

此代码仅是示例,它不包含对有效图像或类似内容的任何检查.它只需要一个文件(假设有一个图像)并保存两次. 当然,您也不需要编码为png ...

This code is only an example, it does not contain any checks, for valid images or stuff like that. It just takes a file (assuming an image) and saves it two times. And of course you also don't need to encode to png...

这篇关于Laravel 5干预图片上传多个尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆