PHP code example of bgillieron / yii2-cloudinary

1. Go to this page and download the library: Download bgillieron/yii2-cloudinary library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bgillieron / yii2-cloudinary example snippets


'modules' => [
    'cloudinary' => [
        'class' => 'yii2cloudinary\Module',
    ],
],
'components' => [
    'yii2cloudinary' => [
        'class' => \yii2cloudinary\components\Yii2CloudinaryComponent::class,
        'cloudName' => $_ENV['CLOUDINARY_CLOUD_NAME'],
        'apiKey' => $_ENV['CLOUDINARY_API_KEY'],
        'apiSecret' => $_ENV['CLOUDINARY_API_SECRET'],
        'uploadPreset' => $_ENV['CLOUDINARY_UPLOAD_PRESET'],
        'uploadHandlerUrl' => '/cloudinary/yii2-cloudinary/upload-handler',
        'db_defaultPublished' => false,
        'db_defaultOrder' => 500,
        'relationSaverMap' => [
            'test-widget' => function ($media) {
                Yii::$app->db->createCommand()->insert('test_media_relation', [
                    'media_id' => $media->id,
                    'label' => 'Uploaded from test widget',
                ])->execute();
            },
        ],
    ],
],

Yii::$app->yii2cloudinary->upload('/path/to/image.jpg', [
    'folder' => 'my-uploads',
    'tags' => ['profile', 'gallery'],
    'relationKey' => 'post-image',
]);

Yii::$app->yii2cloudinary->upload('/path/to/image.jpg', [
    'folder' => 'product-images',
    'customPayload' => [
        'order' => 1,
        'published' => true,
        'product_id' => 123,
    ],
    'relationKey' => 'product-gallery',
]);

'relationSaverMap' => [
    'product-gallery' => function ($media, $payload) {
        Yii::$app->db->createCommand()->insert('product_media', [
            'media_id' => $media->id,
            'product_id' => $payload['product_id'] ?? null,
            'position' => $payload['order'] ?? 1,
        ])->execute();
    },
    'user-avatar' => fn($media) => Yii::$app->user->identity->link('avatar', $media),
    'post-image' => fn($media) => Post::findOne(5)->link('media', $media),
],

echo Yii::$app->yii2cloudinary->renderResponsiveImage($media, [400, 800], [
    'class' => 'img-fluid',
]);

Yii::$app->yii2cloudinary->uploadWidget('upload_widget', [
    'relationKey' => 'product-gallery',
    'customPayload' => [
        'product_id' => 123,
        'order' => 1,
        'published' => true,
    ],
    'reloadAfterClose' => true,
]);

'uploadHandlerUrl' => '/yii2cloudinary/upload-handler',

'uploadHandlerUrl' => '/custom/path/to/upload-handler',

use yii2cloudinary\assets\PhotoSwipeAsset;

PhotoSwipeAsset::register($this);

$cloudinary = Yii::$app->yii2cloudinary->getCloudinary();
$url = $cloudinary->image('your-public-id')->toUrl();
plaintext
yii2-cloudinary/
├── src/
│   ├── assets/
│   │   ├── photoswipe/
│   │   │   ├── photoswipe-lightbox.esm.min.js
│   │   │   ├── photoswipe.esm.min.js
│   │   │   ├── photoswipe.css
│   │   └── PhotoSwipeAsset.php
│   ├── components/
│   │   └── Yii2CloudinaryComponent.php
│   ├── controllers/
│   │   └── Yii2CloudinaryController.php
│   ├── messages/
│   │   ├── en/
│   │   │   └── uploadWidget.php
│   │   └── text.json
│   ├── migrations/
│   │   └── m250430_120000_create_cloudinary_media_tables.php
│   ├── models/
│   │   ├── CloudinaryImageMeta.php
│   │   ├── CloudinaryMedia.php
│   │   ├── CloudinaryMediaDesc.php
│   │   └── Module.php
├── cloudinary-media-migrations.md
├── composer.json
├── LICENSE
├── README.md
├── render-responsive-image.md
└── upload-widget-localization.md
bash
php yii migrate --migrationPath=@yii2cloudinary/migrations
html
 $baseUrl = $this->assetManager->getBundle(\yii2cloudinary\assets\PhotoSwipeAsset::class)->baseUrl;