PHP code example of sokil / php-image

1. Go to this page and download the library: Download sokil/php-image 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/ */

    

sokil / php-image example snippets


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

$factory = new \Sokil\Image\Factory;

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

$factory->openImage($imageResource);

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

// 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');

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

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

$image->rotate(90);

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

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

$image->flipVertical();

$image->flipHorisontal();

$image->flipBoth();

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

// 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');

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

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

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');

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

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

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

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

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

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

// 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');

composer