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
Requires
- imagekit/imagekit: ^3.0
- symfony/config: ^6.0
- symfony/dependency-injection: ^6.0
- symfony/http-kernel: ^6.0
- symfony/string: ^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.14
- mockery/mockery: ^1.5
- phpstan/phpstan: ^1.9
- phpstan/phpstan-mockery: ^1.1
- phpunit/phpunit: ^9.5
- symfony/framework-bundle: ^6.0
- symfony/yaml: ^6.0
README
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 ifsigned
is set totrue
.
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.