Added: Incorrect image orientation

This commit is contained in:
RainLoop Team 2015-10-29 23:02:58 +03:00
parent 217207883f
commit e3b62d24d8
87 changed files with 10461 additions and 21 deletions

View file

@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Image\Palette\Color\ColorInterface;
/**
* Abstract font base class
*/
abstract class AbstractFont implements FontInterface
{
/**
* @var string
*/
protected $file;
/**
* @var integer
*/
protected $size;
/**
* @var ColorInterface
*/
protected $color;
/**
* Constructs a font with specified $file, $size and $color
*
* The font size is to be specified in points (e.g. 10pt means 10)
*
* @param string $file
* @param integer $size
* @param ColorInterface $color
*/
public function __construct($file, $size, ColorInterface $color)
{
$this->file = $file;
$this->size = $size;
$this->color = $color;
}
/**
* {@inheritdoc}
*/
final public function getFile()
{
return $this->file;
}
/**
* {@inheritdoc}
*/
final public function getSize()
{
return $this->size;
}
/**
* {@inheritdoc}
*/
final public function getColor()
{
return $this->color;
}
}

View file

@ -0,0 +1,120 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Image\Metadata\MetadataBag;
abstract class AbstractImage implements ImageInterface
{
/**
* @var MetadataBag
*/
protected $metadata;
/**
* {@inheritdoc}
*
* @return ImageInterface
*/
public function thumbnail(BoxInterface $size, $mode = ImageInterface::THUMBNAIL_INSET, $filter = ImageInterface::FILTER_UNDEFINED)
{
if ($mode !== ImageInterface::THUMBNAIL_INSET &&
$mode !== ImageInterface::THUMBNAIL_OUTBOUND) {
throw new InvalidArgumentException('Invalid mode specified');
}
$imageSize = $this->getSize();
$ratios = array(
$size->getWidth() / $imageSize->getWidth(),
$size->getHeight() / $imageSize->getHeight()
);
$thumbnail = $this->copy();
$thumbnail->usePalette($this->palette());
$thumbnail->strip();
// if target width is larger than image width
// AND target height is longer than image height
if ($size->contains($imageSize)) {
return $thumbnail;
}
if ($mode === ImageInterface::THUMBNAIL_INSET) {
$ratio = min($ratios);
} else {
$ratio = max($ratios);
}
if ($mode === ImageInterface::THUMBNAIL_OUTBOUND) {
if (!$imageSize->contains($size)) {
$size = new Box(
min($imageSize->getWidth(), $size->getWidth()),
min($imageSize->getHeight(), $size->getHeight())
);
} else {
$imageSize = $thumbnail->getSize()->scale($ratio);
$thumbnail->resize($imageSize, $filter);
}
$thumbnail->crop(new Point(
max(0, round(($imageSize->getWidth() - $size->getWidth()) / 2)),
max(0, round(($imageSize->getHeight() - $size->getHeight()) / 2))
), $size);
} else {
if (!$imageSize->contains($size)) {
$imageSize = $imageSize->scale($ratio);
$thumbnail->resize($imageSize, $filter);
} else {
$imageSize = $thumbnail->getSize()->scale($ratio);
$thumbnail->resize($imageSize, $filter);
}
}
return $thumbnail;
}
/**
* Updates a given array of save options for backward compatibility with legacy names
*
* @param array $options
*
* @return array
*/
protected function updateSaveOptions(array $options)
{
// Preserve BC until version 1.0
if (isset($options['quality']) && !isset($options['jpeg_quality'])) {
$options['jpeg_quality'] = $options['quality'];
}
return $options;
}
/**
* {@inheritdoc}
*/
public function metadata()
{
return $this->metadata;
}
/**
* Assures the metadata instance will be cloned, too
*/
public function __clone()
{
if ($this->metadata !== null) {
$this->metadata = clone $this->metadata;
}
}
}

View file

@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Image\Metadata\DefaultMetadataReader;
use Imagine\Image\Metadata\ExifMetadataReader;
use Imagine\Image\Metadata\MetadataReaderInterface;
use Imagine\Exception\InvalidArgumentException;
abstract class AbstractImagine implements ImagineInterface
{
/** @var MetadataReaderInterface */
private $metadataReader;
/**
* @param MetadataReaderInterface $metadataReader
*
* @return ImagineInterface
*/
public function setMetadataReader(MetadataReaderInterface $metadataReader)
{
$this->metadataReader = $metadataReader;
return $this;
}
/**
* @return MetadataReaderInterface
*/
public function getMetadataReader()
{
if (null === $this->metadataReader) {
if (ExifMetadataReader::isSupported()) {
$this->metadataReader = new ExifMetadataReader();
} else {
$this->metadataReader = new DefaultMetadataReader();
}
}
return $this->metadataReader;
}
/**
* Checks a path that could be used with ImagineInterface::open and returns
* a proper string.
*
* @param string|object $path
*
* @return string
*
* @throws InvalidArgumentException In case the given path is invalid.
*/
protected function checkPath($path)
{
// provide compatibility with objects such as \SplFileInfo
if (is_object($path) && method_exists($path, '__toString')) {
$path = (string) $path;
}
$handle = @fopen($path, 'r');
if (false === $handle) {
throw new InvalidArgumentException(sprintf('File %s does not exist.', $path));
}
fclose($handle);
return $path;
}
}

View file

@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
abstract class AbstractLayers implements LayersInterface
{
/**
* {@inheritdoc}
*/
public function add(ImageInterface $image)
{
$this[] = $image;
return $this;
}
/**
* {@inheritdoc}
*/
public function set($offset, ImageInterface $image)
{
$this[$offset] = $image;
return $this;
}
/**
* {@inheritdoc}
*/
public function remove($offset)
{
unset($this[$offset]);
return $this;
}
/**
* {@inheritdoc}
*/
public function get($offset)
{
return $this[$offset];
}
/**
* {@inheritdoc}
*/
public function has($offset)
{
return isset($this[$offset]);
}
}

View file

@ -0,0 +1,122 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Exception\InvalidArgumentException;
/**
* A box implementation
*/
final class Box implements BoxInterface
{
/**
* @var integer
*/
private $width;
/**
* @var integer
*/
private $height;
/**
* Constructs the Size with given width and height
*
* @param integer $width
* @param integer $height
*
* @throws InvalidArgumentException
*/
public function __construct($width, $height)
{
if ($height < 1 || $width < 1) {
throw new InvalidArgumentException(sprintf('Length of either side cannot be 0 or negative, current size is %sx%s', $width, $height));
}
$this->width = (int) $width;
$this->height = (int) $height;
}
/**
* {@inheritdoc}
*/
public function getWidth()
{
return $this->width;
}
/**
* {@inheritdoc}
*/
public function getHeight()
{
return $this->height;
}
/**
* {@inheritdoc}
*/
public function scale($ratio)
{
return new Box(round($ratio * $this->width), round($ratio * $this->height));
}
/**
* {@inheritdoc}
*/
public function increase($size)
{
return new Box((int) $size + $this->width, (int) $size + $this->height);
}
/**
* {@inheritdoc}
*/
public function contains(BoxInterface $box, PointInterface $start = null)
{
$start = $start ? $start : new Point(0, 0);
return $start->in($this) && $this->width >= $box->getWidth() + $start->getX() && $this->height >= $box->getHeight() + $start->getY();
}
/**
* {@inheritdoc}
*/
public function square()
{
return $this->width * $this->height;
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return sprintf('%dx%d px', $this->width, $this->height);
}
/**
* {@inheritdoc}
*/
public function widen($width)
{
return $this->scale($width / $this->width);
}
/**
* {@inheritdoc}
*/
public function heighten($height)
{
return $this->scale($height / $this->height);
}
}

View file

@ -0,0 +1,94 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
/**
* Interface for a box
*/
interface BoxInterface
{
/**
* Gets current image height
*
* @return integer
*/
public function getHeight();
/**
* Gets current image width
*
* @return integer
*/
public function getWidth();
/**
* Creates new BoxInterface instance with ratios applied to both sides
*
* @param float $ratio
*
* @return BoxInterface
*/
public function scale($ratio);
/**
* Creates new BoxInterface, adding given size to both sides
*
* @param integer $size
*
* @return BoxInterface
*/
public function increase($size);
/**
* Checks whether current box can fit given box at a given start position,
* start position defaults to top left corner xy(0,0)
*
* @param BoxInterface $box
* @param PointInterface $start
*
* @return Boolean
*/
public function contains(BoxInterface $box, PointInterface $start = null);
/**
* Gets current box square, useful for getting total number of pixels in a
* given box
*
* @return integer
*/
public function square();
/**
* Returns a string representation of the current box
*
* @return string
*/
public function __toString();
/**
* Resizes box to given width, constraining proportions and returns the new box
*
* @param integer $width
*
* @return BoxInterface
*/
public function widen($width);
/**
* Resizes box to given height, constraining proportions and returns the new box
*
* @param integer $height
*
* @return BoxInterface
*/
public function heighten($height);
}

View file

@ -0,0 +1,30 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Fill;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\PointInterface;
/**
* Interface for the fill
*/
interface FillInterface
{
/**
* Gets color of the fill for the given position
*
* @param PointInterface $position
*
* @return ColorInterface
*/
public function getColor(PointInterface $position);
}

View file

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Fill\Gradient;
use Imagine\Image\PointInterface;
/**
* Horizontal gradient fill
*/
final class Horizontal extends Linear
{
/**
* {@inheritdoc}
*/
public function getDistance(PointInterface $position)
{
return $position->getX();
}
}

View file

@ -0,0 +1,95 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Fill\Gradient;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\Fill\FillInterface;
use Imagine\Image\PointInterface;
/**
* Linear gradient fill
*/
abstract class Linear implements FillInterface
{
/**
* @var integer
*/
private $length;
/**
* @var ColorInterface
*/
private $start;
/**
* @var ColorInterface
*/
private $end;
/**
* Constructs a linear gradient with overall gradient length, and start and
* end shades, which default to 0 and 255 accordingly
*
* @param integer $length
* @param ColorInterface $start
* @param ColorInterface $end
*/
final public function __construct($length, ColorInterface $start, ColorInterface $end)
{
$this->length = $length;
$this->start = $start;
$this->end = $end;
}
/**
* {@inheritdoc}
*/
final public function getColor(PointInterface $position)
{
$l = $this->getDistance($position);
if ($l >= $this->length) {
return $this->end;
}
if ($l < 0) {
return $this->start;
}
return $this->start->getPalette()->blend($this->start, $this->end, $l / $this->length);
}
/**
* @return ColorInterface
*/
final public function getStart()
{
return $this->start;
}
/**
* @return ColorInterface
*/
final public function getEnd()
{
return $this->end;
}
/**
* Get the distance of the position relative to the beginning of the gradient
*
* @param PointInterface $position
*
* @return integer
*/
abstract protected function getDistance(PointInterface $position);
}

View file

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Fill\Gradient;
use Imagine\Image\PointInterface;
/**
* Vertical gradient fill
*/
final class Vertical extends Linear
{
/**
* {@inheritdoc}
*/
public function getDistance(PointInterface $position)
{
return $position->getY();
}
}

View file

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Image\Palette\Color\ColorInterface;
/**
* The font interface
*/
interface FontInterface
{
/**
* Gets the fontfile for current font
*
* @return string
*/
public function getFile();
/**
* Gets font's integer point size
*
* @return integer
*/
public function getSize();
/**
* Gets font's color
*
* @return ColorInterface
*/
public function getColor();
/**
* Gets BoxInterface of font size on the image based on string and angle
*
* @param string $string
* @param integer $angle
*
* @return BoxInterface
*/
public function box($string, $angle = 0);
}

View file

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Histogram;
/**
* Bucket histogram
*/
final class Bucket implements \Countable
{
/**
* @var Range
*/
private $range;
/**
* @var integer
*/
private $count;
/**
* @param Range $range
* @param integer $count
*/
public function __construct(Range $range, $count = 0)
{
$this->range = $range;
$this->count = $count;
}
/**
* @param integer $value
*/
public function add($value)
{
if ($this->range->contains($value)) {
$this->count++;
}
}
/**
* @return integer The number of elements in the bucket.
*/
public function count()
{
return $this->count;
}
}

View file

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Histogram;
use Imagine\Exception\OutOfBoundsException;
/**
* Range histogram
*/
final class Range
{
/**
* @var integer
*/
private $start;
/**
* @var integer
*/
private $end;
/**
* @param integer $start
* @param integer $end
*
* @throws OutOfBoundsException
*/
public function __construct($start, $end)
{
if ($end <= $start) {
throw new OutOfBoundsException(sprintf('Range end cannot be bigger than start, %d %d given accordingly', $this->start, $this->end));
}
$this->start = $start;
$this->end = $end;
}
/**
* @param integer $value
*
* @return Boolean
*/
public function contains($value)
{
return $value >= $this->start && $value < $this->end;
}
}

View file

