sokil/php-image

Realisation of some operations with images like croping and scaling.

1.0.0 2020-09-12 21:20 UTC

This package is auto-updated.

Last update: 2024-10-13 06:24:02 UTC


README

Build Status Latest Stable Version Coverage Status Gitter

Installation

You may install library through composer:

composer require sokil/php-image

Open image

Create instance of image:

$image = new \Sokil\Image\Image($pathToImage);

Factory incapsulates instantiating of all image objects and allow to confirure created images:

$factory = new \Sokil\Image\Factory;

Opening from filename:

$factory->openImage('/path/to/image.jpeg');

Opening from GD resource:

$factory->openImage($imageResource);

Creating new image:

$image = $factory->createImage(300, 200);

Resize image

There is four resize modes: 'scale', 'fit', 'crop' and 'cache'.

$newImage = $factory->resizeImage($image, $mode, $width, $height);

If you want to register own resize strategy, extend class from \Sokil\Image\AbstractResizeStrategy and add namespase:

// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'resize' => '\Vendor\ResizeStrategy',
    ],
]);
// through factory method
$factory->addResizeStrategyNamespace('\Vendor\ResizeStrategy');
// directly to image
$image->addResizeStrategyNamespace('\Vendor\ResizeStrategy');

Classes searches in priority of adding.

Crop image

To get part of image by specified wifth and height and in defined coordinates use:

$x = 10;
$y = 10;
$width = 20;
$height = 20;

$image->crop($x, $y, $width, $height);

Rotate image

Rotating is counter clockwise;

Rotate on 90 degrees:

$image->rotate(90);

Rotate on 45 degrees, and fill empty field with black color:

$image->rotate(45, '#000000');

Rotate on 45 degrees, and fill empty field with transparent green color:

$image->rotate(45, '#8000FF00');

Flip image

Flip in vertical direction:

$image->flipVertical();

Flip in horisontal direction

$image->flipHorisontal();

Flip in both directions

$image->flipBoth();

Filters

Greyscale image:

$factory->filterImage($image, 'greyscale');

If you want to register own filter strategy to support new filters, extend class from \Sokil\Image\AbstractFilterStrategy and add namespase:

// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'filter' => '\Vendor\FilterStrategy',
    ],
]);
// through factory method
$factory->addFilterStrategyNamespace('\Vendor\FilterStrategy');
// or directly to image
$image->addFilterStrategyNamespace('\Vendor\FilterStrategy');

Classes searches in priority of adding.

Image elements

Adding elements to image

Element is everything that can me append to image: text, shape, other image. First we need to create element instabce and configure it:

$someElement = $factory->createElement('someElement')->setParam1('someValue');

Than element placed to image to some coordinates:

$image->appendElementAtPosition($someElement, 30, 30);

You can create your own elements that inherits \Sokil\Image\AbstractElement class, and register namespace:

namespace Vendor\Elements;

class Circle extends \Sokil\Image\AbstractElement
{
    public function setRadius($r) { // code to set radius }
    
    public function draw($resource, $x, $y) 
    {
        // code to draw circle on image $resouce at coordinates ($x, $y)
    }
}

// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'element' => '\Vendor\Element',
    ],
]);
// through factory method
$factory->addElementNamespace('\Vendor\Elements');

Now you can draw your own circles:

$circle = $factory->createElement('circle')->setRadiud(100);
$image->appendElementAtPosition($circle, 100, 100);

Writing text

To create text element you can use one of methods:

$textElement = $factory->createElement('text');
// or through helper 
$textElement = $factory->createTextElement();

First we need to configure text element:

$factory = new \Sokil\Image\Factory();
        
// text element
$element = $factory
    ->createTextElement()
    ->setText('hello world')
    ->setAngle(20)
    ->setSize(40)
    ->setColor('#ababab')
    ->setFont(__DIR__ . '/FreeSerif.ttf');

Now we need to place element in image at some coordinates:

$image->appendElementAtPosition($element, 50, 150);

Save image

Library supports three formats of image: 'jpeg', 'png' and 'gif'.

To write image to disk you must define format of image and configure write strategy:

$factory->writeImage($image, 'jpeg', function(\Sokil\Image\WriteStrategy\JpegWriteStrategy $strategy) {
    $strategy->setQuality(98)->toFile('/path/to/file.jpg');
});

To send image to STDOUT you must define format of image and configure write strategy:

$factory->writeImage($image, 'jpeg', function(\Sokil\Image\WriteStrategy\JpegWriteStrategy $strategy) {
    $strategy->setQuality(98)->toStdout();
});

If you want to register own write strategy to support new image format, extend class from \Sokil\Image\AbstractWriteStrategy and add namespase:

// through factory constructor
$factory = new \Sokil\Image\Factory([
    'namespace' => [
        'write' => '\Vendor\WriteStrategy',
    ],
]);
// through factory method
$factory->addWriteStrategyNamespace('\Vendor\WriteStrategy');
// or directly to image
$image->addWriteStrategyNamespace('\Vendor\WriteStrategy');

Classes searches in priority of adding.