-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
ParamsBuilder.php
175 lines (155 loc) · 4.88 KB
/
ParamsBuilder.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Catalog\Model\Product\Image;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\ConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Catalog\Model\Product\Image;
/**
* Builds parameters array used to build Image Asset
*/
class ParamsBuilder
{
/**
* @var array
*/
private $defaultBackground = [255, 255, 255];
/**
* @var int|null
*/
private $defaultAngle = null;
/**
* @var bool
*/
private $defaultKeepAspectRatio = true;
/**
* @var bool
*/
private $defaultKeepTransparency = true;
/**
* @var bool
*/
private $defaultConstrainOnly = true;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var ConfigInterface
*/
private $viewConfig;
/**
* @param ScopeConfigInterface $scopeConfig
* @param ConfigInterface $viewConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
ConfigInterface $viewConfig
) {
$this->scopeConfig = $scopeConfig;
$this->viewConfig = $viewConfig;
}
/**
* Build image params
*
* @param array $imageArguments
* @param int $scopeId
* @return array
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function build(array $imageArguments, int $scopeId = null): array
{
$miscParams = [
'image_type' => $imageArguments['type'] ?? null,
'image_height' => $imageArguments['height'] ?? null,
'image_width' => $imageArguments['width'] ?? null,
];
$overwritten = $this->overwriteDefaultValues($imageArguments);
$watermark = isset($miscParams['image_type']) ? $this->getWatermark($miscParams['image_type'], $scopeId) : [];
return array_merge($miscParams, $overwritten, $watermark);
}
/**
* Overwrite default values
*
* @param array $imageArguments
* @return array
*/
private function overwriteDefaultValues(array $imageArguments): array
{
$frame = $imageArguments['frame'] ?? $this->hasDefaultFrame();
$constrain = $imageArguments['constrain'] ?? $this->defaultConstrainOnly;
$aspectRatio = $imageArguments['aspect_ratio'] ?? $this->defaultKeepAspectRatio;
$transparency = $imageArguments['transparency'] ?? $this->defaultKeepTransparency;
$background = $imageArguments['background'] ?? $this->defaultBackground;
$angle = $imageArguments['angle'] ?? $this->defaultAngle;
$quality = (int) $this->scopeConfig->getValue(Image::XML_PATH_JPEG_QUALITY);
return [
'background' => (array) $background,
'angle' => $angle,
'quality' => $quality,
'keep_aspect_ratio' => (bool) $aspectRatio,
'keep_frame' => (bool) $frame,
'keep_transparency' => (bool) $transparency,
'constrain_only' => (bool) $constrain,
];
}
/**
* Get watermark
*
* @param string $type
* @param int $scopeId
* @return array
*/
private function getWatermark(string $type, int $scopeId = null): array
{
$file = $this->scopeConfig->getValue(
"design/watermark/{$type}_image",
ScopeInterface::SCOPE_STORE,
$scopeId
);
if ($file) {
$size = explode(
'x',
$this->scopeConfig->getValue(
"design/watermark/{$type}_size",
ScopeInterface::SCOPE_STORE
)
);
$opacity = $this->scopeConfig->getValue(
"design/watermark/{$type}_imageOpacity",
ScopeInterface::SCOPE_STORE,
$scopeId
);
$position = $this->scopeConfig->getValue(
"design/watermark/{$type}_position",
ScopeInterface::SCOPE_STORE,
$scopeId
);
$width = !empty($size['0']) ? $size['0'] : null;
$height = !empty($size['1']) ? $size['1'] : null;
return [
'watermark_file' => $file,
'watermark_image_opacity' => $opacity,
'watermark_position' => $position,
'watermark_width' => $width,
'watermark_height' => $height
];
}
return [];
}
/**
* Get frame from product_image_white_borders
*
* @return bool
*/
private function hasDefaultFrame(): bool
{
return (bool) $this->viewConfig->getViewConfig(['area' => \Magento\Framework\App\Area::AREA_FRONTEND])
->getVarValue('Magento_Catalog', 'product_image_white_borders');
}
}