@ -0,0 +1,173 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Draw\DrawerInterface;
use Imagine\Effects\EffectsInterface;
use Imagine\Image\Palette\PaletteInterface;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Exception\RuntimeException;
use Imagine\Exception\OutOfBoundsException;
/**
* The image interface
*/
interface ImageInterface extends ManipulatorInterface
{
const RESOLUTION_PIXELSPERINCH = 'ppi';
const RESOLUTION_PIXELSPERCENTIMETER = 'ppc';
const INTERLACE_NONE = 'none';
const INTERLACE_LINE = 'line';
const INTERLACE_PLANE = 'plane';
const INTERLACE_PARTITION = 'partition';
const FILTER_UNDEFINED = 'undefined';
const FILTER_POINT = 'point';
const FILTER_BOX = 'box';
const FILTER_TRIANGLE = 'triangle';
const FILTER_HERMITE = 'hermite';
const FILTER_HANNING = 'hanning';
const FILTER_HAMMING = 'hamming';
const FILTER_BLACKMAN = 'blackman';
const FILTER_GAUSSIAN = 'gaussian';
const FILTER_QUADRATIC = 'quadratic';
const FILTER_CUBIC = 'cubic';
const FILTER_CATROM = 'catrom';
const FILTER_MITCHELL = 'mitchell';
const FILTER_LANCZOS = 'lanczos';
const FILTER_BESSEL = 'bessel';
const FILTER_SINC = 'sinc';
/**
* Returns the image content as a binary string
*
* @param string $format
* @param array $options
*
* @throws RuntimeException
*
* @return string binary
*/
public function get($format, array $options = array());
/**
* Returns the image content as a PNG binary string
*
* @throws RuntimeException
*
* @return string binary
*/
public function __toString();
/**
* Instantiates and returns a DrawerInterface instance for image drawing
*
* @return DrawerInterface
*/
public function draw();
/**
* @return EffectsInterface
*/
public function effects();
/**
* Returns current image size
*
* @return BoxInterface
*/
public function getSize();
/**
* Transforms creates a grayscale mask from current image, returns a new
* image, while keeping the existing image unmodified
*
* @return ImageInterface
*/
public function mask();
/**
* Returns array of image colors as Imagine\Image\Palette\Color\ColorInterface instances
*
* @return array
*/
public function histogram();
/**
* Returns color at specified positions of current image
*
* @param PointInterface $point
*
* @throws RuntimeException
*
* @return ColorInterface
*/
public function getColorAt(PointInterface $point);
/**
* Returns the image layers when applicable.
*
* @throws RuntimeException In case the layer can not be returned
* @throws OutOfBoundsException In case the index is not a valid value
*
* @return LayersInterface
*/
public function layers();
/**
* Enables or disables interlacing
*
* @param string $scheme
*
* @throws InvalidArgumentException When an unsupported Interface type is supplied
*
* @return ImageInterface
*/
public function interlace($scheme);
/**
* Return the current color palette
*
* @return PaletteInterface
*/
public function palette();
/**
* Set a palette for the image. Useful to change colorspace.
*
* @param PaletteInterface $palette
*
* @return ImageInterface
*
* @throws RuntimeException
*/
public function usePalette(PaletteInterface $palette);
/**
* Applies a color profile on the Image
*
* @param ProfileInterface $profile
*
* @return ImageInterface
*
* @throws RuntimeException
*/
public function profile(ProfileInterface $profile);
/**
* Returns the Image's meta data
*
* @return Metadata\MetadataInterface
*/
public function metadata();
}

View file

@ -0,0 +1,83 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\RuntimeException;
/**
* The imagine interface
*/
interface ImagineInterface
{
const VERSION = '0.7-dev';
/**
* Creates a new empty image with an optional background color
*
* @param BoxInterface $size
* @param ColorInterface $color
*
* @throws InvalidArgumentException
* @throws RuntimeException
*
* @return ImageInterface
*/
public function create(BoxInterface $size, ColorInterface $color = null);
/**
* Opens an existing image from $path
*
* @param string $path
*
* @throws RuntimeException
*
* @return ImageInterface
*/
public function open($path);
/**
* Loads an image from a binary $string
*
* @param string $string
*
* @throws RuntimeException
*
* @return ImageInterface
*/
public function load($string);
/**
* Loads an image from a resource $resource
*
* @param resource $resource
*
* @throws RuntimeException
*
* @return ImageInterface
*/
public function read($resource);
/**
* Constructs a font with specified $file, $size and $color
*
* The font size is to be specified in points (e.g. 10pt means 10)
*
* @param string $file
* @param integer $size
* @param ColorInterface $color
*
* @return FontInterface
*/
public function font($file, $size, ColorInterface $color);
}

View file

@ -0,0 +1,107 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Exception\RuntimeException;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\OutOfBoundsException;
/**
* The layers interface
*/
interface LayersInterface extends \Iterator, \Countable, \ArrayAccess
{
/**
* Merge layers into the original objects
*
* @throws RuntimeException
*/
public function merge();
/**
* Animates layers
*
* @param string $format The output output format
* @param integer $delay The delay in milliseconds between two frames
* @param integer $loops The number of loops, 0 means infinite
*
* @return LayersInterface
*
* @throws InvalidArgumentException In case an invalid argument is provided
* @throws RuntimeException In case the driver fails to animate
*/
public function animate($format, $delay, $loops);
/**
* Coalesce layers. Each layer in the sequence is the same size as the first and composited with the next layer in
* the sequence.
*/
public function coalesce();
/**
* Adds an image at the end of the layers stack
*
* @param ImageInterface $image
*
* @return LayersInterface
*
* @throws RuntimeException
*/
public function add(ImageInterface $image);
/**
* Set an image at offset
*
* @param integer $offset
* @param ImageInterface $image
*
* @return LayersInterface
*
* @throws RuntimeException
* @throws InvalidArgumentException
* @throws OutOfBoundsException
*/
public function set($offset, ImageInterface $image);
/**
* Removes the image at offset
*
* @param integer $offset
*
* @return LayersInterface
*
* @throws RuntimeException
* @throws InvalidArgumentException
*/
public function remove($offset);
/**
* Returns the image at offset
*
* @param integer $offset
*
* @return ImageInterface
*
* @throws RuntimeException
* @throws InvalidArgumentException
*/
public function get($offset);
/**
* Returns true if a layer at offset is preset
*
* @param integer $offset
*
* @return Boolean
*/
public function has($offset);
}

View file

