PHPackages                             powerkernel/yii-cloudinary - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. powerkernel/yii-cloudinary

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

powerkernel/yii-cloudinary
==========================

Yii Cloudinary Component

066PHP

Since Aug 14Pushed 7y ago1 watchersCompare

[ Source](https://github.com/powerkernel/yii-cloudinary)[ Packagist](https://packagist.org/packages/powerkernel/yii-cloudinary)[ RSS](/packages/powerkernel-yii-cloudinary/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Yii2 file/image upload to Cloudinary component and behavior for ActiveRecord
============================================================================

[](#yii2-fileimage-upload-to-cloudinary-component-and-behavior-for-activerecord)

This package contains Component and Behavior for upload and display files from [Cloudinary](https://cloudinary.com/) service.

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

[](#installation)

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

Either run

```
php composer.phar require nikosid/yii2-coudinary "dev-master"

```

or add

```
"nikosid/yii2-coudinary": "dev-master"

```

to the `require` section of your composer.json.

CloudinaryComponent
-------------------

[](#cloudinarycomponent)

You need to configure cloudinary component in your application config.

```
    'components' => [
        'cloudinary' => [
            'class' => CloudinaryComponent::class,
            'cloud_name' => 'YOUR_CLOUD_NAME',
            'api_key' => 'YOUR_API_KEY',
            'api_secret' => 'YOUR_API_SECRET',
            'cdn_subdomain' => true,//optional
            'useSiteDomain' => false,
        ],
    ],
```

By setting **$useSiteDomain** to true you can make URLs to your doman and than proxy them to cloudinary server. By default it's false.

\###Example of nginx config for forward traffic to cloudinary server ###

```
    location /YOUR_CLOUD_NAME/ {
        proxy_pass https://res.cloudinary.com;
        proxy_set_header Host res.cloudinary.com;
    }

```

CloudinaryBehavior
------------------

[](#cloudinarybehavior)

This behavior allows you to add file uploading logic with ActiveRecord behavior.

### Usage

[](#usage)

Attach the behavior to your model class:

```
    public function behaviors()
    {
        return [
            'cloudynary' => [
                'class' => CloudinaryBehavior::class,
                'attribute' => 'picture',
                'publicId' => Yii::$app->name . '/articles/main{id}',
                'thumbs' => [
                    'large' => ['secure' => true, 'width' => 848, 'height' => 536, 'crop' => 'fill'],
                    'medium' => ['secure' => true, 'width' => 555, 'height' => 536, 'crop' => 'fill'],
                    'small' => ['secure' => true, 'width' => 130, 'height' => 125, 'crop' => 'fill'],
                ],
            ],
        ];
    }
```

Add validation rule:

```

    //For file upload
    public function rules()
    {
        return [
            ['picture', 'image', 'extensions' => 'jpg, jpeg, gif, png', 'on' => ['insert', 'update']],
        ];
    }

    //Or for url type field
    public function rules()
    {
        return [
            ['url_picture', 'url',],
        ];
    }
```

Example view file for upload file from local storage:

```
