PHP code example of tobento / service-picture-generator

1. Go to this page and download the library: Download tobento/service-picture-generator 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/ */

    

tobento / service-picture-generator example snippets


<?= $pictureGenerator->generate(
    // The path to the original image within the storage:
    path: 'path/to/image.jpg',

    // The storage name where the image is located:
    resource: 'storage-name',

    // The named picture definition to use:
    definition: 'name',

    // Whether to queue image generation (recommended):
    queue: true, // default

    // Whether private storages may be read:
    allowPrivateStorage: false, // default
)->imgAttr('alt', 'Alt Text') 

use Tobento\Service\Picture\Definition\ArrayDefinition;

<?= $pictureGenerator->generate(
    path: 'path/to/image.jpg',
    resource: 'storage-name',
    definition: new ArrayDefinition('product-main', [
        'img' => [
            'src' => [600],
            'alt' => 'Alternative Text',
            'loading' => 'lazy',
        ],
        // You may define any sources:
        'sources' => [
            [
                'media' => '(min-width: 800px)',
                'srcset' => [
                    '' => [1200, 500],
                ],
                'type' => 'image/webp',
            ],
            [
                'media' => '(max-width: 600px)',
                'srcset' => [
                    '' => [600, 400],
                ],
                'type' => 'image/webp',
            ],
        ],
    ]),
) 

use Tobento\Service\Imager\ResourceInterface;

<?= $pictureGenerator->generate(
    path: 'path/to/image.jpg',
    resource: $resource, // ResourceInterface
    definition: 'name',
)->imgAttr('alt', 'Alt Text') 

use Psr\Log\LoggerInterface;
use Tobento\Service\FileStorage\StoragesInterface;
use Tobento\Service\Picture\DefinitionsInterface;
use Tobento\Service\Picture\Generator\PictureGenerator;
use Tobento\Service\Picture\Generator\PictureGeneratorInterface;
use Tobento\Service\Picture\Generator\PictureRepository;
use Tobento\Service\Picture\Generator\PictureRepositoryInterface;
use Tobento\Service\Picture\Generator\Queue\PictureQueueHandler;
use Tobento\Service\Picture\Generator\Queue\PictureQueueHandlerInterface;
use Tobento\Service\Picture\PictureCreatorInterface;
use Tobento\Service\Queue\QueueInterface;

// The repository stores picture metadata and generated image paths:
$pictureRepository = new PictureRepository(
    pictureStorageName: 'generated-picture-data', // Storage for picture metadata
    imageStorageName: 'generated-images', // Storage for generated images
    storages: $storages, // StoragesInterface
);

// Optional queue handler for background image generation:
$queueHandler = new PictureQueueHandler(
    queue: $queue, // QueueInterface
    queueName: 'name', // Queue name to dispatch picture jobs to
);

// Create the picture generator:
$pictureGenerator = new PictureGenerator(
    pictureRepository: $pictureRepository, // PictureRepositoryInterface
    storages: $storages, // StoragesInterface
    definitions: $definitions, // DefinitionsInterface
    queueHandler: $queueHandler, // Or null for no queueing at all
    uniqueJob: true, // Whether queued jobs should be unique
    pictureCreator: null, // Custom PictureCreatorInterface or null (default)
    logger: $logger, // LoggerInterface or null
);

var_dump($pictureGenerator instanceof PictureGeneratorInterface);
// bool(true)

use Tobento\App\Media\Picture\PictureGeneratorInterface;

class SomeService
{
    public function __construct(
        protected PictureGeneratorInterface $pictureGenerator,
    ) {}

    private function deleteGeneratedPictures(): void
    {
        $repository = $this->pictureGenerator->pictureRepository();

        // Delete the generated picture (all created images) for a specific path and definition:
        $repository->delete(
            path: 'foo/image.jpg',
            definition: 'product-main',
        );

        // Delete all generated pictures for a specific definition:
        $repository->deleteAll(
            definition: 'product-main',
        );

        // Delete all generated pictures for a specific original path:
        $repository->deleteAllByPath(
            path: 'foo/image.jpg',
        );
    }
}

php ap picture:clear