@ -0,0 +1,181 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\OutOfBoundsException;
use Imagine\Exception\RuntimeException;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\Fill\FillInterface;
/**
* The manipulator interface
*/
interface ManipulatorInterface
{
const THUMBNAIL_INSET = 'inset';
const THUMBNAIL_OUTBOUND = 'outbound';
/**
* Copies current source image into a new ImageInterface instance
*
* @throws RuntimeException
*
* @return static
*/
public function copy();
/**
* Crops a specified box out of the source image (modifies the source image)
* Returns cropped self
*
* @param PointInterface $start
* @param BoxInterface $size
*
* @throws OutOfBoundsException
* @throws RuntimeException
*
* @return static
*/
public function crop(PointInterface $start, BoxInterface $size);
/**
* Resizes current image and returns self
*
* @param BoxInterface $size
* @param string $filter
*
* @throws RuntimeException
*
* @return static
*/
public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDEFINED);
/**
* Rotates an image at the given angle.
* Optional $background can be used to specify the fill color of the empty
* area of rotated image.
*
* @param integer $angle
* @param ColorInterface $background
*
* @throws RuntimeException
*
* @return static
*/
public function rotate($angle, ColorInterface $background = null);
/**
* Pastes an image into a parent image
* Throws exceptions if image exceeds parent image borders or if paste
* operation fails
*
* Returns source image
*
* @param ImageInterface $image
* @param PointInterface $start
*
* @throws InvalidArgumentException
* @throws OutOfBoundsException
* @throws RuntimeException
*
* @return static
*/
public function paste(ImageInterface $image, PointInterface $start);
/**
* Saves the image at a specified path, the target file extension is used
* to determine file format, only jpg, jpeg, gif, png, wbmp and xbm are
* supported
*
* @param string $path
* @param array $options
*
* @throws RuntimeException
*
* @return static
*/
public function save($path = null, array $options = array());
/**
* Outputs the image content
*
* @param string $format
* @param array $options
*
* @throws RuntimeException
*
* @return static
*/
public function show($format, array $options = array());
/**
* Flips current image using horizontal axis
*
* @throws RuntimeException
*
* @return static
*/
public function flipHorizontally();
/**
* Flips current image using vertical axis
*
* @throws RuntimeException
*
* @return static
*/
public function flipVertically();
/**
* Remove all profiles and comments
*
* @throws RuntimeException
*
* @return static
*/
public function strip();
/**
* Generates a thumbnail from a current image
* Returns it as a new image, doesn't modify the current image
*
* @param BoxInterface $size
* @param string $mode
* @param string $filter The filter to use for resizing, one of ImageInterface::FILTER_*
*
* @throws RuntimeException
*
* @return static
*/
public function thumbnail(BoxInterface $size, $mode = self::THUMBNAIL_INSET, $filter = ImageInterface::FILTER_UNDEFINED);
/**
* Applies a given mask to current image's alpha channel
*
* @param ImageInterface $mask
*
* @return static
*/
public function applyMask(ImageInterface $mask);
/**
* Fills image with provided filling, by replacing each pixel's color in
* the current image with corresponding color from FillInterface, and
* returns modified image
*
* @param FillInterface $fill
*
* @return static
*/
public function fill(FillInterface $fill);
}

View file

@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
use Imagine\Exception\InvalidArgumentException;
abstract class AbstractMetadataReader implements MetadataReaderInterface
{
/**
* {@inheritdoc}
*/
public function readFile($file)
{
if (stream_is_local($file)) {
if (!is_file($file)) {
throw new InvalidArgumentException(sprintf('File %s does not exist.', $file));
}
return new MetadataBag(array_merge(array('filepath' => realpath($file), 'uri' => $file), $this->extractFromFile($file)));
}
return new MetadataBag(array_merge(array('uri' => $file), $this->extractFromFile($file)));
}
/**
* {@inheritdoc}
*/
public function readData($data)
{
return new MetadataBag($this->extractFromData($data));
}
/**
* {@inheritdoc}
*/
public function readStream($resource)
{
if (!is_resource($resource)) {
throw new InvalidArgumentException('Invalid resource provided.');
}
return new MetadataBag(array_merge($this->getStreamMetadata($resource), $this->extractFromStream($resource)));
}
/**
* Gets the URI from a stream resource
*
* @param resource $resource
*
* @return string|null The URI f ava
*/
private function getStreamMetadata($resource)
{
$metadata = array();
if (false !== $data = @stream_get_meta_data($resource)) {
$metadata['uri'] = $data['uri'];
if (stream_is_local($resource)) {
$metadata['filepath'] = realpath($data['uri']);
}
}
return $metadata;
}
/**
* Extracts metadata from a file
*
* @param $file
*
* @return array An associative array of metadata
*/
abstract protected function extractFromFile($file);
/**
* Extracts metadata from raw data
*
* @param $data
*
* @return array An associative array of metadata
*/
abstract protected function extractFromData($data);
/**
* Extracts metadata from a stream
*
* @param $resource
*
* @return array An associative array of metadata
*/
abstract protected function extractFromStream($resource);
}

View file

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
/**
* Default metadata reader
*/
class DefaultMetadataReader extends AbstractMetadataReader
{
/**
* {@inheritdoc}
*/
protected function extractFromFile($file)
{
return array();
}
/**
* {@inheritdoc}
*/
protected function extractFromData($data)
{
return array();
}
/**
* {@inheritdoc}
*/
protected function extractFromStream($resource)
{
return array();
}
}

View file

@ -0,0 +1,114 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Exception\NotSupportedException;
/**
* Metadata driven by Exif information
*/
class ExifMetadataReader extends AbstractMetadataReader
{
public function __construct()
{
if (!self::isSupported()) {
throw new NotSupportedException('PHP exif extension is required to use the ExifMetadataReader');
}
}
public static function isSupported()
{
return function_exists('exif_read_data');
}
/**
* {@inheritdoc}
*/
protected function extractFromFile($file)
{
if (false === $data = @file_get_contents($file)) {
throw new InvalidArgumentException(sprintf('File %s is not readable.', $file));
}
return $this->doReadData($data);
}
/**
* {@inheritdoc}
*/
protected function extractFromData($data)
{
return $this->doReadData($data);
}
/**
* {@inheritdoc}
*/
protected function extractFromStream($resource)
{
if (0 < ftell($resource)) {
$metadata = stream_get_meta_data($resource);
if ($metadata['seekable']) {
rewind($resource);
}
}
return $this->doReadData(stream_get_contents($resource));
}
/**
* Extracts metadata from raw data, merges with existing metadata
*
* @param string $data
*
* @return MetadataBag
*/
private function doReadData($data)
{
if (substr($data, 0, 2) === 'II') {
$mime = 'image/tiff';
} else {
$mime = 'image/jpeg';
}
return $this->extract('data://' . $mime . ';base64,' . base64_encode($data));
}
/**
* Performs the exif data extraction given a path or data-URI representation.
*
* @param string $path The path to the file or the data-URI representation.
*
* @return MetadataBag
*/
private function extract($path)
{
if (false === $exifData = @exif_read_data($path, null, true, false)) {
return array();
}
$metadata = array();
$sources = array('EXIF' => 'exif', 'IFD0' => 'ifd0');
foreach ($sources as $name => $prefix) {
if (!isset($exifData[$name])) {
continue;
}
foreach ($exifData[$name] as $prop => $value) {
$metadata[$prefix.'.'.$prop] = $value;
}
}
return $metadata;
}
}

View file

@ -0,0 +1,97 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
/**
* An interface for Image Metadata
*/
class MetadataBag implements \ArrayAccess, \IteratorAggregate, \Countable
{
/** @var array */
private $data;
public function __construct(array $data = array())
{
$this->data = $data;
}
/**
* Returns the metadata key, default value if it does not exist
*
* @param string $key
* @param mixed|null $default
*
* @return mixed
*/
public function get($key, $default = null)
{
return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
}
/**
* {@inheritdoc}
*/
public function count()
{
return count($this->data);
}
/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this->data);
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->data);
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
unset($this->data[$offset]);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Returns metadata as an array
*
* @return array An associative array
*/
public function toArray()
{
return $this->data;
}
}

