This module was created for the usage in Thapp\JitImage, but can be used as a standalone library for manipulating images. It's highly inspired by the Imagine library, but resolves a views flaws, but also way more limited.
Require thapp/image
in your project directory
$ composer require thapp/image
or add this to your composer.json
{
"require": {
"thapp/image": "dev-master"
}
}
<?php
use Thapp\Image\Geometry\Size;
use Thapp\Image\Driver\Imagick\Source;
$source = new Source;
$image = $source->load('image.jpg');
$image->edit()->crop(new Size(100, 100));
$image->save('newimage.jpg');
The Source
object is able to create Image
instances from either filepaths,
filehandles, or binary strings:
<?php
use Thapp\Image\Driver\Imagick\Source;
$source = new Source;
$image = $source->load('image.jpg');
// or read the file from a file handle:
$handle = fopen('image.jpg', 'r+');
$image = $source->read($handle);
// or read the file from a binary string:
$content = file_get_contents('image.jpg');
$image = $source->create($content);
The Source
class takes an instance of
Thapp\Image\Info\MetaDataReaderInterface
as its first argument. The $reader
is used to read meta information about the image. This is useful e.g. if you
want to autorotate the image based on its orientation.
By default, a new instance of Thapp\Image\Info\ImageReader
is created for
you. ImageReader
is capable of reading basic image information derived from
the php getimagesize()
function.
You may use the Thapp\Image\Info\ImageReader
class instead, which provides
a wider range of information (e.g. needed for GD
and Gmagick
drivers to
determine the correct image orientation).
<?php
use Thapp\Image\Info\ExifReader;
use Thapp\Image\Driver\Imagick\Source;
$source = new Source(new ExifReader);
// ...
$image = $source->load('image.jpg');