PHPackages                             kak/storage - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. kak/storage

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

kak/storage
===========

file upload/resize/crop file move storage

0.1.4(6y ago)51.5k2[1 issues](https://github.com/sanchezzzhak/yii2storage/issues)BSD-3-ClausePHPPHP &gt;=5.4.0

Since May 6Pushed 6y ago4 watchersCompare

[ Source](https://github.com/sanchezzzhak/yii2storage)[ Packagist](https://packagist.org/packages/kak/storage)[ Docs](https://github.com/sanchezzzhak/yii2storage)[ RSS](/packages/kak-storage/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (6)Versions (6)Used By (0)

Storage and Upload file for Yii2
================================

[](#storage-and-upload-file-for-yii2)

file upload/resize

Any contributions are welcome Preview
-------------------------------------

[](#any-contributions-are-welcomepreview)

[![](https://camo.githubusercontent.com/01aa5a193c6915a4d53e7a9f00238334bcae562af144d6d4e4dbc7d60ba867f2/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d2d73446d683343613855412f5662587351665f55786f492f414141414141414141446f2f53545233447254726444552f733437372d496334322f5072657669657755706c6f61642e706e67)](https://camo.githubusercontent.com/01aa5a193c6915a4d53e7a9f00238334bcae562af144d6d4e4dbc7d60ba867f2/68747470733a2f2f6c68332e676f6f676c6575736572636f6e74656e742e636f6d2f2d2d73446d683343613855412f5662587351665f55786f492f414141414141414141446f2f53545233447254726444552f733437372d496334322f5072657669657755706c6f61642e706e67)

[Crop Preview (large image)](https://picasaweb.google.com/104329650875154427869/KakGithub#6176102228563362898)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist kak/storage "*"

```

or add

```
"kak/storage": "*"

```

to the require section of your `composer.json` file and run command `composer update`

Usage
-----

[](#usage)

PLS set config params Yii::$app-&gt;params

```
/** @docs https://console.aws.amazon.com/iam/home Generation access key and secret */
$amazon_config = [
    'key' => '',
    'secret' => '',
    'bucket' => 'my',
    'level'  => 2,
    'type'   => 'amazon',
    'region' => 'us-east-1',
]

//...
'storage' => [
    'storages' => [
         // use amazon config
        'photo'  => $amazon_config,
        'custom_name' => [],
        // local server save files
        'tmp'  => [
            'level' => 0,
        ],
    ],
],
```

Example use controller this uploading

```
    public function actions()
    {
         return [
             'upload' => [
                 'class' => UploadAction::className(),
                 'form_name' => 'kak\storage\models\UploadForm',
                 'storage'  => 'tmp',   // save image default tmp storage
                 'resize_image' => [    // list formats
                     'preview'   => [1024,1024, UploadAction::IMAGE_RESIZE, 'options' => [] ],
                     'thumbnail' => [120,120, UploadAction::IMAGE_THUMB],
                     '350'       => [350,280, UploadAction::IMAGE_RESIZE],
                 ]
             ],
         ];
     }
```

resize\_image.options

- animate if true then save image gif animated else first moveclip
- quality 10 - 100

Save model then controller

```
    /**
    * @param $id int edit post
    * @return string
    */
    public function actionUpdate($id)
    {
        $model = $this->findPostById($id);
        $uploadForm = new \kak\storage\models\UploadForm(['meta_name' => 'image_base']);
        $uploadForm->meta = $postModel->images_json;

        if($this->savePostForm($model, $uploadForm)) {
            return $this->redirect(['/dashboard/post/update','id' => $postModel->id]);
        }
        return $this->render('form',compact(
            'model', 'uploadForm'
        ));
    }

    /**
     * @param $model Post
     * @param $uploadForm \kak\storage\models\UploadForm
     * @return bool
     */
    protected function savePostForm(&$model,&$uploadForm)
    {
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $result = $uploadForm->saveToStorage('tmp','images',[]);
            $model->images_json = Json::encode($result);

            return $model->save();
        }

        return false;
    }
```

Once the extension is installed, simply use it in your code by:

```
