Browse files

added LICENSE, doc-blocks, renamed CompositeFilter to Transformation

  • Loading branch information...
1 parent 8ccde9a commit fdf8fa4eeedf3c1ab1761a02a2a8c72787237ed4 @avalanche123 avalanche123 committed Feb 4, 2011
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2004-2011 Bulat Shakirzyanov
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
38 lib/Imagine/Color.php
@@ -33,26 +33,55 @@ public function __construct($color, $alpha = 100)
$this->setAlpha($alpha);
}
+ /**
+ * 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;
}
+ /**
+ * Returns percentage of transparency of the color
+ *
+ * @return integer
+ */
public function getAlpha()
{
return $this->alpha;
}
+ /**
+ * 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) {
@@ -62,6 +91,15 @@ private function setAlpha($alpha)
$this->alpha = $alpha;
}
+ /**
+ * Internal
+ *
+ * Performs checks for color validity (hex or array of array(R, G, B))
+ *
+ * @param string|array $color
+ *
+ * @throws InvalidArgumentException
+ */
private function setColor($color)
{
if (!is_string($color) && !is_array($color)) {
View
4 lib/Imagine/Filter/Basic/Copy.php
@@ -16,6 +16,10 @@
class Copy implements FilterInterface
{
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->copy();
View
13 lib/Imagine/Filter/Basic/Crop.php
@@ -18,6 +18,15 @@ class Crop implements FilterInterface
{
private $x, $y, $width, $height;
+ /**
+ * Constructs a Crop filter with given x, y, coordinates and crop width and
+ * height values
+ *
+ * @param integer $x
+ * @param integer $y
+ * @param integer $width
+ * @param integer $height
+ */
public function __construct($x, $y, $width, $height)
{
$this->x = $x;
@@ -26,6 +35,10 @@ public function __construct($x, $y, $width, $height)
$this->height = $height;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->crop($this->x, $this->y, $this->width, $this->height);
View
4 lib/Imagine/Filter/Basic/FlipHorizontally.php
@@ -16,6 +16,10 @@
class FlipHorizontally implements FilterInterface
{
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->flipHorizontally();
View
4 lib/Imagine/Filter/Basic/FlipVertically.php
@@ -16,6 +16,10 @@
class FlipVertically implements FilterInterface
{
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->flipVertically();
View
12 lib/Imagine/Filter/Basic/Paste.php
@@ -19,13 +19,25 @@ class Paste implements FilterInterface
private $image;
private $x, $y;
+ /**
+ * Constructs a Paste filter with given ImageInterface to paste and x, y
+ * coordinates of target position
+ *
+ * @param ImageInterface $image
+ * @param integer $x
+ * @param integer $y
+ */
public function __construct(ImageInterface $image, $x, $y)
{
$this->image = $image;
$this->x = $x;
$this->y = $y;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->paste($this->image, $this->x, $this->y);
View
10 lib/Imagine/Filter/Basic/Resize.php
@@ -19,12 +19,22 @@ class Resize implements FilterInterface
private $width;
private $height;
+ /**
+ * Constructs Resize filter with given width and height
+ *
+ * @param integer $width
+ * @param integer $height
+ */
public function __construct($width, $height)
{
$this->width = $width;
$this->height = $height;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->resize($this->width, $this->height);
View
10 lib/Imagine/Filter/Basic/Rotate.php
@@ -20,12 +20,22 @@ class Rotate implements FilterInterface
private $angle;
private $background;
+ /**
+ * Constructs Rotate filter with given angle and background color
+ *
+ * @param integer $angle
+ * @param Color $background
+ */
public function __construct($angle, Color $background = null)
{
$this->angle = $angle;
$this->background = $background;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->rotate($this->angle, $this->background);
View
10 lib/Imagine/Filter/Basic/Save.php
@@ -20,12 +20,22 @@ class Save implements FilterInterface
private $path;
private $options;
+ /**
+ * Constructs Save filter with given path and options
+ *
+ * @param string $path
+ * @param array $options
+ */
public function __construct($path, array $options = array())
{
$this->path = $path;
$this->options = $options;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->save($this->path, $this->options);
View
10 lib/Imagine/Filter/Basic/Show.php
@@ -19,12 +19,22 @@ class Show implements FilterInterface
private $format;
private $options;
+ /**
+ * Constructs the Show filter with given format and options
+ *
+ * @param string $format
+ * @param array $options
+ */
public function __construct($format, array $options = array())
{
$this->format = $format;
$this->options = $options;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
public function apply(ImageInterface $image)
{
return $image->show($this->format, $this->options);
View
89 lib/Imagine/Filter/CompositeFilter.php
@@ -1,89 +0,0 @@
-<?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\Filter;
-
-use Imagine\ImageInterface;
-use Imagine\ImageFactoryInterface;
-use Imagine\Filter\Basic\Copy;
-use Imagine\Filter\Basic\Crop;
-use Imagine\Filter\Basic\FlipVertically;
-use Imagine\Filter\Basic\FlipHorizontally;
-use Imagine\Filter\Basic\Paste;
-use Imagine\Filter\Basic\Resize;
-use Imagine\Filter\Basic\Rotate;
-use Imagine\Filter\Basic\Save;
-use Imagine\Filter\Basic\Show;
-
-class CompositeFilter implements FilterInterface, ImageInterface
-{
- private $filters = array();
-
- public function applyFilter(FilterInterface $filter, ImageInterface $image)
- {
- return $filter->apply($image);
- }
-
- public function apply(ImageInterface $image)
- {
- return array_reduce($this->filters, array($this, 'applyFilter'), $image);
- }
-
- public function copy()
- {
- $this->add(new Copy());
- }
-
- public function crop($x, $y, $width, $height)
- {
- $this->add(new Crop($x, $y, $width, $height));
- }
-
- public function flipHorizontally()
- {
- $this->add(new FlipHorizontally());
- }
-
- public function flipVertically()
- {
- $this->add(new FlipVertically());
- }
-
- public function paste(ImageInterface $image, $x, $y)
- {
- $this->add(new Paste($image, $x, $y));
- }
-
- public function resize($width, $height)
- {
- $this->add(new Resize($width, $height));
- }
-
- public function rotate($angle, Color $background = null)
- {
- $this->add(new Rotate($angle, $background));
- }
-
- public function save($path, array $options = array())
- {
- $this->add(new Save($path, $options));
- }
-
- public function show($format, array $options = array())
- {
- $this->add(new Show($format, $options));
- }
-
- public function add(FilterInterface $filter)
- {
- $this->filters[] = $filter;
- }
-}
View
147 lib/Imagine/Filter/Transformation.php
@@ -0,0 +1,147 @@
+<?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\Filter;
+
+use Imagine\ImageInterface;
+use Imagine\ImageFactoryInterface;
+use Imagine\Filter\Basic\Copy;
+use Imagine\Filter\Basic\Crop;
+use Imagine\Filter\Basic\FlipVertically;
+use Imagine\Filter\Basic\FlipHorizontally;
+use Imagine\Filter\Basic\Paste;
+use Imagine\Filter\Basic\Resize;
+use Imagine\Filter\Basic\Rotate;
+use Imagine\Filter\Basic\Save;
+use Imagine\Filter\Basic\Show;
+
+final class Transformation implements FilterInterface, ImageInterface
+{
+ private $filters = array();
+
+ /**
+ * Applies a given FilterInterface onto given ImageInterface and returns
+ * modified ImageInterface
+ *
+ * @param FilterInterface $filter
+ * @param ImageInterface $image
+ *
+ * @return ImageInterface
+ */
+ public function applyFilter(FilterInterface $filter, ImageInterface $image)
+ {
+ return $filter->apply($image);
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
+ public function apply(ImageInterface $image)
+ {
+ return array_reduce($this->filters, array($this, 'applyFilter'), $image);
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::copy()
+ */
+ public function copy()
+ {
+ return $this->add(new Copy());
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::crop()
+ */
+ public function crop($x, $y, $width, $height)
+ {
+ return $this->add(new Crop($x, $y, $width, $height));
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::flipHorizontally()
+ */
+ public function flipHorizontally()
+ {
+ return $this->add(new FlipHorizontally());
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::flipVertically()
+ */
+ public function flipVertically()
+ {
+ return $this->add(new FlipVertically());
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::paste()
+ */
+ public function paste(ImageInterface $image, $x, $y)
+ {
+ return $this->add(new Paste($image, $x, $y));
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::resize()
+ */
+ public function resize($width, $height)
+ {
+ return $this->add(new Resize($width, $height));
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::apply()
+ */
+ public function rotate($angle, Color $background = null)
+ {
+ return $this->add(new Rotate($angle, $background));
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::save()
+ */
+ public function save($path, array $options = array())
+ {
+ return $this->add(new Save($path, $options));
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see Imagine\Filter.FilterInterface::show()
+ */
+ public function show($format, array $options = array())
+ {
+ return $this->add(new Show($format, $options));
+ }
+
+ /**
+ * Registers a given FilterInterface in an internal array of filters for
+ * later application to an instance of ImageInterface
+ *
+ * @param FilterInterface $filter
+ * @return Transformation
+ */
+ public function add(FilterInterface $filter)
+ {
+ $this->filters[] = $filter;
+
+ return $this;
+ }
+}
View
9 lib/Imagine/Gd/BlankImage.php
@@ -15,6 +15,15 @@
final class BlankImage extends Image
{
+ /**
+ * Constructs a blank image in memory for given dimensions
+ * Throws exception if image creation fails
+ *
+ * @param int $width
+ * @param int $height
+ *
+ * @throws RuntimeException
+ */
public function __construct($width, $height)
{
$this->width = $width;
View
8 lib/Imagine/Gd/FileImage.php
@@ -40,6 +40,14 @@
IMAGETYPE_XBM => 'xbm'
);
+ /**
+ * Constructs an image from file
+ * Throws exception if unsupported or non-existent image is given
+ *
+ * @param string $path
+ *
+ * @throws InvalidArgumentException
+ */
public function __construct($path)
{
if (!file_exists($path)) {
View
138 lib/Imagine/Gd/Image.php
@@ -37,21 +37,36 @@ public function __construct($resource)
$this->height = imagesy($resource);
}
+ /**
+ * Makes sure the current image resource is destroyed
+ */
public function __destruct()
{
imagedestroy($this->resource);
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageMetadataInterface::getHeight()
+ */
final public function getHeight()
{
return $this->height;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageMetadataInterface::getWidth()
+ */
final public function getWidth()
{
return $this->width;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::copy()
+ */
final public function copy()
{
$copy = new BlankImage($this->width, $this->height);
@@ -64,6 +79,10 @@ public function __destruct()
return $copy;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::crop()
+ */
final public function crop($x, $y, $width, $height)
{
$dest = imagecreatetruecolor();
@@ -88,6 +107,10 @@ public function __destruct()
return $this;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::paste()
+ */
final public function paste(ImageInterface $image, $x, $y)
{
if (!$image instanceof self) {
@@ -115,6 +138,10 @@ public function __destruct()
return $this;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::resize()
+ */
final public function resize($width, $height)
{
$dest = imagecreatetruecolor($width, $height);
@@ -133,6 +160,10 @@ public function __destruct()
return $this;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::rotate()
+ */
final public function rotate($angle, Color $background = null)
{
$color = $background ? $background : new Color('fff');
@@ -149,6 +180,10 @@ public function __destruct()
return $this;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::save()
+ */
final public function save($path, array $options = array())
{
$info = pathinfo($path);
@@ -160,48 +195,21 @@ public function __destruct()
return $this;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::show()
+ */
final public function show($format, array $options = array())
{
$this->saveOrOutput($format, $options);
return $this;
}
- private function saveOrOutput($format, array $options, $filename = null)
- {
-
- if (!$this->supported($format)) {
- throw new InvalidArgumentException(sprintf('Saving image in "%s" '.
- 'format is not supported, please use one of the following '.
- 'extension: "%s"', $format, implode('", "', $this->supported())
- ));
- }
-
- $save = 'image'.$format;
-
- $args = array($this->resource);
-
- if (null !== $filename) {
- $args[] = $filename;
- }
-
- if (($format === 'jpeg' || $format === 'png') && isset($options['quality'])) {
- $args[] = $options['quality'];
- }
-
- if ($format === 'png' && isset($options['filters'])) {
- $args[] = $options['filters'];
- }
-
- if (($format === 'wbmp' || $format === 'xbm') && isset($options['foreground'])) {
- $args[] = $options['foreground'];
- }
-
- if (!call_user_func_array($save, $args)) {
- throw new RuntimeException('Save operation failed');
- }
- }
-
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::flipHorizontally()
+ */
final public function flipHorizontally()
{
$dest = imagecreatetruecolor($this->width, $this->height);
@@ -220,6 +228,10 @@ private function saveOrOutput($format, array $options, $filename = null)
return $this;
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageInterface::flipVertically()
+ */
final public function flipVertically()
{
$dest = imagecreatetruecolor($this->width, $this->height);
@@ -239,6 +251,62 @@ private function saveOrOutput($format, array $options, $filename = null)
}
+ /**
+ * Internal
+ *
+ * Performs save or show operation using one of GD's image... functions
+ *
+ * @param string $format
+ * @param array $options
+ * @param string $filename
+ *
+ * @throws InvalidArgumentException
+ * @throws RuntimeException
+ */
+ private function saveOrOutput($format, array $options, $filename = null)
+ {
+
+ if (!$this->supported($format)) {
+ throw new InvalidArgumentException(sprintf('Saving image in "%s" '.
+ 'format is not supported, please use one of the following '.
+ 'extension: "%s"', $format, implode('", "', $this->supported())
+ ));
+ }
+
+ $save = 'image'.$format;
+
+ $args = array($this->resource);
+
+ if (null !== $filename) {
+ $args[] = $filename;
+ }
+
+ if (($format === 'jpeg' || $format === 'png') && isset($options['quality'])) {
+ $args[] = $options['quality'];
+ }
+
+ if ($format === 'png' && isset($options['filters'])) {
+ $args[] = $options['filters'];
+ }
+
+ if (($format === 'wbmp' || $format === 'xbm') && isset($options['foreground'])) {
+ $args[] = $options['foreground'];
+ }
+
+ if (!call_user_func_array($save, $args)) {
+ throw new RuntimeException('Save operation failed');
+ }
+ }
+
+ /**
+ * Internal
+ *
+ * Checks whether a given format is supported by GD library
+ *
+ * @param string $format
+ *
+ * @return Boolean
+ */
protected function supported(&$format = null)
{
$formats = array('gif', 'jpeg', 'png', 'wbmp', 'xbm');
View
8 lib/Imagine/Gd/ImageFactory.php
@@ -15,11 +15,19 @@
class ImageFactory implements ImageFactoryInterface
{
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageFactoryInterface::create()
+ */
public function create($width, $height)
{
return new BlankImage($width, $height);
}
+ /**
+ * (non-PHPdoc)
+ * @see Imagine.ImageFactoryInterface::open()
+ */
public function open($path)
{
return new FileImage($path);

0 comments on commit fdf8fa4

Please sign in to comment.