/
Image.php
152 lines (134 loc) · 4.28 KB
/
Image.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace Trive\AdaptiveResize\Model\Product;
class Image extends \Magento\Catalog\Model\Product\Image
{
const DEFAULT_WIDTH = 240;
const POSITION_TOP = 'top';
const POSITION_TOP_LEFT = 'top-left';
const POSITION_TOP_RIGHT = 'top-right';
const POSITION_BOTTOM = 'bottom';
const POSITION_BOTTOM_LEFT = 'bottom-left';
const POSITION_BOTTOM_RIGHT = 'bottom-right';
const POSITION_CENTER = 'center';
const POSITION_CENTER_LEFT = 'center-left';
const POSITION_CENTER_RIGHT = 'center-right';
/**
* Crop position from top
*
* @var float
*/
protected $_topRate = 0.5;
/**
* Crop position from bootom
*
* @var float
*/
protected $_bottomRate = 0.5;
/**
* Crop position from left
*
* @var float
*/
protected $_leftRate = 1;
/**
* Crop position from right
*
* @var float
*/
protected $_rightRate = 1;
/**
* @return $this
*/
public function adaptiveResize()
{
if(is_null($this->getWidth())){
$this->setWidth(self::DEFAULT_WIDTH);
}
if (is_null($this->getHeight())) {
$this->setHeight($this->getWidth());
}
$processor = $this->getImageProcessor();
$currentRatio = $processor->getOriginalWidth() / $processor->getOriginalHeight();
$targetRatio = $this->getWidth() / $this->getHeight();
if ($targetRatio > $currentRatio) {
$processor->resize($this->getWidth(), null);
} else {
$processor->resize(null, $this->getHeight());
}
$diffWidth = $processor->getOriginalWidth() - $this->getWidth();
$diffHeight = $processor->getOriginalHeight() - $this->getHeight();
$processor->crop(
floor($diffHeight * $this->_topRate),
floor( ($diffWidth / 2) * $this->_leftRate),
ceil( ($diffWidth / 2) * $this->_rightRate),
ceil($diffHeight * $this->_bottomRate)
);
return $this;
}
/**
* Set crop position
*
* @param string $position top, bottom or center
*
* @return \Trive\AdaptiveResize\Model\Product\Image
*/
public function setCropPosition($position)
{
switch ($position) {
case self::POSITION_TOP:
$this->_topRate = 0;
$this->_bottomRate = 1;
$this->_leftRate = 1;
$this->_rightRate = 1;
break;
case self::POSITION_TOP_LEFT:
$this->_topRate = 0;
$this->_bottomRate = 1;
$this->_leftRate = 0;
$this->_rightRate = 2;
break;
case self::POSITION_TOP_RIGHT:
$this->_topRate = 0;
$this->_bottomRate = 1;
$this->_leftRate = 2;
$this->_rightRate = 0;
break;
case self::POSITION_BOTTOM:
$this->_topRate = 1;
$this->_bottomRate = 0;
$this->_leftRate = 1;
$this->_rightRate = 1;
break;
case self::POSITION_BOTTOM_LEFT:
$this->_topRate = 1;
$this->_bottomRate = 0;
$this->_leftRate = 0;
$this->_rightRate = 2;
break;
case self::POSITION_BOTTOM_RIGHT:
$this->_topRate = 1;
$this->_bottomRate = 0;
$this->_leftRate = 2;
$this->_rightRate = 0;
break;
case self::POSITION_CENTER_LEFT:
$this->_topRate = 0.5;
$this->_bottomRate = 0.5;
$this->_leftRate = 0;
$this->_rightRate = 2;
break;
case self::POSITION_CENTER_RIGHT:
$this->_topRate = 0.5;
$this->_bottomRate = 0.5;
$this->_leftRate = 2;
$this->_rightRate = 0;
break;
default:
$this->_topRate = 0.5;
$this->_bottomRate = 0.5;
$this->_leftRate = 1;
$this->_rightRate = 1;
}
return $this;
}
}