Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ParamsBuilder.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
13 
18 {
22  private $defaultQuality = 80;
23 
27  private $defaultBackground = [255, 255, 255];
28 
32  private $defaultAngle = null;
33 
37  private $defaultKeepAspectRatio = true;
38 
42  private $defaultKeepTransparency = true;
43 
47  private $defaultConstrainOnly = true;
48 
52  private $scopeConfig;
53 
57  private $viewConfig;
58 
63  public function __construct(
64  ScopeConfigInterface $scopeConfig,
65  ConfigInterface $viewConfig
66  ) {
67  $this->scopeConfig = $scopeConfig;
68  $this->viewConfig = $viewConfig;
69  }
70 
77  public function build(array $imageArguments): array
78  {
79  $miscParams = [
80  'image_type' => $imageArguments['type'] ?? null,
81  'image_height' => $imageArguments['height'] ?? null,
82  'image_width' => $imageArguments['width'] ?? null,
83  ];
84 
85  $overwritten = $this->overwriteDefaultValues($imageArguments);
86  $watermark = isset($miscParams['image_type']) ? $this->getWatermark($miscParams['image_type']) : [];
87 
88  return array_merge($miscParams, $overwritten, $watermark);
89  }
90 
95  private function overwriteDefaultValues(array $imageArguments): array
96  {
97  $frame = $imageArguments['frame'] ?? $this->hasDefaultFrame();
98  $constrain = $imageArguments['constrain'] ?? $this->defaultConstrainOnly;
99  $aspectRatio = $imageArguments['aspect_ratio'] ?? $this->defaultKeepAspectRatio;
100  $transparency = $imageArguments['transparency'] ?? $this->defaultKeepTransparency;
101  $background = $imageArguments['background'] ?? $this->defaultBackground;
102  $angle = $imageArguments['angle'] ?? $this->defaultAngle;
103 
104  return [
105  'background' => (array) $background,
106  'angle' => $angle,
107  'quality' => $this->defaultQuality,
108  'keep_aspect_ratio' => (bool) $aspectRatio,
109  'keep_frame' => (bool) $frame,
110  'keep_transparency' => (bool) $transparency,
111  'constrain_only' => (bool) $constrain,
112  ];
113  }
114 
119  private function getWatermark(string $type): array
120  {
121  $file = $this->scopeConfig->getValue(
122  "design/watermark/{$type}_image",
123  ScopeInterface::SCOPE_STORE
124  );
125 
126  if ($file) {
127  $size = $this->scopeConfig->getValue(
128  "design/watermark/{$type}_size",
129  ScopeInterface::SCOPE_STORE
130  );
131  $opacity = $this->scopeConfig->getValue(
132  "design/watermark/{$type}_imageOpacity",
133  ScopeInterface::SCOPE_STORE
134  );
135  $position = $this->scopeConfig->getValue(
136  "design/watermark/{$type}_position",
137  ScopeInterface::SCOPE_STORE
138  );
139  $width = !empty($size['width']) ? $size['width'] : null;
140  $height = !empty($size['height']) ? $size['height'] : null;
141 
142  return [
143  'watermark_file' => $file,
144  'watermark_image_opacity' => $opacity,
145  'watermark_position' => $position,
146  'watermark_width' => $width,
147  'watermark_height' => $height
148  ];
149  }
150 
151  return [];
152  }
153 
158  private function hasDefaultFrame(): bool
159  {
160  return (bool) $this->viewConfig->getViewConfig()->getVarValue(
161  'Magento_Catalog',
162  'product_image_white_borders'
163  );
164  }
165 }
__construct(ScopeConfigInterface $scopeConfig, ConfigInterface $viewConfig)
$type
Definition: item.phtml:13