如何将我的上传文件重命名为数据库中的url,然后在另一个页面中调整上传的大小


How to rename my upload file into url in my database, then resize the upload in another page

我在一个项目上工作与Laravel 4,我想重命名一个上传文件在我的数据库的url,我不知道如何这样做。我所有的验证代码工作得很好,我只需要找到如何重命名一个上传的文件在一个url名称,然后调整(200x200)上传的文件在另一个页面/布局/表单(blade.php)。。

下面是我的表单代码(createBand.blade.php):
{{Form::open(array('url'=>'createBand','files' => true))}}
{{-- *****files true allow users to upload files (image/logo) that we allow to upload for the layout creerGroupe***** --}}//this is how we comment with blade laravel template
<p>
    {{Form::label('name','Name of the Band (*): ')}}
    {{Form::text('name',Input::old('name'))}}
</p>
@if ($errors->has('name'))
<p class='error'> {{ $errors->first('name')}}</p>
@endif
<br>
<br>
<p>
    {{Form::label('url_Avatar','Band Avatar: ')}}
    {{Form::file('url_Avatar',Input::old('Url_Avatar'))}}
</p>
@if ($errors->has('url_Avatar'))
<p class='error'> {{ $errors->first('url_Avatar')}}</p>
@endif
<br>
<br>
<p>
    {{Form::submit('Create your Band')}}
</p>
{{Form::close()}}

我的文件上传得很好,但我需要将文件名重命名为url,以便将其保存在我的数据库中。然后,调整上传的图像到200x200格式在另一个页面/布局/形式(刀片模板),就像我们可以在Facebook的乐队页面上找到(如果你需要一个例子,我想建立在另一个页面/布局/形式,与调整图像)。

这是我的控制器代码:

    public function uploadfile() {
//*****VALIDATORS INPUTS and RULES*****
        $inputs = Input::all();
        $rules = array(
            'name' => 'required|between:1,64|unique:groupes,name',
            //"urlAvatar" is an url in database but an img on the server
            'url_Avatar' => 'min:1|image|unique:groupes,urlAvatar',
);
 //*****UPLOAD FILE (on server it's an image, on the DB it's an url*****
        $file = Input::file('url_Avatar');
        $destinationPath = 'upload/';
        $filename = str_random(32) . '.' . $file->getClientOriginalExtension();
        //This produces a random string of length 24 made up of alphanumeric characters [a-zA-z0-9]
        //$extension =$file->getClientOriginalExtension();
        $upload_success = Input::file('url_Avatar')->move($destinationPath, $filename);
        // we can do a redirect with some messages that file was uploaded,
        // but we need to pass all validators before reaching to the layout default
        if ($upload_success) {
            //save in the Band table database
            Band::create($data);
            return Redirect::to('/')
                            ->with('alert_success', 'your band have been created');
        } 
        else {
            return Redirect::to('createBand')
                     ->withInput()
                     ->withErrors($validation)
                     ->with('alert_error', 'respect format image or correct errors');
        }
    }

我的代码工作得很好,创建的乐队在我的数据库中注册了,但我想在我的数据库中注册上的url格式上的乐队头像。在我的数据库中为此创建了一个字段。

提前感谢。

我发现它是如何工作的:

其实我需要上传的是 package Intervention image你可以在这里找到他:https://github.com/Intervention/image。

之后,遵循这里的重定向:http://image.intervention.io/getting_started/laravel。请按照laravel的说明操作:

安装

: 安装干预映像的最好方法是使用Composer快速方便地安装。windows命令提示符中运行 Composer。

-在您的composer.json:中通过Composer要求软件包

"intervention/image": "2.*"

然后运行Composer来安装或更新新的需求。

$ php composer.phar install

$ php composer.phar update

现在我可以要求vendor/autolload .php文件为PSR-0自动加载库。

    // include composer autoload
    require 'vendor/autoload.php';
    // import the Intervention Image Manager Class
    use Intervention'Image'ImageManagerStatic as Image;
    // and you are ready to go ...
    $image = Image::make('public/foo.jpg')->resize(300, 200);

Image类也有可选的Laravel 4支持。

对于Laravel集成:安装完成后Intervention Image,打开Laravel配置文件config/app.php ,添加以下行:

$providers数组中添加服务提供商

'Intervention'Image'ImageServiceProvider'

将这个包的外观添加到$aliases数组

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

现在图像类将被Laravel自动加载。

配置:

默认情况下,干预图像使用PHP的GD库扩展来处理所有图像。如果我想切换到Imagick,我可以通过运行以下artisan命令将配置文件拉入我的应用程序。

$ php artisan config:publish intervention/image

该命令将配置文件复制到app/config/packages/intervention/image/config.php,在那里我可以在本地更改应用程序的驱动程序设置。

如果您需要更多关于PHP的GD库Imagick的信息,请点击laravel入门链接。

现在,这里是我的控制器代码,用于将我的上传文件保存到数据库中的URL,并将图像裁剪为200x200格式:

//*****UPLOAD FILE (on server it's an image, on the DB it's an url*****
        $file = Input::file('url_Avatar');
        $destinationPath = 'upload/';
        $filename = str_random(32) . '.' . $file->getClientOriginalExtension();
        //This produces a random string of length 24 made up of alphanumeric characters [a-zA-z0-9]
        //$extension =$file->getClientOriginalExtension();
        $upload_success = Input::file('url_Avatar')->move($destinationPath, $filename);
        var_dump(Image::make($image->getRealPath())->resize('200', '200')->save('upload/' . $filename));
        // we can do a redirect with some messages that file was uploaded,
        // but we need to pass all validators before reaching to the layout default
        if ($upload_success) {
        //save in the Band table database
        Band::create($data);
        return Redirect::to('/')
                        ->with('alert_success', 'your band have been created');
        } 
         else {
               return Redirect::to('createBand')
               ->withInput()
               ->withErrors($validation)
               ->with('alert_error', 'respect format image or correct errors');
        }
}
相关文章: