PHP code example of tobento / service-imager

1. Go to this page and download the library: Download tobento/service-imager 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-imager example snippets


use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\ImagerInterface;

$imager = (new ImagerFactory())->createImager();

var_dump($imager instanceof ImagerInterface);
// bool(true)

use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Imager\Action;

$response = $imager
    ->resource(new File('path/image.jpg'))
    ->action(new Action\Crop(width: 200, height: 200))
    ->action(new Action\Save('path/image.webp'));
    
var_dump($response instanceof ResponseInterface);
// bool(true)

$response = $imager
    ->file('path/image.jpg')
    ->crop(width: 200, height: 200)
    ->save('path/image.webp');

use Tobento\Service\Imager\Resource\Base64;
use Tobento\Service\Imager\ResourceInterface;

$resource = new Base64(
    data: 'Base64 encoded image data'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->data());
// string(25) "Base64 encoded image data"

use Tobento\Service\Imager\Resource\Binary;
use Tobento\Service\Imager\ResourceInterface;

$resource = new Binary(
    data: 'Binary image data'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->data());
// string(17) "Binary image data"

use Tobento\Service\Imager\Resource\DataUrl;
use Tobento\Service\Imager\ResourceInterface;

$resource = new DataUrl(
    data: 'Data-URL encoded image data'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->data());
// string(27) "Data-URL encoded image data"

use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Filesystem\File as BaseFile;
use Tobento\Service\Imager\ResourceInterface;

$resource = new File(
    file: 'path/image.jpg' // string|BaseFile
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->file() instanceof BaseFile);
// bool(true)

use Tobento\Service\Imager\Resource\Stream;
use Tobento\Service\Imager\ResourceInterface;
use Psr\Http\Message\StreamInterface;

$resource = new Stream(
    stream: $stream // StreamInterface
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->stream() instanceof StreamInterface);
// bool(true)

use Tobento\Service\Imager\Resource\Url;
use Tobento\Service\Imager\ResourceInterface;

$resource = new Url(
    url: 'https://www.example.com/image.jpg'
);

var_dump($resource instanceof ResourceInterface);
// bool(true)

var_dump($resource->url());
// string(33) "https://www.example.com/image.jpg"

use Tobento\Service\Imager\Action\Background;

$action = new Background(
    color: '#333333' // string
);

$imager->action($action);

// or using method:
$imager->background(color: '#333333');

use Tobento\Service\Imager\Action\Blur;

$action = new Blur(
    blur: 20 // int, between 0 and 100.
);

$imager->action($action);

// or using method:
$imager->blur(20);

use Tobento\Service\Imager\Action\Brightness;

$action = new Brightness(
    brightness: 20 // int, between -100 and 100.
);

$imager->action($action);

// or using method:
$imager->brightness(20);

use Tobento\Service\Imager\Action\Colorize;

$action = new Colorize(
    red: 20 // int, between -100 and 100.
    green: 5 // int, between -100 and 100.
    blue: 45 // int, between -100 and 100.
);

$imager->action($action);

// or using method:
$imager->colorize(red: 20, green: 5, blue: 45);

use Tobento\Service\Imager\Action\Contrast;

$action = new Contrast(
    contrast: 20 // int, between -100 and 100.
);

$imager->action($action);

// or using method:
$imager->contrast(20);

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200, x: 10, y: 10);

$imager->action($action);

// or using method:
$imager->crop(width: 200, height: 200, x: 10, y: 10);

use Tobento\Service\Imager\Action\Encode;
use Tobento\Service\Imager\Response\Encoded;

$action = new Encode(
    mimeType: 'image/webp',
    quality: 90, // null|int
);

$response = $imager->action($action);

// or using method:
$response = $imager->encode(mimeType: 'image/webp');

// return type:
var_dump($response instanceof Encoded);
// bool(true)

use Tobento\Service\Imager\Action\Fit;

$action = new Fit(
    width: 200,
    height: 200,
    position: Fit::CENTER, // is default
    upsize: null // is default
);

$imager->action($action);

// or using method:
$imager->fit(width: 200, height: 200);

// with upsize limit: (image width is 200)
$imager->fit(width: 300, height: 150, upsize: 1.5); // limits to 250

$imager->fit(width: 300, height: 150, upsize: 1); // limits to 200

$imager->fit(width: 300, height: 150, upsize: 0.5); // limits to 150

use Tobento\Service\Imager\Action\Flip;

$action = new Flip(
    flip: Flip::HORIZONTAL, // is default
);

$imager->action($action);

// or using method:
$imager->flip(flip: Flip::HORIZONTAL);

use Tobento\Service\Imager\Action\Gamma;

$action = new Gamma(
    gamma: 2.3 // float, between 0.01 and 9.99.
);

$imager->action($action);

// or using method:
$imager->gamma(2.3);

use Tobento\Service\Imager\Action\Greyscale;

$action = new Greyscale();

$imager->action($action);

// or using method:
$imager->greyscale();

use Tobento\Service\Imager\Action\Orientate;

$action = new Orientate();

$imager->action($action);

// or using method:
$imager->orientate();

use Tobento\Service\Imager\Action\Pixelate;

$action = new Pixelate(
    pixelate: 10 // int, between 0 and 1000.
);

$imager->action($action);

// or using method:
$imager->pixelate(10);

use Tobento\Service\Imager\Action\Resize;

$action = new Resize(
    width: 200, // null|int
    height: null, // null|int
    keepRatio: true, // bool (true is default)
    upsize: null, // null|float
);

$imager->action($action);

// or using method:
$imager->resize(width: 200);

// resize the image to a width of 200: (will keep aspect ratio)
$imager->resize(width: 200);

// resize the image to a height of 200: (will keep aspect ratio)
$imager->resize(height: 200);

// resize image to a fixed size:
$imager->resize(width: 200, height: 100, keepRatio: false);

// resize only the width of the image:
$imager->resize(width: 200, keepRatio: false);

// resize only the height of the image:
$imager->resize(height: 200, keepRatio: false);

// with upsize limit: (image width is 200)
$imager->resize(width: 300, upsize: 1.5); // limits to 250

$imager->resize(width: 300, upsize: 1); // limits to 200

$imager->resize(width: 300, upsize: 0.5); // limits to 150

use Tobento\Service\Imager\Action\Rotate;

$action = new Rotate(
    degrees: 20 // float, between -360 and 360.
    bgcolor: '#ffffff' // string, is default
);

$imager->action($action);

// or using method:
$imager->rotate(degrees: 20);

use Tobento\Service\Imager\Action\Sepia;

$action = new Sepia();

$imager->action($action);

// or using method:
$imager->sepia();

use Tobento\Service\Imager\Action\Save;
use Tobento\Service\Imager\Response\File;

$action = new Save(
    filename: 'path/image.jpg',
    
    // The prioritized mimeType for the filename to save.
    mimeType: 'image/webp', // null|string
    
    quality: 90, // null|int
    
    overwrite: Save::OVERWRITE, // is default
    
    modeDir: 0755 // is default
);

$response = $imager->action($action);

// or using method:
$response = $imager->save(filename: 'path/image.jpg');

// return type:
var_dump($response instanceof File);
// bool(true)

use Tobento\Service\Imager\Action\Save;
use Tobento\Service\Imager\ActionException;

use Tobento\Service\Imager\Action\Sharpen;

$action = new Sharpen(
    sharpen: 20 // int, between 0 and 100.
);

$imager->action($action);

// or using method:
$imager->sharpen(20);

use Tobento\Service\Imager\Response\Encoded;
use Tobento\Service\Imager\ResponseInterface;

$response = $imager->encode(mimeType: 'image/png');

var_dump($response instanceof ResponseInterface);
// bool(true)

var_dump($response instanceof Encoded);
// bool(true)

// returns the encoded image data:
$encodedImage = $response->encoded();
$encodedImage = (string)$response;

// Returns the base 64 encoded image data:
$base64encodedImage = $response->base64();

// Returns the data url:
$base64encodedImageDataUrl = $response->dataUrl();

// Info data:
$mimeType = $response->mimeType(); // string
$extension = $response->extension(); // string (e.g. "jpg")
$imageWidth = $response->width(); // int
$imageHeight = $response->height(); // int
$imageSize = $response->size(); // null|int|float
$humanImageSize = $response->humanSize(); // string, like '15 KB'

use Tobento\Service\Imager\Response\File;
use Tobento\Service\Filesystem\File as BaseFile;
use Tobento\Service\Imager\ResponseInterface;

$response = $imager->save(filename: 'path/image.jpg');

var_dump($response instanceof ResponseInterface);
// bool(true)

var_dump($response instanceof File);
// bool(true)

var_dump($response->file() instanceof BaseFile);
// bool(true)

// returns the width of the image:
var_dump($response->width());
// int(200)

// returns the height of the image:
var_dump($response->height());
// int(100)

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\Action\Action;
use Tobento\Service\Imager\Action\Processable;
use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\ActionException;

class SomeActions extends Action implements Processable
{
    /**
     * Create a new instance of SomeActions.
     *
     * @param int $width
     */
    public function __construct(
        private int $width
    ) {}
    
    /**
     * Process the action.
     *
     * @param ImagerInterface $imager
     * @param int $srcWidth
     * @param int $srcHeight     
     * @return ImagerInterface|ResponseInterface
     * @throws ActionException
     */
    public function process(
        ImagerInterface $imager,
        int $srcWidth,
        int $srcHeight
    ): ImagerInterface|ResponseInterface {
        return $imager
            ->greyscale()
            ->resize(width: $this->width);
    }

