1. Go to this page and download the library: Download id-sign/image-bundle 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/ */
use Symfony\Component\Validator\Constraints as Assert;
class Product
{
#[Assert\Image(
maxSize: '5M',
maxWidth: 4000,
maxHeight: 4000,
mimeTypes: ['image/jpeg', 'image/png', 'image/webp'],
detectCorrupted: true,
)]
public ?File $image = null;
}
use IdSign\ImageBundle\Cache\CacheStorageInterface;
class ImageUploadHandler
{
public function __construct(
private readonly CacheStorageInterface $cacheStorage,
) {}
public function onImageUpdated(string $src): void
{
$this->cacheStorage->deleteBySource($src); // e.g. 'uploads/photo.jpg'
}
}
use IdSign\ImageBundle\Service\ImageUrlGenerator;
class ImageApiController
{
public function __construct(
private readonly ImageUrlGenerator $imageUrlGenerator,
) {}
public function getImageUrl(Request $request): JsonResponse
{
// Negotiate format from Accept header (AVIF > WebP > original)
$url = $this->imageUrlGenerator->generateFromRequest($request, 'uploads/photo.jpg', 800, 600, 'cover');
// Or specify format explicitly
$url = $this->imageUrlGenerator->generate('uploads/photo.jpg', 800, 600, 'cover', 80, 'webp');
return new JsonResponse(['image' => $url]);
}
}
bash
# Files not modified in 30 days
php bin/console image:purge --modified-before=30 --force
# Files not accessed in 14 days
php bin/console image:purge --accessed-before=14 --force
# Combine both
php bin/console image:purge --modified-before=30 --accessed-before=14 --force
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.