Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Images.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
20  private $imageDirectorySubpath;
21 
26  protected $_currentPath;
27 
32  protected $_currentUrl;
33 
39  protected $_storeId;
40 
44  protected $_directory;
45 
51  protected $_backendData;
52 
58  protected $_storeManager;
59 
65  protected $escaper;
66 
76  public function __construct(
77  \Magento\Framework\App\Helper\Context $context,
78  \Magento\Backend\Helper\Data $backendData,
79  \Magento\Framework\Filesystem $filesystem,
81  \Magento\Framework\Escaper $escaper
82  ) {
83  parent::__construct($context);
84  $this->_backendData = $backendData;
85  $this->_storeManager = $storeManager;
86  $this->escaper = $escaper;
87 
88  $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
89  $this->_directory->create($this->getStorageRoot());
90  }
91 
98  public function setStoreId($store)
99  {
100  $this->_storeId = $store;
101  return $this;
102  }
103 
109  public function getStorageRoot()
110  {
111  return $this->_directory->getAbsolutePath($this->getStorageRootSubpath());
112  }
113 
119  public function getStorageRootSubpath()
120  {
121  return '';
122  }
123 
129  public function getBaseUrl()
130  {
131  return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
132  }
133 
139  public function getTreeNodeName()
140  {
141  return 'node';
142  }
143 
150  public function convertPathToId($path)
151  {
152  $path = str_replace($this->getStorageRoot(), '', $path);
153  return $this->idEncode($path);
154  }
155 
163  public function convertIdToPath($id)
164  {
165  if ($id === \Magento\Theme\Helper\Storage::NODE_ROOT) {
166  return $this->getStorageRoot();
167  } else {
168  $path = $this->getStorageRoot() . $this->idDecode($id);
169  if (preg_match('/\.\.(\\\|\/)/', $path)) {
170  throw new \InvalidArgumentException('Path is invalid');
171  }
172 
173  return $path;
174  }
175  }
176 
182  public function isUsingStaticUrlsAllowed()
183  {
184  $checkResult = (object) [];
185  $checkResult->isAllowed = false;
186  $this->_eventManager->dispatch(
187  'cms_wysiwyg_images_static_urls_allowed',
188  ['result' => $checkResult, 'store_id' => $this->_storeId]
189  );
190  return $checkResult->isAllowed;
191  }
192 
200  public function getImageHtmlDeclaration($filename, $renderAsTag = false)
201  {
202  $fileUrl = $this->getCurrentUrl() . $filename;
203  $mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
204  $mediaPath = str_replace($mediaUrl, '', $fileUrl);
205  $directive = sprintf('{{media url="%s"}}', $mediaPath);
206  if ($renderAsTag) {
207  $src = $this->isUsingStaticUrlsAllowed() ? $fileUrl : $this->escaper->escapeHtml($directive);
208  $html = sprintf('<img src="%s" alt="" />', $src);
209  } else {
210  if ($this->isUsingStaticUrlsAllowed()) {
211  $html = $fileUrl;
212  } else {
213  $directive = $this->urlEncoder->encode($directive);
214  $html = $this->_backendData->getUrl(
215  'cms/wysiwyg/directive',
216  [
217  '___directive' => $directive,
218  '_escape_params' => false,
219  ]
220  );
221  }
222  }
223  return $html;
224  }
225 
233  public function getCurrentPath()
234  {
235  if (!$this->_currentPath) {
236  $currentPath = $this->getStorageRoot();
237  $path = $this->_getRequest()->getParam($this->getTreeNodeName());
238  if ($path) {
239  $path = $this->convertIdToPath($path);
240  if ($this->_directory->isDirectory($this->_directory->getRelativePath($path))) {
241  $currentPath = $path;
242  }
243  }
244  try {
245  $currentDir = $this->_directory->getRelativePath($currentPath);
246  if (!$this->_directory->isExist($currentDir)) {
247  $this->_directory->create($currentDir);
248  }
249  } catch (\Magento\Framework\Exception\FileSystemException $e) {
250  $message = __('The directory %1 is not writable by server.', $currentPath);
251  throw new \Magento\Framework\Exception\LocalizedException($message);
252  }
253  $this->_currentPath = $currentPath;
254  }
255  return $this->_currentPath;
256  }
257 
263  public function getCurrentUrl()
264  {
265  if (!$this->_currentUrl) {
266  $path = $this->getCurrentPath();
267  $mediaUrl = $this->_storeManager->getStore(
268  $this->_storeId
269  )->getBaseUrl(
270  \Magento\Framework\UrlInterface::URL_TYPE_MEDIA
271  );
272  $this->_currentUrl = rtrim($mediaUrl . $this->_directory->getRelativePath($path), '/') . '/';
273  }
274  return $this->_currentUrl;
275  }
276 
283  public function idEncode($string)
284  {
285  return strtr(base64_encode($string), '+/=', ':_-');
286  }
287 
294  public function idDecode($string)
295  {
296  $string = strtr($string, ':_-', '+/=');
297  return base64_decode($string);
298  }
299 
307  public function getShortFilename($filename, $maxLength = 20)
308  {
309  if (strlen($filename) <= $maxLength) {
310  return $filename;
311  }
312  return substr($filename, 0, $maxLength) . '...';
313  }
314 
321  public function setImageDirectorySubpath($subpath)
322  {
323  $this->imageDirectorySubpath = $subpath;
324  }
325 }
$mediaPath
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Backend\Helper\Data $backendData, \Magento\Framework\Filesystem $filesystem, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Escaper $escaper)
Definition: Images.php:76
$id
Definition: fieldset.phtml:14
$storeManager
__()
Definition: __.php:13
$message
getImageHtmlDeclaration($filename, $renderAsTag=false)
Definition: Images.php:200
getShortFilename($filename, $maxLength=20)
Definition: Images.php:307
$filesystem