    /**
     * Returns the action parameters.
     *
     * @return array
     */
    public function parameters(): array
    {
        return [
            'width' => $this->width,
        ];
    }
    
    /**
     * Returns a description of the action.
     *
     * @return string
     */
    public function description(): string
    {
        return 'Greyscaled and resized image to width :width.';
    }
}

$imager = (new ImagerFactory())->createImager();

$response = $imager
    ->file('path/image.jpg')
    ->action(new SomeActions(width: 200))
    ->save('path/image.webp');

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\InterventionImage\Processor;
use Tobento\Service\Imager\Action\Action;
use Tobento\Service\Imager\Action\Processable;
use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\ActionException;

class SomeNewAction extends Action implements Processable
{
    /**
     * Create a new instance of SomeAction.
     *
     * @param int $foo
     */
    public function __construct(
        private int $foo
    ) {}
    
    /**
     * Process the action.
     *
     * @param ImagerInterface $imager
     * @param int $srcWidth
     * @param int $srcHeight     
     * @return ImagerInterface|ResponseInterface
     * @throws ActionException
     */
    public function process(
        ImagerInterface $imager,
        int $srcWidth,
        int $srcHeight
    ): ImagerInterface|ResponseInterface {

        if ($imager->getProcessor() instanceof InterventionImage\Processor) {            
            $image = $imager->processor()->image();
            // do something with the intervention image.
            
            return $imager;
        }
        
        // handle other processor or throw ActionException
        
        return $imager;
    }

    /**
     * Returns the action parameters.
     *
     * @return array
     */
    public function parameters(): array
    {
        return ['foo' => $this->foo];
    }
    
    /**
     * Returns a description of the action.
     *
     * @return string
     */
    public function description(): string
    {
        return 'Some new action with foo :foo processed.';
    }
}

$imager = (new ImagerFactory())->createImager();

$response = $imager
    ->file('path/image.jpg')
    ->action(new SomeNewAction(foo: 200))
    ->save('path/image.webp');

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\Message;
use Tobento\Service\Imager\ActionsInterface;
use Tobento\Service\Message\MessagesFactoryInterface;

$imager = (new ImagerFactory())->createImager();

$response = $imager
    ->file('path/image.jpg')
    ->crop(width: 200, height: 200, x: 0, y: 0)
    ->save('path/image.webp');

$messagesFactory = new Message\MessagesFactory();

var_dump($messagesFactory instanceof MessagesFactoryInterface);
// bool(true)

// You may filter out actions:
$actions = $response->actions()->withoutProcessedBy();

$messages = $messagesFactory->createMessagesFromActions(
    actions: $actions // ActionsInterface
);

foreach($messages as $message) {
    var_dump((string)$message);
}

// string(52) "Cropped image to width 200, height 200, x 0 and y 0."
// string(56) "Saved image path/image.webp with quality "90"."

use Tobento\Service\Imager\ImagerFactoryInterface;
use Tobento\Service\Imager\InterventionImage\ImagerFactory;

$imagerFactory = new ImagerFactory();

var_dump($imagerFactory instanceof ImagerFactoryInterface);
// bool(true)

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\ImagerInterface;

$imagerFactory = new ImagerFactory();

$imager = $imagerFactory->createImager();

var_dump($imager instanceof ImagerInterface);
// bool(true)

use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\InterventionImage\ImagerFactory;

$imager = (new ImagerFactory())->createImager();

var_dump($imager instanceof ImagerInterface);
// bool(true)

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$resource = new File('path/image.jpg');

var_dump($resource instanceof ResourceInterface);
// bool(true)

$imager = $imager->resource(resource: $resource);

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$imager->resource(resource: new File('path/image.jpg'));

$resource = $imager->getResource();

var_dump($resource instanceof ResourceInterface);
// bool(true)

use Tobento\Service\Imager\ProcessorInterface;
use Tobento\Service\Imager\Resource\File;

$imager->resource(resource: new File('path/image.jpg'));

$processor = $imager->getProcessor();

var_dump($processor instanceof ProcessorInterface);
// bool(true)

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$imager->file(file: 'path/image.jpg');

var_dump($imager->getResource() instanceof File);
// bool(true)

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Action\Save;

$action = new Save(filename: 'path/image.jpg');

var_dump($action instanceof ActionInterface);
// bool(true)

$imager->action(action: $action);

use Tobento\Service\Imager\ProcessorFactoryInterface;
use Tobento\Service\Imager\InterventionImage\ProcessorFactory;

$processorFactory = new ProcessorFactory();

var_dump($processorFactory instanceof ProcessorFactoryInterface);
// bool(true)