View file

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Metadata;
use Imagine\Exception\InvalidArgumentException;
interface MetadataReaderInterface
{
/**
* Reads metadata from a file.
*
* @param $file The path to the file where to read metadata.
*
* @throws InvalidArgumentException In case the file does not exist.
*
* @return MetadataBag
*/
public function readFile($file);
/**
* Reads metadata from a binary string.
*
* @param $data The binary string to read.
*
* @return MetadataBag
*/
public function readData($data);
/**
* Reads metadata from a stream.
*
* @param $resource The stream to read.
*
* @throws InvalidArgumentException In case the resource is not valid.
*
* @return MetadataBag
*/
public function readStream($resource);
}

View file

@ -0,0 +1,118 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\Palette\Color\CMYK as CMYKColor;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Exception\RuntimeException;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Image\Profile;
use Imagine\Image\ProfileInterface;
class CMYK implements PaletteInterface
{
private $parser;
private $profile;
private static $colors = array();
public function __construct()
{
$this->parser = new ColorParser();
}
/**
* {@inheritdoc}
*/
public function name()
{
return PaletteInterface::PALETTE_CMYK;
}
/**
* {@inheritdoc}
*/
public function pixelDefinition()
{
return array(
ColorInterface::COLOR_CYAN,
ColorInterface::COLOR_MAGENTA,
ColorInterface::COLOR_YELLOW,
ColorInterface::COLOR_KEYLINE,
);
}
/**
* {@inheritdoc}
*/
public function supportsAlpha()
{
return false;
}
/**
* {@inheritdoc}
*/
public function color($color, $alpha = null)
{
if (null !== $alpha) {
throw new InvalidArgumentException('CMYK palette does not support alpha');
}
$color = $this->parser->parseToCMYK($color);
$index = sprintf('cmyk(%d, %d, %d, %d)', $color[0], $color[1], $color[2], $color[3]);
if (false === array_key_exists($index, self::$colors)) {
self::$colors[$index] = new CMYKColor($this, $color);
}
return self::$colors[$index];
}
/**
* {@inheritdoc}
*/
public function blend(ColorInterface $color1, ColorInterface $color2, $amount)
{
if (!$color1 instanceof CMYKColor || ! $color2 instanceof CMYKColor) {
throw new RuntimeException('CMYK palette can only blend CMYK colors');
}
return $this->color(array(
min(100, $color1->getCyan() + $color2->getCyan() * $amount),
min(100, $color1->getMagenta() + $color2->getMagenta() * $amount),
min(100, $color1->getYellow() + $color2->getYellow() * $amount),
min(100, $color1->getKeyline() + $color2->getKeyline() * $amount),
));
}
/**
* {@inheritdoc}
*/
public function useProfile(ProfileInterface $profile)
{
$this->profile = $profile;
return $this;
}
/**
* {@inheritdoc}
*/
public function profile()
{
if (!$this->profile) {
$this->profile = Profile::fromPath(__DIR__ . '/../../resources/Adobe/CMYK/USWebUncoated.icc');
}
return $this->profile;
}
}

View file

@ -0,0 +1,219 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\CMYK as CMYKPalette;
use Imagine\Exception\RuntimeException;
use Imagine\Exception\InvalidArgumentException;
final class CMYK implements ColorInterface
{
/**
* @var integer
*/
private $c;
/**
* @var integer
*/
private $m;
/**
* @var integer
*/
private $y;
/**
* @var integer
*/
private $k;
/**
*
* @var CMYK
*/
private $palette;
public function __construct(CMYKPalette $palette, array $color)
{
$this->palette = $palette;
$this->setColor($color);
}
/**
* {@inheritdoc}
*/
public function getValue($component)
{
switch ($component) {
case ColorInterface::COLOR_CYAN:
return $this->getCyan();
case ColorInterface::COLOR_MAGENTA:
return $this->getMagenta();
case ColorInterface::COLOR_YELLOW:
return $this->getYellow();
case ColorInterface::COLOR_KEYLINE:
return $this->getKeyline();
default:
throw new InvalidArgumentException(sprintf('Color component %s is not valid', $component));
}
}
/**
* Returns Cyan value of the color
*
* @return integer
*/
public function getCyan()
{
return $this->c;
}
/**
* Returns Magenta value of the color
*
* @return integer
*/
public function getMagenta()
{
return $this->m;
}
/**
* Returns Yellow value of the color
*
* @return integer
*/
public function getYellow()
{
return $this->y;
}
/**
* Returns Key value of the color
*
* @return integer
*/
public function getKeyline()
{
return $this->k;
}
/**
* {@inheritdoc}
*/
public function getPalette()
{
return $this->palette;
}
/**
* {@inheritdoc}
*/
public function getAlpha()
{
return null;
}
/**
* {@inheritdoc}
*/
public function dissolve($alpha)
{
throw new RuntimeException('CMYK does not support dissolution');
}
/**
* {@inheritdoc}
*/
public function lighten($shade)
{
return $this->palette->color(
array(
$this->c,
$this->m,
$this->y,
max(0, $this->k - $shade),
)
);
}
/**
* {@inheritdoc}
*/
public function darken($shade)
{
return $this->palette->color(
array(
$this->c,
$this->m,
$this->y,
min(100, $this->k + $shade),
)
);
}
/**
* {@inheritdoc}
*/
public function grayscale()
{
$color = array(
$this->c * (1 - $this->k / 100) + $this->k,
$this->m * (1 - $this->k / 100) + $this->k,
$this->y * (1 - $this->k / 100) + $this->k,
);
$gray = min(100, round(0.299 * $color[0] + 0.587 * $color[1] + 0.114 * $color[2]));
return $this->palette->color(array($gray, $gray, $gray, $this->k));
}
/**
* {@inheritdoc}
*/
public function isOpaque()
{
return true;
}
/**
* Returns hex representation of the color
*
* @return string
*/
public function __toString()
{
return sprintf('cmyk(%d%%, %d%%, %d%%, %d%%)', $this->c, $this->m, $this->y, $this->k);
}
/**
* Internal, Performs checks for color validity (an of array(C, M, Y, K))
*
* @param array $color
*
* @throws InvalidArgumentException
*/
private function setColor(array $color)
{
if (count($color) !== 4) {
throw new InvalidArgumentException('Color argument must look like array(C, M, Y, K), where C, M, Y, K are the integer values between 0 and 255 for cyan, magenta, yellow and black color indexes accordingly');
}
$colors = array_values($color);
array_walk($colors, function ($color) {
return max(0, min(100, $color));
});
list($this->c, $this->m, $this->y, $this->k) = $colors;
}
}

View file

