Image processing in PHP is hard, but now we have Imagine, inspired by Python PIL, made for PHP 5.3, this image manipulation library will meet all your needs
8. Imagine...
• all drivers implemented the same interfaces
• code could be reused with any driver
• there were interfaces for mocking in tests
• API was simple and intuitive
Saturday, March 5, 2011
24. Reflection
$imagine = new ImagineGdImagine();
$logo = $imagine->open('/path/to/google/logo.png');
$size = $logo->getSize();
$canvas = $imagine->create(
new ImagineBox($size->getWidth(), $size->getHeight() * 2),
new ImagineColor('000', 100)
);
$reflection = $logo->copy()
->flipVertically()
->applyMask(
$imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
new ImagineColor(array(127, 127, 127)),
new ImagineColor('fff')
)
)
);
$canvas->paste($logo, new ImaginePoint(0, 0))
->paste($reflection, new ImaginePoint(0, $size->getHeight()))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
25. Reflection
$imagine = new ImagineGdImagine();
$logo = $imagine->open('/path/to/google/logo.png');
$size = $logo->getSize();
$canvas = $imagine->create(
new ImagineBox($size->getWidth(), $size->getHeight() * 2 + 1),
new ImagineColor('000', 100)
);
$reflection = $logo->copy()
->flipVertically()
open image to reflect and remember its size
->applyMask(
$imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
new ImagineColor(array(127, 127, 127)),
new ImagineColor('fff')
)
)
);
$canvas->paste($logo, new ImaginePoint(0, 0))
->paste($reflection, new ImaginePoint(0, $size->getHeight()))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
26. Reflection
$imagine = new ImagineGdImagine();
$logo = $imagine->open('/path/to/google/logo.png');
$size = $logo->getSize();
$canvas = $imagine->create(
new ImagineBox($size->getWidth(), $size->getHeight() * 2),
new ImagineColor('000', 100)
);
$reflection = $logo->copy()
->flipVertically()
create empty canvas to fit image and reflection
->applyMask(
$imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
new ImagineColor(array(127, 127, 127)),
new ImagineColor('fff')
)
)
);
$canvas->paste($logo, new ImaginePoint(0, 0))
->paste($reflection, new ImaginePoint(0, $size->getHeight()))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
27. Reflection
$imagine = new ImagineGdImagine();
$logo = $imagine->open('/path/to/google/logo.png');
$size = $logo->getSize();
$canvas = $imagine->create(
new ImagineBox($size->getWidth(), $size->getHeight() * 2),
new ImagineColor('000', 100)
);
$reflection = $logo->copy()
->flipVertically()
make a copy of source, flipped vertically
->applyMask(
$imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
new ImagineColor(array(127, 127, 127)),
new ImagineColor('fff')
)
)
);
$canvas->paste($logo, new ImaginePoint(0, 0))
->paste($reflection, new ImaginePoint(0, $size->getHeight()))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
28. Reflection
$imagine = new ImagineGdImagine();
$logo = $imagine->open('/path/to/google/logo.png');
$size = $logo->getSize();
$canvas = $imagine->create(
new ImagineBox($size->getWidth(), $size->getHeight() * 2),
new ImagineColor('000', 100)
);
replace white regions with transparency
$reflection = $logo->copy()
->flipVertically()
->applyMask(
$imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
new ImagineColor(array(127, 127, 127)),
new ImagineColor('fff')
)
)
);
$canvas->paste($logo, new ImaginePoint(0, 0))
->paste($reflection, new ImaginePoint(0, $size->getHeight()))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
29. Reflection
$imagine = new ImagineGdImagine();
$logo = $imagine->open('/path/to/google/logo.png');
$size = $logo->getSize();
$canvas = $imagine->create(
new ImagineBox($size->getWidth(), $size->getHeight() * 2),
new ImagineColor('000', 100)
);
create image like the one above
$reflection = $logo->copy()
->flipVertically()
->applyMask(
$imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
new ImagineColor(array(127, 127, 127)),
new ImagineColor('fff')
)
)
);
$canvas->paste($logo, new ImaginePoint(0, 0))
->paste($reflection, new ImaginePoint(0, $size->getHeight()))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
30. Reflection
$imagine = new ImagineGdImagine();
$logo = $imagine->open('/path/to/google/logo.png');
$size = $logo->getSize();
$canvas = $imagine->create(
new ImagineBox($size->getWidth(), $size->getHeight() * 2),
new ImagineColor('000', 100)
);
$reflection = $logo->copy()
place original logo on top of created canvas
->flipVertically()
->applyMask(
place reflection underneath it
$imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
new ImagineColor(array(127, 127, 127)),
new ImagineColor('fff')
)
)
);
$canvas->paste($logo, new ImaginePoint(0, 0))
->paste($reflection, new ImaginePoint(0, $size->getHeight()))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
45. Filter
namespace ImagineFilter;
use ImagineImageInterface;
interface FilterInterface
{
/**
* Applies scheduled transformation to ImageInterface instance
* Returns processed ImageInterface instance
*
* @param ImagineImageInterface $image
*
* @return ImagineImageInterface
*/
function apply(ImageInterface $image);
}
Saturday, March 5, 2011
46. Filters
Filter is a collection of manipulations, calculations and other
operations, that can be applied to an image
Saturday, March 5, 2011
47. Reflection filter
class ReflectionFilter implements ImagineFilterFilterInterface
{
private $imagine;
public function __construct(ImagineImagineInterface $imagine)
{
$this->imagine = $imagine;
}
public function apply(ImagineImageInterface $image)
{
$size = $image->getSize();
$white = new ImagineColor('fff');
$canvas = new ImagineBox($size->getWidth(), $size->getHeight() * 2);
return $this->imagine->create($canvas)
->paste($image, new ImaginePoint(0, 0))
->paste($logo->copy()
->flipVertically()
->applyMask($this->imagine->create($size)
->fill(
new ImagineFillGradientVertical(
$size->getHeight(),
$white->darken(127),
$white
)
)
));
}
}
Saturday, March 5, 2011
48. Reflection filter
$imagine = new ImagineGdImagine();
$filter = new ReflectionFilter($imagine);
$filter->apply($imagine->open('/path/to/google/logo.png'))
->save('/path/to/google/logo/reflection.png');
Saturday, March 5, 2011
49. Transformation
Delayed image processing using a filter
Saturday, March 5, 2011
50. Transformation
$path = '/path/to/processed/image.png';
$size = new ImagineBox(50, 50);
$resize = new ImagineBox(200, 200);
$angle = 90;
$background = new ImagineColor('fff');
$transformation = new ImagineFilterTransformation();
$transformation->resize($resize)
->copy()
->rotate($angle, $background)
->thumbnail($size, ImagineImageInterface::THUMBNAIL_INSET)
->save($path);
operate on a transformation as on a regular image, except
nothing is being executed
Saturday, March 5, 2011
52. Transformation
foreach(glob('/path/to/many/images/*.png') as $path) {
$transformation->apply($imagine->open($path))
->save('/path/to/processed/image/'.md5($path).'.png');
}
Or even batch process...
Saturday, March 5, 2011
57. Process
<img src="/imagine/preview/users/1/photo.jpg" alt="avatar" />
first request processes image and outputs response
other controller requests result in a 301 redirect to file
Saturday, March 5, 2011
59. To be improved
• advanced operations are still not easy
• not all drivers are supported
• Imagick
• GD
• Gmagick
• library is very young, there might be issues
Saturday, March 5, 2011
60. Was improved
• thumbnails are easy
• code is readable
• foundation is solid
• its available today
• its gonna be great
Saturday, March 5, 2011