Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Image.php
Go to the documentation of this file.
1 <?php
8 
14 
20 class Image implements LocalInterface
21 {
27  private $sourceContentType;
28 
32  private $filePath;
33 
37  private $contentType = 'image';
38 
42  private $context;
43 
49  private $miscParams;
50 
54  private $mediaConfig;
55 
59  private $encryptor;
60 
70  public function __construct(
71  ConfigInterface $mediaConfig,
72  ContextInterface $context,
73  EncryptorInterface $encryptor,
74  $filePath,
75  array $miscParams
76  ) {
77  if (isset($miscParams['image_type'])) {
78  $this->sourceContentType = $miscParams['image_type'];
79  unset($miscParams['image_type']);
80  } else {
81  $this->sourceContentType = $this->contentType;
82  }
83  $this->mediaConfig = $mediaConfig;
84  $this->context = $context;
85  $this->filePath = $filePath;
86  $this->miscParams = $miscParams;
87  $this->encryptor = $encryptor;
88  }
89 
93  public function getUrl()
94  {
95  return $this->context->getBaseUrl() . DIRECTORY_SEPARATOR . $this->getImageInfo();
96  }
97 
101  public function getContentType()
102  {
103  return $this->contentType;
104  }
105 
109  public function getPath()
110  {
111  return $this->context->getPath() . DIRECTORY_SEPARATOR . $this->getImageInfo();
112  }
113 
117  public function getSourceFile()
118  {
119  return $this->mediaConfig->getBaseMediaPath()
120  . DIRECTORY_SEPARATOR . ltrim($this->getFilePath(), DIRECTORY_SEPARATOR);
121  }
122 
128  public function getSourceContentType()
129  {
130  return $this->sourceContentType;
131  }
132 
136  public function getContent()
137  {
138  return null;
139  }
140 
144  public function getFilePath()
145  {
146  return $this->filePath;
147  }
148 
153  public function getContext()
154  {
155  return $this->context;
156  }
157 
161  public function getModule()
162  {
163  return 'cache';
164  }
165 
171  private function getMiscPath()
172  {
173  return $this->encryptor->hash(
174  implode('_', $this->convertToReadableFormat($this->miscParams)),
176  );
177  }
178 
184  private function getImageInfo()
185  {
186  $path = $this->getModule()
187  . DIRECTORY_SEPARATOR . $this->getMiscPath()
188  . DIRECTORY_SEPARATOR . $this->getFilePath();
189  return preg_replace('|\Q'. DIRECTORY_SEPARATOR . '\E+|', DIRECTORY_SEPARATOR, $path);
190  }
191 
197  private function convertToReadableFormat($miscParams)
198  {
199  $miscParams['image_height'] = 'h:' . ($miscParams['image_height'] ?? 'empty');
200  $miscParams['image_width'] = 'w:' . ($miscParams['image_width'] ?? 'empty');
201  $miscParams['quality'] = 'q:' . ($miscParams['quality'] ?? 'empty');
202  $miscParams['angle'] = 'r:' . ($miscParams['angle'] ?? 'empty');
203  $miscParams['keep_aspect_ratio'] = (isset($miscParams['keep_aspect_ratio']) ? '' : 'non') . 'proportional';
204  $miscParams['keep_frame'] = (isset($miscParams['keep_frame']) ? '' : 'no') . 'frame';
205  $miscParams['keep_transparency'] = (isset($miscParams['keep_transparency']) ? '' : 'no') . 'transparency';
206  $miscParams['constrain_only'] = (isset($miscParams['constrain_only']) ? 'do' : 'not') . 'constrainonly';
207  $miscParams['background'] = isset($miscParams['background'])
208  ? 'rgb' . implode(',', $miscParams['background'])
209  : 'nobackground';
210  return $miscParams;
211  }
212 }
__construct(ConfigInterface $mediaConfig, ContextInterface $context, EncryptorInterface $encryptor, $filePath, array $miscParams)
Definition: Image.php:70
$mediaConfig