Skip to content

Commit

Permalink
Merge pull request #23 from yiisoft/support-resources-and-imageinterf…
Browse files Browse the repository at this point in the history
…ace-in-methods

Fixes #2, Fixes #11: ImageInterface objects and resources are now supported as image files
  • Loading branch information
samdark committed Nov 3, 2016
2 parents e8c7161 + 5bb87e6 commit 6177f82
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
80 changes: 52 additions & 28 deletions BaseImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Yii;
use Imagine\Image\Box;
use Imagine\Image\BoxInterface;
use Imagine\Image\Color;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\ManipulatorInterface;
Expand Down Expand Up @@ -122,7 +121,32 @@ protected static function createImagine()
throw new InvalidConfigException("Unknown driver: $driver");
}
}
throw new InvalidConfigException("Your system does not support any of these drivers: " . implode(',', (array) static::$driver));
throw new InvalidConfigException('Your system does not support any of these drivers: ' . implode(',', (array) static::$driver));
}

/**
* Takes either file path or ImageInterface. In case of file path, creates an instance of ImageInterface from it.
*
* @param string|resource|ImageInterface $image
* @return ImageInterface
* @throws \yii\base\InvalidParamException
* @since 2.1.0
*/
protected static function ensureImageInterfaceInstance($image)
{
if ($image instanceof ImageInterface) {
return $image;
}

if (is_resource($image)) {
return static::getImagine()->read($image);
}

if (is_string($image)) {
return static::getImagine()->open(Yii::getAlias($image));
}

throw new InvalidParamException('File should be either ImageInterface, resource or a string containing file path.');
}

/**
Expand All @@ -137,36 +161,35 @@ protected static function createImagine()
* $obj->crop('path\to\image.jpg', 200, 200, $point);
* ~~~
*
* @param string $filename the image file path or path alias.
* @param string|resource|ImageInterface $image either ImageInterface, resource or a string containing file path
* @param integer $width the crop width
* @param integer $height the crop height
* @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates.
* @return ImageInterface
* @throws InvalidParamException if the `$start` parameter is invalid
*/
public static function crop($filename, $width, $height, array $start = [0, 0])
public static function crop($image, $width, $height, array $start = [0, 0])
{
if (!isset($start[0], $start[1])) {
throw new InvalidParamException('$start must be an array of two elements.');
}

return static::getImagine()
->open(Yii::getAlias($filename))
return static::ensureImageInterfaceInstance($image)
->copy()
->crop(new Point($start[0], $start[1]), new Box($width, $height));
}

/**
* Rotates an image automatically based on exif information.
* Rotates an image automatically based on EXIF information.
*
* @param \Imagine\Image\ImageInterface $image The imagine instance object to rotate.
* @param string|resource|ImageInterface $image either ImageInterface, resource or a string containing file path
* @param string $color
* @return \Imagine\Image\ImageInterface
* @since 2.1.0
*/
public static function autorotate(ImageInterface $image, $color = '000000')
public static function autorotate($image, $color = '000000')
{
return (new Autorotate($color))->apply($image);
return (new Autorotate($color))->apply(static::ensureImageInterfaceInstance($image));
}

