Skip to content

Latest commit

History

42 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Image Tools

A simple image editing tool for PHP. It uses the immutable object style. It means that an image object can't be changed after creation, the result of any modifying method of an image object is a new image object.

Installation

Composer

Add this to the composer.json file:

"require": {
"aiger-team/image-tools": "~1.0.7"
}

and then call:

composer install

Or use the composer require command:

composer require aiger-team/image-tools

Manually

Download the files from GitHub, put them to your code directory and require them into your PHP code.

Examples

Resize an image:

useAigerTeam\ImageTools\ImageFactory;
(newImageFactory())
->openFile('image.jpg')
->resize(600, 500)
->toFile('image-resized.jpg');

Create a few thumbnails from an uploaded image and put a watermark on some of them:

useAigerTeam\ImageTools\ImageFactory;
// List of thumb to create. The values are width, height and "put watermark?".$thumbsSizes = [
[150, 80, false],
[300, 200, true],
[500, 400, true]
];
// Open uploaded image and watermark image$factory = newImageFactory();
$image = $factory->openFile($_FILES['image']['tmp_name']);
$watermark = $factory->openFile('watermark.png');
// Make thumbs$thumbsFiles = array_map(function($size) use ($image, $watermark)
{
$thumb = $image->resize($size[0], $size[1], false, $image::SIZING_COVER);	// Original $image is not modified so thumbs may be created in any orderif ($size[2]) {
$thumb = $thumb->stamp($watermark, 0.2);	// Watermark size is relative, not pixel
}
return$thumb->toUncertainFile('uploads/thumbs');	// File name and format is set automatically
}, $thumbSizes);

Reference

None of methods triggers errors or warnings. Instead of that methods throw exceptions. If some of them triggers an error please report us.

Creating Image object

An Image object can be created by various ways.

1. Using one of the ImageFactory object method

useAigerTeam\ImageTools\ImageFactory;
$factory = newImageFactory();
$image = $factory->blank(100, 150);

See the PHPDoc in the ImageFactoryclass code for more details and the list of all image creating methods.

2. Passing a DG image resource to the Image constructor

useAigerTeam\ImageTools\Image;
$resource = imagecreatetruecolor(100, 150);
$image = newImage($resource);

The resource passed to the constructor becomes an Image object own. So the resource can be modified inside the constructor and the resource will be automatically destroyed on the Image object destruction.

Processing an image

Image object is immutable. The Image methods that modify image return a new object or the original object (if it's not modified). So this code is incorrect:

// WRONG! Don't do this or you will be fired.$image->resize(200, 150);
$image->toFile('image.jpg');

This one is correct:

// Variant 1$image = $image->resize(200, 150);
$image->toFile('image.jpg');
// Variant 2$image
->resize(200, 150)
->toFile('image.jpg');

See the PHPDoc in the Imageclass code for more details and the list of all methods.

Version compatibility

Versions are backward compatible within minor versions. For example versions 1.1.3 and 1.1.5 are backward compatible, but versions 1.1.3 and 1.2.1 may be not compatible. So we advice you to set a specific minor version in the composer configuration, for example ~1.0.7.

License

ImageTools is licensed under the MIT License. See the LICENSE file for details.

About

Light image editing tool for PHP with immutable objects style

Topics

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages