2017 © Pedro Peláez
 

library php-image

Realisation of some operations with images like croping and scaling.

image

sokil/php-image

Realisation of some operations with images like croping and scaling.

  • Sunday, July 2, 2017
  • by sokil
  • Repository
  • 2 Watchers
  • 3 Stars
  • 48 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 7 Versions
  • 7 % Grown

The README.md

php-image

Build Status Latest Stable Version Coverage Status Gitter, (*1)

Installation

You may install library through composer:, (*2)

composer require sokil/php-image

Open image

Create instance of image:, (*3)

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

Factory incapsulates instantiating of all image objects and allow to confirure created images:, (*4)

$factory = new \Sokil\Image\Factory;

Opening from filename:, (*5)

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

Opening from GD resource:, (*6)

$factory->openImage($imageResource);

Creating new image:, (*7)

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

Resize image

There is four resize modes: 'scale', 'fit', 'crop' and 'cache'., (*8)

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

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

// 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., (*10)

Crop image

To get part of image by specified wifth and height and in defined coordinates use:, (*11)

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

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

Rotate image

Rotating is counter clockwise;, (*12)

Rotate on 90 degrees:, (*13)

$image->rotate(90);

Rotate on 45 degrees, and fill empty field with black color:, (*14)

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

Rotate on 45 degrees, and fill empty field with transparent green color:, (*15)

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

Flip image

Flip in vertical direction:, (*16)

$image->flipVertical();

Flip in horisontal direction, (*17)

$image->flipHorisontal();

Flip in both directions, (*18)

$image->flipBoth();

Filters

Greyscale image:, (*19)

$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:, (*20)

// 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., (*21)

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:, (*22)

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

Than element placed to image to some coordinates:, (*23)

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

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

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:, (*25)

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

Writing text

To create text element you can use one of methods:, (*26)

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

First we need to configure text element:, (*27)

$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:, (*28)

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

Save image

Library supports three formats of image: 'jpeg', 'png' and 'gif'., (*29)

To write image to disk you must define format of image and configure write strategy:, (*30)

$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:, (*31)

$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:, (*32)

// 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., (*33)

The Versions

02/07 2017

dev-master

9999999-dev

Realisation of some operations with images like croping and scaling.

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-gd *

 

The Development Requires

by Avatar sokil

07/01 2017

0.8.1

0.8.1.0

Realisation of some operations with images like croping and scaling.

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-gd *

 

The Development Requires

by Avatar sokil

13/10 2014

0.7.2

0.7.2.0

Realisation of some operations with images like croping and scaling.

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-gd *

 

The Development Requires

by Avatar sokil

10/10 2014

0.7.1

0.7.1.0

Realisation of some operations with images like croping and scaling.

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-gd *

 

The Development Requires

by Avatar sokil

09/10 2014

0.6

0.6.0.0

Realisation of some operations with images like croping and scaling.

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-gd *

 

The Development Requires

by Avatar sokil

09/10 2014

0.5

0.5.0.0

Realisation of some operations with images like croping and scaling.

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-gd *

 

The Development Requires

by Avatar sokil

08/10 2014

0.3

0.3.0.0

Realisation of some operations with images like croping and scaling.

  Sources   Download

MIT

by Avatar sokil