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
afbcae0c
Commit
afbcae0c
authored
Jan 01, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored Image helper.
parent
2bd594ea
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
40 deletions
+17
-40
BaseImage.php
extensions/yii/imagine/BaseImage.php
+17
-40
No files found.
extensions/yii/imagine/BaseImage.php
View file @
afbcae0c
...
...
@@ -8,7 +8,6 @@
namespace
yii\imagine
;
use
Yii
;
use
Imagine\Exception\InvalidArgumentException
;
use
Imagine\Image\Box
;
use
Imagine\Image\Color
;
use
Imagine\Image\ImageInterface
;
...
...
@@ -66,7 +65,7 @@ class BaseImage
}
/**
* @param Imagine
\Image\Imagine
Interface $imagine the `Imagine` object.
* @param ImagineInterface $imagine the `Imagine` object.
*/
public
static
function
setImagine
(
$imagine
)
{
...
...
@@ -119,29 +118,20 @@ class BaseImage
* @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.
* @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
,
$start
=
[
0
,
0
])
public
static
function
crop
(
$filename
,
$width
,
$height
,
array
$start
=
[
0
,
0
])
{
if
(
is_array
(
$start
))
{
if
(
isset
(
$start
[
0
],
$start
[
1
]))
{
$start
=
new
Point
(
$start
[
0
],
$start
[
1
]);
}
else
{
if
(
!
isset
(
$start
[
0
],
$start
[
1
]))
{
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.'
);
}
->
crop
(
new
Point
(
$start
[
0
],
$start
[
1
]),
new
Box
(
$width
,
$height
));
}
/**
...
...
@@ -188,63 +178,50 @@ class BaseImage
* 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.
* @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
,
$start
=
[
0
,
0
])
public
static
function
watermark
(
$filename
,
$watermarkFilename
,
array
$start
=
[
0
,
0
])
{
if
(
is_array
(
$start
))
{
if
(
isset
(
$start
[
0
],
$start
[
1
]))
{
$start
=
new
Point
(
$start
[
0
],
$start
[
1
]);
}
else
{
if
(
!
isset
(
$start
[
0
],
$start
[
1
]))
{
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.'
);
}
return
$img
->
paste
(
$watermark
,
new
Point
(
$start
[
0
],
$start
[
1
]));
}
/**
* 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 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.
* @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
)
public
static
function
text
(
$filename
,
$text
,
$fontFile
,
array
$start
=
[
0
,
0
],
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.'
);
if
(
!
isset
(
$start
[
0
],
$start
[
1
]))
{
throw
new
InvalidParamException
(
'$start must be an array of two elements.'
);
}
$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
));
$font
=
static
::
getImagine
()
->
font
(
Yii
::
getAlias
(
$font
File
),
$fontSize
,
new
Color
(
$fontColor
));
return
$img
->
draw
()
->
text
(
$text
,
$font
,
new
Point
(
$
fontPosX
,
$fontPosY
),
$fontAngle
);
return
$img
->
draw
()
->
text
(
$text
,
$font
,
new
Point
(
$
start
[
0
],
$start
[
1
]
),
$fontAngle
);
}
/**
...
...
@@ -255,7 +232,7 @@ class BaseImage
* @param integer $alpha the alpha value of the frame.
* @return ImageInterface
*/
public
static
function
frame
(
$filename
,
$margin
=
5
,
$color
=
'000
'
,
$alpha
=
100
)
public
static
function
frame
(
$filename
,
$margin
=
20
,
$color
=
'666
'
,
$alpha
=
100
)
{
$img
=
static
::
getImagine
()
->
open
(
Yii
::
getAlias
(
$filename
));
...
...
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