1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Image.php |
4
|
|
|
* @author Revin Roman http://phptime.ru |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace rmrevin\yii\module\File\component; |
8
|
|
|
|
9
|
|
|
use Imagine; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Image |
13
|
|
|
* @package rmrevin\yii\module\File\component |
14
|
|
|
*/ |
15
|
|
|
class Image extends \yii\imagine\Image |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Resize an image by width. |
20
|
|
|
* |
21
|
|
|
* For example, |
22
|
|
|
* |
23
|
|
|
* ~~~ |
24
|
|
|
* $obj->resizeByWidth('path\to\image.jpg', 200); |
25
|
|
|
* $obj->resizeByWidth('path\to\image.jpg', 150, \Imagine\Image\ImageInterface::FILTER_UNDEFINED); |
26
|
|
|
* ~~~ |
27
|
|
|
* |
28
|
|
|
* @param string $filename the image file path or path alias. |
29
|
|
|
* @param integer $width the resize width |
30
|
|
|
* @param string $filter |
31
|
|
|
* @return \Imagine\Image\ImageInterface |
32
|
|
|
*/ |
33
|
1 |
View Code Duplication |
public static function resizeByWidth($filename, $width, $filter = Imagine\Image\ImageInterface::FILTER_UNDEFINED) |
|
|
|
|
34
|
|
|
{ |
35
|
1 |
|
$img = static::getImagine() |
36
|
1 |
|
->open(\Yii::getAlias($filename)); |
37
|
|
|
|
38
|
1 |
|
$height = $img->getSize()->getHeight() / $img->getSize()->getWidth() * $width; |
39
|
|
|
|
40
|
|
|
return $img |
41
|
1 |
|
->copy() |
42
|
1 |
|
->resize(new Imagine\Image\Box($width, $height), $filter); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Resize an image by height. |
47
|
|
|
* |
48
|
|
|
* For example, |
49
|
|
|
* |
50
|
|
|
* ~~~ |
51
|
|
|
* $obj->resizeByHeight('path\to\image.jpg', 200); |
52
|
|
|
* $obj->resizeByHeight('path\to\image.jpg', 250, \Imagine\Image\ImageInterface::FILTER_UNDEFINED); |
53
|
|
|
* ~~~ |
54
|
|
|
* |
55
|
|
|
* @param string $filename the image file path or path alias. |
56
|
|
|
* @param integer $height the resize height |
57
|
|
|
* @param string $filter |
58
|
|
|
* @return \Imagine\Image\ImageInterface |
59
|
|
|
*/ |
60
|
1 |
View Code Duplication |
public static function resizeByHeight($filename, $height, $filter = Imagine\Image\ImageInterface::FILTER_UNDEFINED) |
|
|
|
|
61
|
|
|
{ |
62
|
1 |
|
$img = static::getImagine() |
63
|
1 |
|
->open(\Yii::getAlias($filename)); |
64
|
|
|
|
65
|
1 |
|
$width = $img->getSize()->getWidth() / $img->getSize()->getHeight() * $height; |
66
|
|
|
|
67
|
|
|
return $img |
68
|
1 |
|
->copy() |
69
|
1 |
|
->resize(new Imagine\Image\Box($width, $height), $filter); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Strict resize an image. |
74
|
|
|
* |
75
|
|
|
* For example, |
76
|
|
|
* |
77
|
|
|
* ~~~ |
78
|
|
|
* $obj->resize('path\to\image.jpg', 200, 200); |
79
|
|
|
* $obj->resize('path\to\image.jpg', 150, 250, \Imagine\Image\ImageInterface::FILTER_UNDEFINED); |
80
|
|
|
* ~~~ |
81
|
|
|
* |
82
|
|
|
* @param string $filename the image file path or path alias. |
83
|
|
|
* @param integer $width the resize width |
84
|
|
|
* @param integer $height the resize height |
85
|
|
|
* @param string $filter |
86
|
|
|
* @return \Imagine\Image\ImageInterface |
87
|
|
|
*/ |
88
|
1 |
|
public static function resize($filename, $width, $height, $filter = Imagine\Image\ImageInterface::FILTER_UNDEFINED) |
89
|
|
|
{ |
90
|
1 |
|
return static::getImagine() |
91
|
1 |
|
->open(\Yii::getAlias($filename)) |
92
|
1 |
|
->copy() |
93
|
1 |
|
->resize(new Imagine\Image\Box($width, $height), $filter); |
94
|
|
|
} |
95
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.