Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
3cd7a744
Commit
3cd7a744
authored
Jan 01, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored imagine extension.
parent
df8d79ee
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
316 additions
and
365 deletions
+316
-365
BaseImage.php
extensions/yii/imagine/BaseImage.php
+273
-0
Image.php
extensions/yii/imagine/Image.php
+6
-304
README.md
extensions/yii/imagine/README.md
+9
-34
AbstractImageTest.php
tests/unit/extensions/imagine/AbstractImageTest.php
+22
-20
ImageGdTest.php
tests/unit/extensions/imagine/ImageGdTest.php
+2
-3
ImageGmagickTest.php
tests/unit/extensions/imagine/ImageGmagickTest.php
+2
-2
ImageImagickTest.php
tests/unit/extensions/imagine/ImageImagickTest.php
+2
-2
No files found.
extensions/yii/imagine/BaseImage.php
0 → 100644
View file @
3cd7a744
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\imagine
;
use
Yii
;
use
Imagine\Exception\InvalidArgumentException
;
use
Imagine\Image\Box
;
use
Imagine\Image\Color
;
use
Imagine\Image\ImageInterface
;
use
Imagine\Image\ImagineInterface
;
use
Imagine\Image\ManipulatorInterface
;
use
Imagine\Image\Point
;
use
yii\base\InvalidConfigException
;
use
yii\base\InvalidParamException
;
use
yii\helpers\ArrayHelper
;
/**
* BaseImage provides concrete implementation for [[Image]].
*
* Do not use BaseImage. Use [[Image]] instead.
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
BaseImage
{
/**
* GD2 driver definition for Imagine implementation using the GD library.
*/
const
DRIVER_GD2
=
'gd2'
;
/**
* imagick driver definition.
*/
const
DRIVER_IMAGICK
=
'imagick'
;
/**
* gmagick driver definition.
*/
const
DRIVER_GMAGICK
=
'gmagick'
;
/**
* @var array|string the driver to use. This can be either a single driver name or an array of driver names.
* If the latter, the first available driver will be used.
*/
public
static
$driver
=
[
self
::
DRIVER_GMAGICK
,
self
::
DRIVER_IMAGICK
,
self
::
DRIVER_GD2
];
/**
* @var ImagineInterface instance.
*/
private
static
$_imagine
;
/**
* Returns the `Imagine` object that supports various image manipulations.
* @return ImagineInterface the `Imagine` object
*/
public
static
function
getImagine
()
{
if
(
static
::
$_imagine
===
null
)
{
static
::
$_imagine
=
static
::
createImagine
();
}
return
static
::
$_imagine
;
}
/**
* @param Imagine\Image\ImagineInterface $imagine the `Imagine` object.
*/
public
static
function
setImagine
(
$imagine
)
{
static
::
$_imagine
=
$imagine
;
}
/**
* Creates an `Imagine` object based on the specified [[driver]].
* @return ImagineInterface the new `Imagine` object
* @throws InvalidConfigException if [[driver]] is unknown or the system doesn't support any [[driver]].
*/
protected
static
function
createImagine
()
{
foreach
((
array
)
static
::
$driver
as
$driver
)
{
switch
(
$driver
)
{
case
self
::
DRIVER_GMAGICK
:
if
(
class_exists
(
'Gmagick'
,
false
))
{
return
new
\Imagine\Gmagick\Imagine
();
}
break
;
case
self
::
DRIVER_IMAGICK
:
if
(
!
class_exists
(
'Imagick'
,
false
))
{
return
new
\Imagine\Imagick\Imagine
();
}
break
;
case
self
::
DRIVER_GD2
:
if
(
!
function_exists
(
'gd_info'
))
{
return
new
\Imagine\Gd\Imagine
();
}
break
;
default
:
throw
new
InvalidConfigException
(
"Unknown driver:
$driver
"
);
}
}
throw
new
InvalidConfigException
(
"Your system does not support any of these drivers: "
.
implode
(
','
,
(
array
)
static
::
$driver
));
}
/**
* Crops an image.
*
* For example,
*
* ~~~
* $obj->crop('path\to\image.jpg', 200, 200, [5, 5]);
*
* $point = new \Imagine\Image\Point(5, 5);
* $obj->crop('path\to\image.jpg', 200, 200, $point);
* ~~~
*
* @param string $filename the image file path or path alias.
* @param integer $width the crop width
* @param integer $height the crop height
* @param array|Point $start the starting point. This can be either an array of `x` and `y` coordinates, or
* a `Point` object.
* @return ImageInterface
* @throws InvalidParamException if the `$start` parameter is invalid
*/
public
static
function
crop
(
$filename
,
$width
,
$height
,
$start
=
[
0
,
0
])
{
if
(
is_array
(
$start
))
{
if
(
isset
(
$start
[
0
],
$start
[
1
]))
{
$start
=
new
Point
(
$start
[
0
],
$start
[
1
]);
}
else
{
throw
new
InvalidParamException
(
'$start must be an array of two elements.'
);
}
}
if
(
$start
instanceof
Point
)
{
return
static
::
getImagine
()
->
open
(
Yii
::
getAlias
(
$filename
))
->
copy
()
->
crop
(
$start
,
new
Box
(
$width
,
$height
));
}
else
{
throw
new
InvalidParamException
(
'$start must be either an array or an "Imagine\\Image\\Point" object.'
);
}
}
/**
* Creates a thumbnail image. The function differs from [[\Imagine\Image\ImageInterface::thumbnail()]] function that
* it keeps the aspect ratio of the image.
* @param string $filename the image file path or path alias.
* @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
* @return ImageInterface
*/
public
static
function
thumbnail
(
$filename
,
$width
,
$height
,
$mode
=
ManipulatorInterface
::
THUMBNAIL_OUTBOUND
)
{
$box
=
new
Box
(
$width
,
$height
);
$img
=
static
::
getImagine
()
->
open
(
Yii
::
getAlias
(
$filename
));
if
((
$img
->
getSize
()
->
getWidth
()
<=
$box
->
getWidth
()
&&
$img
->
getSize
()
->
getHeight
()
<=
$box
->
getHeight
())
||
(
!
$box
->
getWidth
()
&&
!
$box
->
getHeight
()))
{
return
$img
->
copy
();
}
$img
=
$img
->
thumbnail
(
$box
,
$mode
);
// create empty image to preserve aspect ratio of thumbnail
$thumb
=
static
::
getImagine
()
->
create
(
$box
);
// calculate points
$size
=
$img
->
getSize
();
$startX
=
0
;
$startY
=
0
;
if
(
$size
->
getWidth
()
<
$width
)
{
$startX
=
ceil
(
$width
-
$size
->
getWidth
())
/
2
;
}
if
(
$size
->
getHeight
()
<
$height
)
{
$startY
=
ceil
(
$height
-
$size
->
getHeight
())
/
2
;
}
$thumb
->
paste
(
$img
,
new
Point
(
$startX
,
$startY
));
return
$thumb
;
}
/**
* 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 array|Point $start the starting point. This can be either an array of `x` and `y` coordinates, or
* a `Point` object.
* @return ImageInterface
* @throws InvalidParamException if `$start` is invalid
*/
public
static
function
watermark
(
$filename
,
$watermarkFilename
,
$start
=
[
0
,
0
])
{
if
(
is_array
(
$start
))
{
if
(
isset
(
$start
[
0
],
$start
[
1
]))
{
$start
=
new
Point
(
$start
[
0
],
$start
[
1
]);
}
else
{
throw
new
InvalidParamException
(
'$start must be an array of two elements.'
);
}
}
if
(
$start
instanceof
Point
)
{
$img
=
static
::
getImagine
()
->
open
(
Yii
::
getAlias
(
$filename
));
$watermark
=
static
::
getImagine
()
->
open
(
Yii
::
getAlias
(
$watermarkFilename
));
return
$img
->
paste
(
$watermark
,
$start
);
}
else
{
throw
new
InvalidParamException
(
'$start must be either an array or an "Imagine\\Image\\Point" object.'
);
}
}
/**
* Draws a text string on an existing image.
* @param string $filename the image file path or path alias.
* @param string $text the text to write to the image
* @param array $fontOptions the font options. The following options may be specified:
*
* - font: The path to the font file to use to style the text. This option is required.
* - color: The font color. Defaults to "fff".
* - size: The font size. Defaults to 12.
* - x: The X position to write the text. Defaults to 5.
* - y: The Y position to write the text. Defaults to 5.
* - angle: The angle to use to write the text. Defaults to 0.
*
* @return ImageInterface
* @throws InvalidParamException if `$fontOptions` is invalid
*/
public
static
function
text
(
$filename
,
$text
,
array
$fontOptions
)
{
$font
=
ArrayHelper
::
getValue
(
$fontOptions
,
'font'
);
if
(
$font
===
null
)
{
throw
new
InvalidParamException
(
'$fontOptions must contain a "font" key specifying which font file to use.'
);
}
$fontSize
=
ArrayHelper
::
getValue
(
$fontOptions
,
'size'
,
12
);
$fontColor
=
ArrayHelper
::
getValue
(
$fontOptions
,
'color'
,
'fff'
);
$fontPosX
=
ArrayHelper
::
getValue
(
$fontOptions
,
'x'
,
5
);
$fontPosY
=
ArrayHelper
::
getValue
(
$fontOptions
,
'y'
,
5
);
$fontAngle
=
ArrayHelper
::
getValue
(
$fontOptions
,
'angle'
,
0
);
$img
=
static
::
getImagine
()
->
open
(
Yii
::
getAlias
(
$filename
));
$font
=
static
::
getImagine
()
->
font
(
Yii
::
getAlias
(
$font
),
$fontSize
,
new
Color
(
$fontColor
));
return
$img
->
draw
()
->
text
(
$text
,
$font
,
new
Point
(
$fontPosX
,
$fontPosY
),
$fontAngle
);
}
/**
* 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 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
=
5
,
$color
=
'000'
,
$alpha
=
100
)
{
$img
=
static
::
getImagine
()
->
open
(
Yii
::
getAlias
(
$filename
));
$size
=
$img
->
getSize
();
$pasteTo
=
new
Point
(
$margin
,
$margin
);
$padColor
=
new
Color
(
$color
,
$alpha
);
$box
=
new
Box
(
$size
->
getWidth
()
+
ceil
(
$margin
*
2
),
$size
->
getHeight
()
+
ceil
(
$margin
*
2
));
$image
=
static
::
getImagine
()
->
create
(
$box
,
$padColor
);
return
$image
->
paste
(
$img
,
$pasteTo
);
}
}
extensions/yii/imagine/Image.php
View file @
3cd7a744
...
...
@@ -7,321 +7,23 @@
namespace
yii\imagine
;
use
Imagine\Exception\InvalidArgumentException
;
use
Imagine\Image\Box
;
use
Imagine\Image\Color
;
use
Imagine\Image\ManipulatorInterface
;
use
Imagine\Image\Point
;
use
yii\base\Component
;
use
yii\base\InvalidConfigException
;
use
yii\helpers\ArrayHelper
;
/**
* Image implements most common image manipulation functions using Imagine library.
*
* To use Image, you should configure it in the application configuration like the following,
*
* ~~~
* 'components' => [
* ...
* 'image' => [
* 'class' => 'yii\imagine\Image',
* 'driver' => \yii\imagine\Image::DRIVER_GD2,
* ],
* ...
* ],
* ~~~
*
* But you can also use it directly,
*
* ~~~
* use yii\imagine\Image;
*
* $img = new Image();
* ~~~
* Image implements most commonly used image manipulation functions using the [Imagine library](http://imagine.readthedocs.org/).
*
* Example of use:
*
* ~~~
* ~~~
php
* // thumb - saved on runtime path
* $imagePath = Yii::$app->getBasePath() . '/web/img/test-image.jpg';
* $runtimePath = Yii::$app->getRuntimePath();
* Yii::$app->image
* ->thumb($imagePath, 120, 120)
* ->save($runtime . '/thumb-test-image.jpg', ['quality' => 50]);
* Image::thumb('@app/web/img/test-image.jpg', 120, 120)
* ->save('@runtime/thumb-test-image.jpg', ['quality' => 50]);
* ~~~
*
*
* @see http://imagine.readthedocs.org/
*
* @author Antonio Ramirez <amigo.cobos@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
Image
extends
Component
class
Image
extends
BaseImage
{
/**
* GD2 driver definition for Imagine implementation using the GD library.
*/
const
DRIVER_GD2
=
'gd2'
;
/**
* imagick driver definition.
*/
const
DRIVER_IMAGICK
=
'imagick'
;
/**
* gmagick driver definition.
*/
const
DRIVER_GMAGICK
=
'gmagick'
;
/**
* @var \Imagine\Image\ImagineInterface instance.
*/
private
$_imagine
;
/**
* @var string the driver to use. These can be:
* - [[DRIVER_GD2]]
* - [[DRIVER_IMAGICK]]
* - [[DRIVER_GMAGICK]]
*/
private
$_driver
=
self
::
DRIVER_GD2
;
/**
* Sets the driver.
* @param $driver
* @throws \yii\base\InvalidConfigException
*/
public
function
setDriver
(
$driver
)
{
if
(
!
is_string
(
$driver
)
||
!
in_array
(
$driver
,
$this
->
getAvailableDrivers
(),
true
))
{
throw
new
InvalidConfigException
(
strtr
(
'"{class}::driver" should be string of these possible options "{drivers}", "{driver}" given.'
,
[
'{class}'
=>
get_class
(
$this
),
'{drivers}'
=>
implode
(
'", "'
,
$this
->
getAvailableDrivers
()),
'{driver}'
=>
$driver
]));
}
$this
->
_driver
=
$driver
;
}
/**
* Returns the driver which is going to be used for \Imagine\Image\ImagineInterface instance creation.
* @return string the driver used.
*/
public
function
getDriver
()
{
return
$this
->
_driver
;
}
/**
* @return array of available drivers.
*/
public
function
getAvailableDrivers
()
{
static
$drivers
;
if
(
$drivers
===
null
)
{
$drivers
=
[
static
::
DRIVER_GD2
,
static
::
DRIVER_GMAGICK
,
static
::
DRIVER_IMAGICK
];
}
return
$drivers
;
}
/**
* @return \Imagine\Image\ImagineInterface instance
*/
public
function
getImagine
()
{
if
(
$this
->
_imagine
===
null
)
{
switch
(
$this
->
_driver
)
{
case
static
::
DRIVER_GD2
:
$this
->
_imagine
=
new
\Imagine\Gd\Imagine
();
break
;
case
static
::
DRIVER_IMAGICK
:
$this
->
_imagine
=
new
\Imagine\Imagick\Imagine
();
break
;
case
static
::
DRIVER_GMAGICK
:
$this
->
_imagine
=
new
\Imagine\Gmagick\Imagine
();
break
;
}
}
return
$this
->
_imagine
;
}
/**
* Crops an image
* @param string $filename the full path to the image file
* @param integer $width the crop width
* @param integer $height the crop height
* @param mixed $point. This argument can be both an array or an \Imagine\Image\Point type class, containing both
* `x` and `y` coordinates. For example:
* ~~~
* // as array
* $obj->crop('path\to\image.jpg', 200, 200, [5, 5]);
* // as \Imagine\Image\Point
* $point = new \Imagine\Image\Point(5, 5);
* $obj->crop('path\to\image.jpg', 200, 200, $point);
* ~~~
* If null, it will crop from 0,0 pixel position
* @return \Imagine\Image\ManipulatorInterface
* @throws \InvalidArgumentException
*/
public
function
crop
(
$filename
,
$width
,
$height
,
$point
=
null
)
{
if
(
is_array
(
$point
))
{
list
(
$x
,
$y
)
=
$point
;
$point
=
new
Point
(
$x
,
$y
);
}
elseif
(
$point
===
null
)
{
$point
=
new
Point
(
0
,
0
);
}
elseif
(
!
$point
instanceof
Point
)
{
throw
new
\InvalidArgumentException
(
strtr
(
'"{class}::crop()" "$point" if not null, should be an "array" or a "{type}" class type, containing both "x" and "y" coordinates.'
,
[
'{class}'
=>
get_class
(
$this
),
'{type}'
=>
'Imagine\\Image\\Point'
]));
}
return
$this
->
getImagine
()
->
open
(
$filename
)
->
copy
()
->
crop
(
$point
,
new
Box
(
$width
,
$height
));
}
/**
* Creates a thumbnail image. The function differs from [[\Imagine\Image\ImageInterface::thumbnail()]] function that
* it keeps the aspect ratio of the image.
* @param string $filename the full path to the image file
* @param integer $width the width to create the thumbnail
* @param integer $height the height in pixels to create the thumbnail
* @param string $mode
* @return \Imagine\Image\ImageInterface|ManipulatorInterface
*/
public
function
thumbnail
(
$filename
,
$width
,
$height
,
$mode
=
ManipulatorInterface
::
THUMBNAIL_OUTBOUND
)
{
$box
=
new
Box
(
$width
,
$height
);
$img
=
$this
->
getImagine
()
->
open
(
$filename
);
if
((
$img
->
getSize
()
->
getWidth
()
<=
$box
->
getWidth
()
&&
$img
->
getSize
()
->
getHeight
()
<=
$box
->
getHeight
())
||
(
!
$box
->
getWidth
()
&&
!
$box
->
getHeight
()))
{
return
$img
->
copy
();
}
$img
=
$img
->
thumbnail
(
$box
,
$mode
);
// create empty image to preserve aspect ratio of thumbnail
$thumb
=
$this
->
getImagine
()
->
create
(
$box
);
// calculate points
$size
=
$img
->
getSize
();
$startX
=
0
;
$startY
=
0
;
if
(
$size
->
getWidth
()
<
$width
)
{
$startX
=
ceil
(
$width
-
$size
->
getWidth
())
/
2
;
}
if
(
$size
->
getHeight
()
<
$height
)
{
$startY
=
ceil
(
$height
-
$size
->
getHeight
())
/
2
;
}
$thumb
->
paste
(
$img
,
new
Point
(
$startX
,
$startY
));
return
$thumb
;
}
/**
* Paste a watermark image onto another.
* Note: If any of `$x` or `$y` parameters are null, bottom right position will be default.
* @param string $filename the full path to the image file to apply the watermark to
* @param string $watermarkFilename the full path to the image file to apply as watermark
* @param mixed $point. This argument can be both an array or an \Imagine\Image\Point type class, containing both
* `x` and `y` coordinates. For example:
* ~~~
* // as array
* $obj->watermark('path\to\image.jpg', 'path\to\watermark.jpg', [5, 5]);
* // as \Imagine\Image\Point
* $point = new \Imagine\Image\Point(5, 5);
* $obj->watermark('path\to\image.jpg', 'path\to\watermark.jpg', $point);
* ~~~
* @return ManipulatorInterface
* @throws \InvalidArgumentException
*/
public
function
watermark
(
$filename
,
$watermarkFilename
,
$point
=
null
)
{
$img
=
$this
->
getImagine
()
->
open
(
$filename
);
$watermark
=
$this
->
getImagine
()
->
open
(
$watermarkFilename
);
$size
=
$img
->
getSize
();
$wSize
=
$watermark
->
getSize
();
// if x or y position was not given, set its bottom right by default
if
(
is_array
(
$point
))
{
list
(
$x
,
$y
)
=
$point
;
$point
=
new
Point
(
$x
,
$y
);
}
elseif
(
$point
===
null
)
{
$x
=
$size
->
getWidth
()
-
$wSize
->
getWidth
();
$y
=
$size
->
getHeight
()
-
$wSize
->
getHeight
();
$point
=
new
Point
(
$x
,
$y
);
}
elseif
(
!
$point
instanceof
Point
)
{
throw
new
\InvalidArgumentException
(
strtr
(
'"{class}::watermark()" "$point" if not null, should be an "array" or a "{type}" class type, containing both "x" and "y" coordinates.'
,
[
'{class}'
=>
get_class
(
$this
),
'{type}'
=>
'Imagine\\Image\\Point'
]));
}
return
$img
->
paste
(
$watermark
,
$point
);
}
/**
* Draws text to an image.
* @param string $filename the full path to the image file
* @param string $text the text to write to the image
* @param array $fontConfig the font configuration. The font configuration holds the following keys:
* - font: The path to the font file to use to style the text. Required parameter.
* - size: The font size. Defaults to 12.
* - posX: The X position to write the text. Defaults to 5.
* - posY: The Y position to write the text. Defaults to 5.
* - angle: The angle to use to write the text. Defaults to 0.
* @return \Imagine\Image\ImageInterface
* @throws \Imagine\Exception\InvalidArgumentException
*/
public
function
text
(
$filename
,
$text
,
array
$fontConfig
)
{
$img
=
$this
->
getImagine
()
->
open
(
$filename
);
$font
=
ArrayHelper
::
getValue
(
$fontConfig
,
'font'
);
if
(
$font
===
null
)
{
throw
new
InvalidArgumentException
(
'"'
.
get_class
(
$this
)
.
'::text()" "$fontConfig" parameter should contain a "font" key with the path to the font file to use.'
);
}
$fontSize
=
ArrayHelper
::
getValue
(
$fontConfig
,
'size'
,
12
);
$fontColor
=
ArrayHelper
::
getValue
(
$fontConfig
,
'color'
,
'fff'
);
$fontPosX
=
ArrayHelper
::
getValue
(
$fontConfig
,
'posX'
,
5
);
$fontPosY
=
ArrayHelper
::
getValue
(
$fontConfig
,
'posY'
,
5
);
$fontAngle
=
ArrayHelper
::
getValue
(
$fontConfig
,
'angle'
,
0
);
$font
=
$this
->
getImagine
()
->
font
(
$font
,
$fontSize
,
new
Color
(
$fontColor
));
$img
->
draw
()
->
text
(
$text
,
$font
,
new
Point
(
$fontPosX
,
$fontPosY
),
$fontAngle
);
return
$img
;
}
/**
* Adds a frame around of the image. Please note that the image will increase `$margin` x 2.
* @param string $filename the full path to the image file
* @param integer $margin the frame size to add around the image
* @param string $color the frame color
* @param integer $alpha
* @return \Imagine\Image\ImageInterface
*/
public
function
frame
(
$filename
,
$margin
,
$color
=
'000'
,
$alpha
=
100
)
{
$img
=
$this
->
getImagine
()
->
open
(
$filename
);
$size
=
$img
->
getSize
();
$pasteTo
=
new
Point
(
$margin
,
$margin
);
$padColor
=
new
Color
(
$color
,
$alpha
);
$box
=
new
Box
(
$size
->
getWidth
()
+
ceil
(
$margin
*
2
),
$size
->
getHeight
()
+
ceil
(
$margin
*
2
));
$image
=
$this
->
getImagine
()
->
create
(
$box
,
$padColor
);
$image
->
paste
(
$img
,
$pasteTo
);
return
$image
;
}
}
extensions/yii/imagine/README.md
View file @
3cd7a744
Image Extension for Yii 2
=========================
=====
=========================
This extension adds most common image functions and also acts as a wrapper to
[
Imagine
](
http://imagine.readthedocs.org/
)
image manipulation library.
...
...
@@ -27,44 +27,19 @@ to the `require` section of your composer.json.
Usage & Documentation
---------------------
This extension is a wrapper to the
[
Imagine
](
http://imagine.readthedocs.org/
)
and also adds the most common
methods
used for Image manipulation
.
This extension is a wrapper to the
[
Imagine
](
http://imagine.readthedocs.org/
)
and also adds the most common
ly used
image manipulation methods
.
To use this extension, you can use it in to ways, whether you configure it on your application file or you use it
directly.
The following example shows how to use this extension:
The following shows how to use it via application configuration file:
```
// configuring on your application configuration file
'components' => [
'image' => [
'class' => 'yii\imagine\Image',
'driver' => \yii\imagine\Image::DRIVER_GD2,
]
...
]
// Once configured you can access to the extension like this:
$img = Yii::$app->image->thumb('path/to/image.jpg', 120, 120);
```
This is how to use it directly:
```
```
php
use
yii\imagine\Image
;
$image = new Image();
$img = $image->thumb('path/to/image.jpg', 120, 120);
```
**About the methods**
Each method returns an instance to
`\Imagine\Image\ManipulatorInterface`
, that means that you can easily make use of the methods included in the
`Imagine`
library:
```
// frame, rotate and save an image
Yii::$app->image->frame('path/to/image.jpg', 5, '666', 0)
Image
::
frame
(
'path/to/image.jpg'
,
5
,
'666'
,
0
)
->
rotate
(
-
8
)
->
save
(
'path/to/destination/image.jpg'
,
[
'quality'
=>
50
]);
```
Note that each
`Image`
method returns an instance of
`\Imagine\Image\ImageInterface`
.
This means you can make use of the methods included in the
`Imagine`
library:
tests/unit/extensions/imagine/AbstractImageTest.php
View file @
3cd7a744
...
...
@@ -2,6 +2,7 @@
namespace
yiiunit\extensions\imagine
;
use
Yii
;
use
yii\imagine\Image
;
use
Imagine\Image\Point
;
use
yiiunit\VendorTestCase
;
...
...
@@ -9,10 +10,6 @@ Yii::setAlias('@yii/imagine', __DIR__ . '/../../../../extensions/yii/imagine');
abstract
class
AbstractImageTest
extends
VendorTestCase
{
/**
* @var yii\imagine\Image
*/
protected
$image
;
protected
$imageFile
;
protected
$watermarkFile
;
protected
$runtimeTextFile
;
...
...
@@ -33,14 +30,15 @@ abstract class AbstractImageTest extends VendorTestCase
@
unlink
(
$this
->
runtimeWatermarkFile
);
}
public
function
testText
()
{
if
(
!
$this
->
isFontTestSupported
())
{
public
function
testText
()
{
if
(
!
$this
->
isFontTestSupported
())
{
$this
->
markTestSkipped
(
'Skipping ImageGdTest Gd not installed'
);
}
$fontFile
=
Yii
::
getAlias
(
'@yiiunit/data/imagine/GothamRnd-Light'
)
.
'.otf'
;
$img
=
$this
->
image
->
text
(
$this
->
imageFile
,
'Yii-2 Image'
,
[
$img
=
Image
::
text
(
$this
->
imageFile
,
'Yii-2 Image'
,
[
'font'
=>
$fontFile
,
'size'
=>
12
,
'color'
=>
'000'
...
...
@@ -53,14 +51,14 @@ abstract class AbstractImageTest extends VendorTestCase
public
function
testCrop
()
{
$point
=
[
20
,
20
];
$img
=
$this
->
image
->
crop
(
$this
->
imageFile
,
100
,
100
,
$point
);
$point
=
[
20
,
20
];
$img
=
Image
::
crop
(
$this
->
imageFile
,
100
,
100
,
$point
);
$this
->
assertEquals
(
100
,
$img
->
getSize
()
->
getWidth
());
$this
->
assertEquals
(
100
,
$img
->
getSize
()
->
getHeight
());
$point
=
new
Point
(
20
,
20
);
$img
=
$this
->
image
->
crop
(
$this
->
imageFile
,
100
,
100
,
$point
);
$img
=
Image
::
crop
(
$this
->
imageFile
,
100
,
100
,
$point
);
$this
->
assertEquals
(
100
,
$img
->
getSize
()
->
getWidth
());
$this
->
assertEquals
(
100
,
$img
->
getSize
()
->
getHeight
());
...
...
@@ -68,7 +66,7 @@ abstract class AbstractImageTest extends VendorTestCase
public
function
testWatermark
()
{
$img
=
$this
->
image
->
watermark
(
$this
->
imageFile
,
$this
->
watermarkFile
);
$img
=
Image
::
watermark
(
$this
->
imageFile
,
$this
->
watermarkFile
);
$img
->
save
(
$this
->
runtimeWatermarkFile
);
$this
->
assertTrue
(
file_exists
(
$this
->
runtimeWatermarkFile
));
}
...
...
@@ -76,9 +74,9 @@ abstract class AbstractImageTest extends VendorTestCase
public
function
testFrame
()
{
$frameSize
=
5
;
$original
=
$this
->
image
->
getImagine
()
->
open
(
$this
->
imageFile
);
$original
=
Image
::
getImagine
()
->
open
(
$this
->
imageFile
);
$originalSize
=
$original
->
getSize
();
$img
=
$this
->
image
->
frame
(
$this
->
imageFile
,
$frameSize
,
'666'
,
0
);
$img
=
Image
::
frame
(
$this
->
imageFile
,
$frameSize
,
'666'
,
0
);
$size
=
$img
->
getSize
();
$this
->
assertEquals
(
$size
->
getWidth
(),
$originalSize
->
getWidth
()
+
(
$frameSize
*
2
));
...
...
@@ -86,7 +84,7 @@ abstract class AbstractImageTest extends VendorTestCase
public
function
testThumbnail
()
{
$img
=
$this
->
image
->
thumbnail
(
$this
->
imageFile
,
120
,
120
);
$img
=
Image
::
thumbnail
(
$this
->
imageFile
,
120
,
120
);
$this
->
assertEquals
(
120
,
$img
->
getSize
()
->
getWidth
());
$this
->
assertEquals
(
120
,
$img
->
getSize
()
->
getHeight
());
...
...
@@ -95,22 +93,26 @@ abstract class AbstractImageTest extends VendorTestCase
/**
* @expectedException \yii\base\InvalidConfigException
*/
public
function
testShouldThrowExceptionOnDriverInvalidArgument
()
{
$this
->
image
->
setDriver
(
'fake-driver'
);
public
function
testShouldThrowExceptionOnDriverInvalidArgument
()
{
Image
::
setImagine
(
null
);
Image
::
$driver
=
'fake-driver'
;
}
/**
* @expectedException \InvalidArgumentException
*/
public
function
testShouldThrowExceptionOnCropInvalidArgument
()
{
$this
->
image
->
crop
(
$this
->
imageFile
,
100
,
100
,
new
\stdClass
());
public
function
testShouldThrowExceptionOnCropInvalidArgument
()
{
Image
::
crop
(
$this
->
imageFile
,
100
,
100
,
new
\stdClass
());
}
/**
* @expectedException \InvalidArgumentException
*/
public
function
testShouldThrowExceptionOnWatermarkInvalidArgument
()
{
$this
->
image
->
watermark
(
$this
->
imageFile
,
$this
->
watermarkFile
,
new
\stdClass
());
public
function
testShouldThrowExceptionOnWatermarkInvalidArgument
()
{
Image
::
watermark
(
$this
->
imageFile
,
$this
->
watermarkFile
,
new
\stdClass
());
}
...
...
tests/unit/extensions/imagine/ImageGdTest.php
View file @
3cd7a744
...
...
@@ -10,14 +10,13 @@ use yii\imagine\Image;
*/
class
ImageGdTest
extends
AbstractImageTest
{
protected
function
setUp
()
{
if
(
!
function_exists
(
'gd_info'
))
{
$this
->
markTestSkipped
(
'Skipping ImageGdTest, Gd not installed'
);
}
else
{
$this
->
image
=
new
Image
(
);
$this
->
image
->
setDriver
(
Image
::
DRIVER_GD2
)
;
Image
::
setImagine
(
null
);
Image
::
$driver
=
Image
::
DRIVER_GD2
;
parent
::
setUp
();
}
}
...
...
tests/unit/extensions/imagine/ImageGmagickTest.php
View file @
3cd7a744
...
...
@@ -16,8 +16,8 @@ class ImageGmagickTest extends AbstractImageTest
if
(
!
class_exists
(
'Gmagick'
))
{
$this
->
markTestSkipped
(
'Skipping ImageGmagickTest, Gmagick is not installed'
);
}
else
{
$this
->
image
=
new
Image
(
);
$this
->
image
->
setDriver
(
Image
::
DRIVER_GMAGICK
)
;
Image
::
setImagine
(
null
);
Image
::
$driver
=
Image
::
DRIVER_GMAGICK
;
parent
::
setUp
();
}
}
...
...
tests/unit/extensions/imagine/ImageImagickTest.php
View file @
3cd7a744
...
...
@@ -16,8 +16,8 @@ class ImageImagickTest extends AbstractImageTest
if
(
!
class_exists
(
'Imagick'
))
{
$this
->
markTestSkipped
(
'Skipping ImageImagickTest, Imagick is not installed'
);
}
else
{
$this
->
image
=
new
Image
(
);
$this
->
image
->
setDriver
(
Image
::
DRIVER_IMAGICK
)
;
Image
::
setImagine
(
null
);
Image
::
$driver
=
Image
::
DRIVER_IMAGICK
;
parent
::
setUp
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment