PHP code example of wolfpack-it / yii2-glide

1. Go to this page and download the library: Download wolfpack-it/yii2-glide 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/ */

    

wolfpack-it / yii2-glide example snippets


'components' => [
    'glideSource' => [
        'class' => \creocoder\flysystem\LocalFilesystem::class,
        'path' => '</path/to/source-storage>'
    ],
]

'container' => [
    'definitions' => [
        \WolfpackIT\glide\components\Glide::class => [
            'class' => \WolfpackIT\glide\components\Glide::class,
            'source' => 'glideSource', // via component
            'cache' => [
                'class' => \creocoder\flysystem\LocalFilesystem::class,
                'path' => '</path/to/cache-storage>'
            ], // via configuration
            'watermarks' => \creocoder\flysystem\AwsS3Filesystem:class // via container
        ]
    ]
]

class GlideController extends yii\web\Controller
{
    /**
     * @return array
     */
    public function actions(): array
    {
        return ArrayHelper::merge(
            parent::actions(),
            [
                'index' => [
                    'class' => \WolfpackIT\glide\actions\GlideAction::class
                ]
            ]
        );
    }
}

'container' => [
    'definitions' => [
        \SamIT\Yii2\UrlSigner\UrlSigner::class => [
            'secret' => '<secret>',
        ],
    ]
]

class GlideController extends yii\web\Controller
{
    /**
     * @return array
     */
    public function behaviors(): array
    {
        return ArrayHelper::merge(
            [
                HmacFilter::class => [
                    'class' => HmacFilter::class,
                    'signer' => \Yii::$container->get(\SamIT\Yii2\UrlSigner\UrlSigner::class) //via Dependancy Injection
                    'signer' => $this->controller->module->get('<urlSignerComponent>') // via component
                ]
            ],
            parent::behaviors()
        );
    }

$urlSigner = \Yii::createObject(\SamIT\Yii2\UrlSigner\UrlSigner::class);

$url = [
    '/img/index', // NOTE: This must be the route from the root 
    'path' => '</path/to/image>'
];
$allowAddition = true; // Whether or not to allow image modifications after url generation
$expiration = new DateTime())->add(new DateInterval('P7D'));

$urlSigner->signParams(
    $url,
    $allowAddition,
    $expiration
);

echo yii\helpers\Url::to($url, true);

'container' => [
    'definitions' => [
         \League\Glide\Signatures\Signature::class => function(\yii\di\Container $container) {
            return \League\Glide\Signatures\SignatureFactory::create('<secret>');
         },
         \League\Glide\Urls\UrlBuilder::class => function(\yii\di\Container $container) {
             return new \League\Glide\Urls\UrlBuilder('', $container->get(\League\Glide\Signatures\Signature::class));
         },
    ]
]

class GlideController extends yii\web\Controller
{
    /**
     * @return array
     */
    public function behaviors(): array
    {
        return ArrayHelper::merge(
            [
                SignatureFilter::class => [
                    'class' => SignatureFilter::class,
                ]
            ],
            parent::behaviors()
        );
    }
}

$urlBuilder = \Yii::createObject(\League\Glide\Urls\UrlBuilder::class);

$url = [
    '/img/index', // NOTE: This must be the route from the root 
    'path' => '</path/to/image>',
];

$options = [
    'w' => 1000,
];

echo $urlBuilder->getUrl(Url::to($url), $options);