use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\ProcessorInterface;
use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Imager\ProcessorCreateException;

$processorFactory = new ProcessorFactory();

$processor = $processorFactory->createProcessor(
    resource: new File('path/image.jpg'), // ResourceInterface
);

var_dump($processor instanceof ProcessorInterface);
// bool(true)

// throws ProcessorCreateException if processor could not get created.

use Tobento\Service\Imager\ProcessorInterface;
use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\Resource\File;

$processor = (new ProcessorFactory())->createProcessor(
    resource: new File('path/image.jpg'),
);

var_dump($processor instanceof ProcessorInterface);
// bool(true)

use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\Resource\File;
use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Action;
use Tobento\Service\Imager\ImagerInterface;
use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\ActionProcessException;

$processor = (new ProcessorFactory())->createProcessor(
    resource: new File('path/image.jpg'),
);

$imager = (new ImagerFactory())->createImager();

$processed = $processor->processAction(
    action: new Action\Crop(200, 200), // ActionInterface
    imager: $imager, // ImagerInterface
);

// $processed might be:
var_dump($processed instanceof ImagerInterface);
// bool(true)

// or depending on the action:
var_dump($processed instanceof ResponseInterface);
// bool(false)

// throws ActionProcessException if action could not get processed.

use Tobento\Service\Imager\ResourceInterface;
use Tobento\Service\Imager\Resource\File;

$resource = new File('path/image.jpg');

var_dump($resource instanceof ResourceInterface);
// bool(true)

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

var_dump($action instanceof ActionInterface);
// bool(true)

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

var_dump($action->parameters());
// array(4) { ["width"]=> int(200) ["height"]=> int(200) ["x"]=> NULL ["y"]=> NULL } 

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

var_dump($action->description());
// string(61) "Cropped image to width :width, height :height, x x: and y :y."

use Tobento\Service\Imager\Action\Crop;

$action = new Crop(width: 200, height: 200);

$action->setProcessedBy(action: 'ClassName');

var_dump($action->processedBy());
// string(9) "ClassName"

use Tobento\Service\Imager\ActionsInterface;
use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action\Crop;

$actions = new Actions(
    new Crop(width: 200, height: 200), // ActionInterface
);

var_dump($actions instanceof ActionsInterface);
// bool(true)

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action\Crop;

$actions = new Actions();

$actions->add(new Crop(width: 200, height: 200)); // ActionInterface

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action\Crop;

$actions = new Actions();

var_dump($actions->empty());
// bool(true)

$actions = new Actions(
    new Crop(width: 200, height: 200),
);

var_dump($actions->empty());
// bool(false)

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
);

$actions = $actions->filter(
    fn(ActionInterface $a): bool => in_array($a::class, [Action\Crop::class])
);

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
    new Action\Save(filename: 'image.jpg'),
);

$actions = $actions->only(Action\Crop::class, Action\Resize::class);

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
    new Action\Save(filename: 'image.jpg'),
);

$actions = $actions->except(Action\Save::class, Action\Resize::class);

use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Sepia(),
    (new Action\Greyscale())->setProcessedBy(Action\Sepia::class),
    new Action\Save(filename: 'image.jpg'),
);

$actions = $actions->withoutProcessedBy();

use Tobento\Service\Imager\ActionInterface;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\Action;

$actions = new Actions(
    new Action\Crop(width: 200, height: 200),
    new Action\Resize(width: 200),
);

foreach($actions->all() as $action) {
    var_dump($action instanceof ActionInterface);
    // bool(true)
}

// or just
foreach($actions as $action) {}

use Tobento\Service\Imager\ResponseInterface;
use Tobento\Service\Imager\Response\File;
use Tobento\Service\Imager\Actions;

$response = new File(
    file: 'path/image.jpg',
    actions: new Actions()
);

var_dump($response instanceof ResponseInterface);
// bool(true)

use Tobento\Service\Imager\Response\File;
use Tobento\Service\Imager\Actions;
use Tobento\Service\Imager\ActionsInterface;

$response = new File(
    file: 'path/image.jpg',
    actions: new Actions()
);

var_dump($response->actions() instanceof ActionsInterface);
// bool(true)

use Tobento\Service\Imager\InterventionImage\ImagerFactory;
use Tobento\Service\Imager\ImagerFactoryInterface;

$imagerFactory = new ImagerFactory(
    config: ['driver' => 'gd'], // is default
);

var_dump($imagerFactory  instanceof ImagerFactoryInterface);
// bool(true)

use Tobento\Service\Imager\InterventionImage\ProcessorFactory;
use Tobento\Service\Imager\ProcessorFactoryInterface;

$processorFactory = new ProcessorFactory(
    config: ['driver' => 'gd'], // is default
);

var_dump($processorFactory  instanceof ProcessorFactoryInterface);
// bool(true)