geekcell/imagekit-bundle

A Symfony bundle for Imagekit integration

Installs: 4 090

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.0.4 2023-07-21 14:30 UTC

This package is auto-updated.

Last update: 2024-10-21 17:29:12 UTC


README

Unit tests workflow status Coverage Bugs Maintainability Rating Quality Gate Status

A Symfony bundle for smooth integration with the PHP SDK for ImageKit.

Installation

To use this package, require it in your Symfony project with Composer.

composer require geekcell/imagekit-bundle

Verify that the bundle has been enabled in config/bundles.php

<?php

return [
    // other bundles ...
    GeekCell\ImagekitBundle\GeekCellImagekitBundle::class => ['all' => true],
];

Usage

This bundle uses the concept of "providers" to return an ImageKit asset. The recommended way to interact with providers is to configure them inside config/packages/geek_cell_imagekit.yaml

geek_cell_imagekit:
    public_key: '%env(IMAGEKIT_PUBLIC_KEY)%'
    private_key: '%env(IMAGEKIT_PRIVATE_KEY)%'
    base_url: 'https://ik.imagekit.io'
    configurations:
        user_avatars:
            endpoint: '/user/avatars'
            transformation:
                width: 150
                height: 150
                quality: 70
            signed: false
        user_profile_images:
            endpoint: '/user/profile_images'
            transformation:
                width: 800
                height: 800
                quality: 80
            signed: true
            expires: 3600

In the example above, we've defined two configurations called user_avatars and user_profile_images. Currently, the following settings are supported:

  • endpoint - Custom endpoints configured in your ImageKit account.
  • transformation - Key-value pairs of transformations to apply to the asset. Click here the full list of supported transformations.
  • signed - Append a signature to your asset's URL.
  • expires - Expiration time in seconds; must be set if signed is set to true.

If you're using autowiring in your Symfony project, you can then simply typehint GeekCell\ImagekitBundle\Imagekit\ProviderRegistry in your services and/or controllers to inject a registry from which you can retrieve every configured provider by its name.

#[AsController]
class AvatarController extends AbstractController
{
    private Provider $avatarProvider;

    public function __construct(ProviderRegistry $registry)
    {
        $this->avatarProvider = $registry->getProvider('user_avatars');
    }

    #[Route('/avatar', name: 'avatar')]
    public function avatar()
    {
        $asset = $this->avatarProvider->provide('some-username.png');
        
        return new JsonResponse([
            'url' => $asset->getUrl(),
        ]);
    }
}

In a real-world application, of course, you would replace the hard-coded some-username.png path with a path returned from some datastore that corresponds to the asset source(s) you've configured in your ImageKit account.