/**
Expand All @@ -187,16 +210,17 @@ public static function autorotate(ImageInterface $image, $color = '000000')
* contained within the thumbnail dimensions. The rest is filled with background that could be configured via
* [[Image::$thumbnailBackgroundColor]] and [[Image::$thumbnailBackgroundAlpha]].
*
* @param string $filename the image file path or path alias.
* @param string|resource|ImageInterface $image either ImageInterface, resource or a string containing file path
* @param integer $width the width in pixels to create the thumbnail
* @param integer $height the height in pixels to create the thumbnail
* @param string $mode mode of resizing original image to use in case both width and height specified
* @return ImageInterface
*/
public static function thumbnail($filename, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND)
public static function thumbnail($image, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND)
{
$img = static::getImagine()->open(Yii::getAlias($filename));
$img = self::ensureImageInterfaceInstance($image);

/** @var BoxInterface $sourceBox */
$sourceBox = $img->getSize();
$thumbnailBox = static::getThumbnailBox($sourceBox, $width, $height);

Expand Down Expand Up @@ -239,28 +263,28 @@ public static function thumbnail($filename, $width, $height, $mode = Manipulator

/**
* Adds a watermark to an existing image.
* @param string $filename the image file path or path alias.
* @param string $watermarkFilename the file path or path alias of the watermark image.
* @param string|resource|ImageInterface $image either ImageInterface, resource or a string containing file path
* @param string|resource|ImageInterface $watermarkImage either ImageInterface, resource or a string containing watermark file path
* @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates.
* @return ImageInterface
* @throws InvalidParamException if `$start` is invalid
*/
public static function watermark($filename, $watermarkFilename, array $start = [0, 0])
public static function watermark($image, $watermarkImage, array $start = [0, 0])
{
if (!isset($start[0], $start[1])) {
throw new InvalidParamException('$start must be an array of two elements.');
}

$img = static::getImagine()->open(Yii::getAlias($filename));
$watermark = static::getImagine()->open(Yii::getAlias($watermarkFilename));
$img = self::ensureImageInterfaceInstance($image);
$watermark = self::ensureImageInterfaceInstance($watermarkImage);
$img->paste($watermark, new Point($start[0], $start[1]));

return $img;
}

/**
* Draws a text string on an existing image.
* @param string $filename the image file path or path alias.
* @param string|resource|ImageInterface $image either ImageInterface, resource or a string containing file path
* @param string $text the text to write to the image
* @param string $fontFile the file path or path alias
* @param array $start the starting position of the text. This must be an array with two elements representing `x` and `y` coordinates.
Expand All @@ -273,7 +297,7 @@ public static function watermark($filename, $watermarkFilename, array $start = [
* @return ImageInterface
* @throws InvalidParamException if `$fontOptions` is invalid
*/
public static function text($filename, $text, $fontFile, array $start = [0, 0], array $fontOptions = [])
public static function text($image, $text, $fontFile, array $start = [0, 0], array $fontOptions = [])
{
if (!isset($start[0], $start[1])) {
throw new InvalidParamException('$start must be an array of two elements.');
Expand All @@ -286,7 +310,7 @@ public static function text($filename, $text, $fontFile, array $start = [0, 0],
$palette = new RGB();
$color = $palette->color($fontColor);

$img = static::getImagine()->open(Yii::getAlias($filename));
$img = self::ensureImageInterfaceInstance($image);
$font = static::getImagine()->font(Yii::getAlias($fontFile), $fontSize, $color);

$img->draw()->text($text, $font, new Point($start[0], $start[1]), $fontAngle);
Expand All @@ -296,30 +320,30 @@ public static function text($filename, $text, $fontFile, array $start = [0, 0],

/**
* Adds a frame around of the image. Please note that the image size will increase by `$margin` x 2.
* @param string $filename the full path to the image file
* @param string|resource|ImageInterface $image either ImageInterface, resource or a string containing file path
* @param integer $margin the frame size to add around the image
* @param string $color the frame color
* @param integer $alpha the alpha value of the frame.
* @return ImageInterface
*/
public static function frame($filename, $margin = 20, $color = '666', $alpha = 100)
public static function frame($image, $margin = 20, $color = '666', $alpha = 100)
{
$img = static::getImagine()->open(Yii::getAlias($filename));
$img = static::getImagine()->open(Yii::getAlias($image));

$size = $img->getSize();

$pasteTo = new Point($margin, $margin);

$palette = new RGB();
$color = $palette->color($color, $alpha);

$box = new Box($size->getWidth() + ceil($margin * 2), $size->getHeight() + ceil($margin * 2));

$image = static::getImagine()->create($box, $color);
$finalImage = static::getImagine()->create($box, $color);

$image->paste($img, $pasteTo);
$finalImage->paste($img, $pasteTo);

return $image;
return $finalImage;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Yii Framework 2 imagine extension Change Log
changed. In addition a new `autorotate` method has been implemented
in order to rotate images based in the EXIF informations provided
inside the image (nadar)

- Enh #2: ImageInterface objects are now supported as image files (samdark)
- Enh #11: Resources are now supported as image files (samdark)

2.0.4 September 4, 2016
-----------------------
Expand Down

0 comments on commit 6177f82

Please sign in to comment.