@ -0,0 +1,95 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\PaletteInterface;
interface ColorInterface
{
const COLOR_RED = 'red';
const COLOR_GREEN = 'green';
const COLOR_BLUE = 'blue';
const COLOR_CYAN = 'cyan';
const COLOR_MAGENTA = 'magenta';
const COLOR_YELLOW = 'yellow';
const COLOR_KEYLINE = 'keyline';
const COLOR_GRAY = 'gray';
/**
* Return the value of one of the component.
*
* @param string $component One of the ColorInterface::COLOR_* component
*
* @return Integer
*/
public function getValue($component);
/**
* Returns percentage of transparency of the color
*
* @return integer
*/
public function getAlpha();
/**
* Returns the palette attached to the current color
*
* @return PaletteInterface
*/
public function getPalette();
/**
* Returns a copy of current color, incrementing the alpha channel by the
* given amount
*
* @param integer $alpha
*
* @return ColorInterface
*/
public function dissolve($alpha);
/**
* Returns a copy of the current color, lightened by the specified number
* of shades
*
* @param integer $shade
*
* @return ColorInterface
*/
public function lighten($shade);
/**
* Returns a copy of the current color, darkened by the specified number of
* shades
*
* @param integer $shade
*
* @return ColorInterface
*/
public function darken($shade);
/**
* Returns a gray related to the current color
*
* @return ColorInterface
*/
public function grayscale();
/**
* Checks if the current color is opaque
*
* @return Boolean
*/
public function isOpaque();
}

View file

@ -0,0 +1,164 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\Grayscale;
use Imagine\Exception\InvalidArgumentException;
final class Gray implements ColorInterface
{
/**
* @var integer
*/
private $gray;
/**
* @var integer
*/
private $alpha;
/**
*
* @var Grayscale
*/
private $palette;
public function __construct(Grayscale $palette, array $color, $alpha)
{
$this->palette = $palette;
$this->setColor($color);
$this->setAlpha($alpha);
}
/**
* {@inheritdoc}
*/
public function getValue($component)
{
switch ($component) {
case ColorInterface::COLOR_GRAY:
return $this->getGray();
default:
throw new InvalidArgumentException(sprintf('Color component %s is not valid', $component));
}
}
/**
* Returns Gray value of the color
*
* @return integer
*/
public function getGray()
{
return $this->gray;
}
/**
* {@inheritdoc}
*/
public function getPalette()
{
return $this->palette;
}
/**
* {@inheritdoc}
*/
public function getAlpha()
{
return $this->alpha;
}
/**
* {@inheritdoc}
*/
public function dissolve($alpha)
{
return $this->palette->color(
array($this->gray), $this->alpha + $alpha
);
}
/**
* {@inheritdoc}
*/
public function lighten($shade)
{
return $this->palette->color(array(min(255, $this->gray + $shade)), $this->alpha);
}
/**
* {@inheritdoc}
*/
public function darken($shade)
{
return $this->palette->color(array(max(0, $this->gray - $shade)), $this->alpha);
}
/**
* {@inheritdoc}
*/
public function grayscale()
{
return $this;
}
/**
* {@inheritdoc}
*/
public function isOpaque()
{
return 100 === $this->alpha;
}
/**
* Returns hex representation of the color
*
* @return string
*/
public function __toString()
{
return sprintf('#%02x%02x%02x', $this->gray, $this->gray, $this->gray);
}
/**
* Performs checks for validity of given alpha value and sets it
*
* @param integer $alpha
*
* @throws InvalidArgumentException
*/
private function setAlpha($alpha)
{
if (!is_int($alpha) || $alpha < 0 || $alpha > 100) {
throw new InvalidArgumentException(sprintf('Alpha must be an integer between 0 and 100, %s given', $alpha));
}
$this->alpha = $alpha;
}
/**
* Performs checks for color validity (array of array(gray))
*
* @param array $color
*
* @throws InvalidArgumentException
*/
private function setColor(array $color)
{
if (count($color) !== 1) {
throw new InvalidArgumentException('Color argument must look like array(gray), where gray is the integer value between 0 and 255 for the grayscale');
}
list($this->gray) = array_values($color);
}
}

View file

@ -0,0 +1,214 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette\Color;
use Imagine\Image\Palette\RGB as RGBPalette;
use Imagine\Exception\InvalidArgumentException;
final class RGB implements ColorInterface
{
/**
* @var integer
*/
private $r;
/**
* @var integer
*/
private $g;
/**
* @var integer
*/
private $b;
/**
* @var integer
*/
private $alpha;
/**
*
* @var RGBPalette
*/
private $palette;
public function __construct(RGBPalette $palette, array $color, $alpha)
{
$this->palette = $palette;
$this->setColor($color);
$this->setAlpha($alpha);
}
/**
* {@inheritdoc}
*/
public function getValue($component)
{
switch ($component) {
case ColorInterface::COLOR_RED:
return $this->getRed();
case ColorInterface::COLOR_GREEN:
return $this->getGreen();
case ColorInterface::COLOR_BLUE:
return $this->getBlue();
default:
throw new InvalidArgumentException(sprintf('Color component %s is not valid', $component));
}
}
/**
* Returns RED value of the color
*
* @return integer
*/
public function getRed()
{
return $this->r;
}
/**
* Returns GREEN value of the color
*
* @return integer
*/
public function getGreen()
{
return $this->g;
}
/**
* Returns BLUE value of the color
*
* @return integer
*/
public function getBlue()
{
return $this->b;
}
/**
* {@inheritdoc}
*/
public function getPalette()
{
return $this->palette;
}
/**
* {@inheritdoc}
*/
public function getAlpha()
{
return $this->alpha;
}
/**
* {@inheritdoc}
*/
public function dissolve($alpha)
{
return $this->palette->color(array($this->r, $this->g, $this->b), $this->alpha + $alpha);
}
/**
* {@inheritdoc}
*/
public function lighten($shade)
{
return $this->palette->color(
array(
min(255, $this->r + $shade),
min(255, $this->g + $shade),
min(255, $this->b + $shade),
), $this->alpha
);
}
/**
* {@inheritdoc}
*/
public function darken($shade)
{
return $this->palette->color(
array(
max(0, $this->r - $shade),
max(0, $this->g - $shade),
max(0, $this->b - $shade),
), $this->alpha
);
}
/**
* {@inheritdoc}
*/
public function grayscale()
{
$gray = min(255, round(0.299 * $this->getRed() + 0.114 * $this->getBlue() + 0.587 * $this->getGreen()));
return $this->palette->color(array($gray, $gray, $gray), $this->alpha);
}
/**
* {@inheritdoc}
*/
public function isOpaque()
{
return 100 === $this->alpha;
}
/**
* Returns hex representation of the color
*
* @return string
*/
public function __toString()
{
return sprintf('#%02x%02x%02x', $this->r, $this->g, $this->b);
}
/**
* Internal
*
* Performs checks for validity of given alpha value and sets it
*
* @param integer $alpha
*
* @throws InvalidArgumentException
*/
private function setAlpha($alpha)
{
if (!is_int($alpha) || $alpha < 0 || $alpha > 100) {
throw new InvalidArgumentException(sprintf('Alpha must be an integer between 0 and 100, %s given', $alpha));
}
$this->alpha = $alpha;
}
/**
* Internal
*
* Performs checks for color validity (array of array(R, G, B))
*
* @param array $color
*
* @throws InvalidArgumentException
*/
private function setColor(array $color)
{
if (count($color) !== 3) {
throw new InvalidArgumentException('Color argument must look like array(R, G, B), where R, G, B are the integer values between 0 and 255 for red, green and blue color indexes accordingly');
}
list($this->r, $this->g, $this->b) = array_values($color);
}
}

View file

@ -0,0 +1,153 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Exception\InvalidArgumentException;
class ColorParser
{
/**
* Parses a color to a RGB tuple
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
public function parseToRGB($color)
{
$color = $this->parse($color);
if (4 === count($color)) {
$color = array(
255 * (1 - $color[0] / 100) * (1 - $color[3] / 100),
255 * (1 - $color[1] / 100) * (1 - $color[3] / 100),
255 * (1 - $color[2] / 100) * (1 - $color[3] / 100),
);
}
return $color;
}
/**
* Parses a color to a CMYK tuple
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
public function parseToCMYK($color)
{
$color = $this->parse($color);
if (3 === count($color)) {
$r = $color[0] / 255;
$g = $color[1] / 255;
$b = $color[2] / 255;
$k = 1 - max($r, $g, $b);
$color = array(
1 === $k ? 0 : round((1 - $r - $k) / (1- $k) * 100),
1 === $k ? 0 : round((1 - $g - $k) / (1- $k) * 100),
1 === $k ? 0 : round((1 - $b - $k) / (1- $k) * 100),
round($k * 100)
);
}
return $color;
}
/**
* Parses a color to a grayscale value
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
public function parseToGrayscale($color)
{
if (is_array($color) && 1 === count($color)) {
return array_values($color);
}
$color = array_unique($this->parse($color));
if (1 !== count($color)) {
throw new InvalidArgumentException('The provided color has different values of red, green and blue components. Grayscale colors must have the same values for these.');
}
return $color;
}
/**
* Parses a color
*
* @param string|array|integer $color
*
* @return array
*
* @throws InvalidArgumentException
*/
private function parse($color)
{
if (!is_string($color) && !is_array($color) && !is_int($color)) {
throw new InvalidArgumentException(sprintf('Color must be specified as a hexadecimal string, array or integer, %s given', gettype($color)));
}
if (is_array($color)) {
if (3 === count($color) || 4 === count($color)) {
return array_values($color);
}
throw new InvalidArgumentException('Color argument if array, must look like array(R, G, B), or array(C, M, Y, K) where R, G, B are the integer values between 0 and 255 for red, green and blue or cyan, magenta, yellow and black color indexes accordingly');
}
if (is_string($color)) {
if (0 === strpos($color, 'cmyk(')) {
$substrColor = substr($color, 5, strlen($color) - 6);
$components = array_map(function ($component) {
return round(trim($component, ' %'));
}, explode(',', $substrColor));
if (count($components) !== 4) {
throw new InvalidArgumentException(sprintf('Unable to parse color %s', $color));
}
return $components;
} else {
$color = ltrim($color, '#');
if (strlen($color) !== 3 && strlen($color) !== 6) {
throw new InvalidArgumentException(sprintf('Color must be a hex value in regular (6 characters) or short (3 characters) notation, "%s" given', $color));
}
if (strlen($color) === 3) {
$color = $color[0] . $color[0] . $color[1] . $color[1] . $color[2] . $color[2];
}
$color = array_map('hexdec', str_split($color, 2));
}
}
if (is_int($color)) {
$color = array(255 & ($color >> 16), 255 & ($color >> 8), 255 & $color);
}
return $color;
}
}

View file

@ -0,0 +1,123 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\Palette\Color\Gray as GrayColor;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\ProfileInterface;
use Imagine\Image\Profile;
use Imagine\Exception\RuntimeException;
class Grayscale implements PaletteInterface
{
/**
* @var ColorParser
*/
private $parser;
/**
* @var ProfileInterface
*/
private $profile;
/**
* @var array
*/
protected static $colors = array();
public function __construct()
{
$this->parser = new ColorParser();
}
/**
* {@inheritdoc}
*/
public function name()
{
return PaletteInterface::PALETTE_GRAYSCALE;
}
/**
* {@inheritdoc}
*/
public function pixelDefinition()
{
return array(ColorInterface::COLOR_GRAY);
}
/**
* {@inheritdoc}
*/
public function supportsAlpha()
{
return true;
}
/**
* {@inheritdoc}
*/
public function useProfile(ProfileInterface $profile)
{
$this->profile = $profile;
return $this;
}
/**
* {@inheritdoc}
*/
public function profile()
{
if (!$this->profile) {
$this->profile = Profile::fromPath(__DIR__ . '/../../resources/colormanagement.org/ISOcoated_v2_grey1c_bas.ICC');
}
return $this->profile;
}
/**
* {@inheritdoc}
*/
public function color($color, $alpha = null)
{
if (null === $alpha) {
$alpha = 0;
}
$color = $this->parser->parseToGrayscale($color);
$index = sprintf('#%02x%02x%02x-%d', $color[0], $color[0], $color[0], $alpha);
if (false === array_key_exists($index, static::$colors)) {
static::$colors[$index] = new GrayColor($this, $color, $alpha);
}
return static::$colors[$index];
}
/**
* {@inheritdoc}
*/
public function blend(ColorInterface $color1, ColorInterface $color2, $amount)
{
if (!$color1 instanceof GrayColor || ! $color2 instanceof GrayColor) {
throw new RuntimeException('Grayscale palette can only blend Grayscale colors');
}
return $this->color(
array(
(int) min(255, min($color1->getGray(), $color2->getGray()) + round(abs($color2->getGray() - $color1->getGray()) * $amount)),
),
(int) min(100, min($color1->getAlpha(), $color2->getAlpha()) + round(abs($color2->getAlpha() - $color1->getAlpha()) * $amount))
);
}
}

View file

@ -0,0 +1,87 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\ProfileInterface;
use Imagine\Image\Palette\Color\ColorInterface;
interface PaletteInterface
{
const PALETTE_GRAYSCALE = 'gray';
const PALETTE_RGB = 'rgb';
const PALETTE_CMYK = 'cmyk';
/**
* Returns a color given some values
*
* @param string|array|integer $color A color
* @param integer|null $alpha Set alpha to null to disable it
*
* @return ColorInterface
*
* @throws InvalidArgumentException In case you pass an alpha value to a
* Palette that does not support alpha
*/
public function color($color, $alpha = null);
/**
* Blend two colors given an amount
*
* @param ColorInterface $color1
* @param ColorInterface $color2
* @param float $amount The amount of color2 in color1
*
* @return ColorInterface
*/
public function blend(ColorInterface $color1, ColorInterface $color2, $amount);
/**
* Attachs an ICC profile to this Palette.
*
* (A default profile is provided by default)
*
* @param ProfileInterface $profile
*
* @return PaletteInterface
*/
public function useProfile(ProfileInterface $profile);
/**
* Returns the ICC profile attached to this Palette.
*
* @return ProfileInterface
*/
public function profile();
/**
* Returns the name of this Palette, one of PaletteInterface::PALETTE_*
* constants
*
* @return String
*/
public function name();
/**
* Returns an array containing ColorInterface::COLOR_* constants that
* define the structure of colors for a pixel.
*
* @return array
*/
public function pixelDefinition();
/**
* Tells if alpha channel is supported in this palette
*
* @return Boolean
*/
public function supportsAlpha();
}

View file

@ -0,0 +1,129 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Palette;
use Imagine\Image\Palette\Color\RGB as RGBColor;
use Imagine\Image\Palette\Color\ColorInterface;
use Imagine\Image\ProfileInterface;
use Imagine\Image\Profile;
use Imagine\Exception\RuntimeException;
class RGB implements PaletteInterface
{
/**
* @var ColorParser
*/
private $parser;
/**
* @var ProfileInterface
*/
private $profile;
/**
* @var array
*/
protected static $colors = array();
public function __construct()
{
$this->parser = new ColorParser();
}
/**
* {@inheritdoc}
*/
public function name()
{
return PaletteInterface::PALETTE_RGB;
}
/**
* {@inheritdoc}
*/
public function pixelDefinition()
{
return array(
ColorInterface::COLOR_RED,
ColorInterface::COLOR_GREEN,
ColorInterface::COLOR_BLUE,
);
}
/**
* {@inheritdoc}
*/
public function supportsAlpha()
{
return true;
}
/**
* {@inheritdoc}
*/
public function useProfile(ProfileInterface $profile)
{
$this->profile = $profile;
return $this;
}
/**
* {@inheritdoc}
*/
public function profile()
{
if (!$this->profile) {
$this->profile = Profile::fromPath(__DIR__ . '/../../resources/color.org/sRGB_IEC61966-2-1_black_scaled.icc');
}
return $this->profile;
}
/**
* {@inheritdoc}
*/
public function color($color, $alpha = null)
{
if (null === $alpha) {
$alpha = 100;
}
$color = $this->parser->parseToRGB($color);
$index = sprintf('#%02x%02x%02x-%d', $color[0], $color[1], $color[2], $alpha);
if (false === array_key_exists($index, static::$colors)) {
static::$colors[$index] = new RGBColor($this, $color, $alpha);
}
return static::$colors[$index];
}
/**
* {@inheritdoc}
*/
public function blend(ColorInterface $color1, ColorInterface $color2, $amount)
{
if (!$color1 instanceof RGBColor || ! $color2 instanceof RGBColor) {
throw new RuntimeException('RGB palette can only blend RGB colors');
}
return $this->color(
array(
(int) min(255, min($color1->getRed(), $color2->getRed()) + round(abs($color2->getRed() - $color1->getRed()) * $amount)),
(int) min(255, min($color1->getGreen(), $color2->getGreen()) + round(abs($color2->getGreen() - $color1->getGreen()) * $amount)),
(int) min(255, min($color1->getBlue(), $color2->getBlue()) + round(abs($color2->getBlue() - $color1->getBlue()) * $amount)),
),
(int) min(100, min($color1->getAlpha(), $color2->getAlpha()) + round(abs($color2->getAlpha() - $color1->getAlpha()) * $amount))
);
}
}

View file

@ -0,0 +1,88 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Exception\InvalidArgumentException;
/**
* The point class
*/
final class Point implements PointInterface
{
/**
* @var integer
*/
private $x;
/**
* @var integer
*/
private $y;
/**
* Constructs a point of coordinates
*
* @param integer $x
* @param integer $y
*
* @throws InvalidArgumentException
*/
public function __construct($x, $y)
{
if ($x < 0 || $y < 0) {
throw new InvalidArgumentException(sprintf('A coordinate cannot be positioned outside of a bounding box (x: %s, y: %s given)', $x, $y));
}
$this->x = $x;
$this->y = $y;
}
/**
* {@inheritdoc}
*/
public function getX()
{
return $this->x;
}
/**
* {@inheritdoc}
*/
public function getY()
{
return $this->y;
}
/**
* {@inheritdoc}
*/
public function in(BoxInterface $box)
{
return $this->x < $box->getWidth() && $this->y < $box->getHeight();
}
/**
* {@inheritdoc}
*/
public function move($amount)
{
return new Point($this->x + $amount, $this->y + $amount);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return sprintf('(%d, %d)', $this->x, $this->y);
}
}

View file

@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image\Point;
use Imagine\Image\BoxInterface;
use Imagine\Image\Point as OriginalPoint;
use Imagine\Image\PointInterface;
/**
* Point center
*/
final class Center implements PointInterface
{
/**
* @var BoxInterface
*/
private $box;
/**
* Constructs coordinate with size instance, it needs to be relative to
*
* @param BoxInterface $box
*/
public function __construct(BoxInterface $box)
{
$this->box = $box;
}
/**
* {@inheritdoc}
*/
public function getX()
{
return ceil($this->box->getWidth() / 2);
}
/**
* {@inheritdoc}
*/
public function getY()
{
return ceil($this->box->getHeight() / 2);
}
/**
* {@inheritdoc}
*/
public function in(BoxInterface $box)
{
return $this->getX() < $box->getWidth() && $this->getY() < $box->getHeight();
}
/**
* {@inheritdoc}
*/
public function move($amount)
{
return new OriginalPoint($this->getX() + $amount, $this->getY() + $amount);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return sprintf('(%d, %d)', $this->getX(), $this->getY());
}
}

View file

@ -0,0 +1,56 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
/**
* The point interface
*/
interface PointInterface
{
/**
* Gets points x coordinate
*
* @return integer
*/
public function getX();
/**
* Gets points y coordinate
*
* @return integer
*/
public function getY();
/**
* Checks if current coordinate is inside a given bo
*
* @param BoxInterface $box
*
* @return Boolean
*/
public function in(BoxInterface $box);
/**
* Returns another point, moved by a given amount from current coordinates
*
* @param integer $amount
* @return ImageInterface
*/
public function move($amount);
/**
* Gets a string representation for the current point
*
* @return string
*/
public function __toString();
}

View file

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
use Imagine\Exception\InvalidArgumentException;
class Profile implements ProfileInterface
{
private $data;
private $name;
public function __construct($name, $data)
{
$this->name = $name;
$this->data = $data;
}
/**
* {@inheritdoc}
*/
public function name()
{
return $this->name;
}
/**
* {@inheritdoc}
*/
public function data()
{
return $this->data;
}
/**
* Creates a profile from a path to a file
*
* @param String $path
*
* @return Profile
*
* @throws InvalidArgumentException In case the provided path is not valid
*/
public static function fromPath($path)
{
if (!file_exists($path) || !is_file($path) || !is_readable($path)) {
throw new InvalidArgumentException(sprintf('Path %s is an invalid profile file or is not readable', $path));
}
return new static(basename($path), file_get_contents($path));
}
}

View file

@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Imagine package.
*
* (c) Bulat Shakirzyanov <mallluhuct@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Imagine\Image;
interface ProfileInterface
{
/**
* Returns the name of the profile
*
* @return String
*/
public function name();
/**
* Returns the profile data
*
* @return String
*/
public function data();
}