Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Gallery.php
Go to the documentation of this file.
1 <?php
13 
17 
23 {
29  protected $_coreRegistry = null;
30 
36  public function __construct(
37  \Magento\Framework\View\Element\Template\Context $context,
38  \Magento\Framework\Registry $registry,
39  array $data = []
40  ) {
41  $this->_coreRegistry = $registry;
42  parent::__construct($context, $data);
43  }
44 
48  protected function _prepareLayout()
49  {
50  $this->pageConfig->getTitle()->set($this->getProduct()->getMetaTitle());
51  return parent::_prepareLayout();
52  }
53 
57  public function getProduct()
58  {
59  return $this->_coreRegistry->registry('product');
60  }
61 
65  public function getGalleryCollection()
66  {
67  return $this->getProduct()->getMediaGalleryImages();
68  }
69 
73  public function getCurrentImage()
74  {
75  $imageId = $this->getRequest()->getParam('image');
76  $image = null;
77  if ($imageId) {
78  $image = $this->getGalleryCollection()->getItemById($imageId);
79  }
80 
81  if (!$image) {
82  $image = $this->getGalleryCollection()->getFirstItem();
83  }
84  return $image;
85  }
86 
90  public function getImageUrl()
91  {
92  return $this->getCurrentImage()->getUrl();
93  }
94 
98  public function getImageFile()
99  {
100  return $this->getCurrentImage()->getFile();
101  }
102 
108  public function getImageWidth()
109  {
110  $file = $this->getCurrentImage()->getPath();
111 
112  if ($this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->isFile($file)) {
113  $size = getimagesize($file);
114  if (isset($size[0])) {
115  if ($size[0] > 600) {
116  return 600;
117  } else {
118  return $size[0];
119  }
120  }
121  }
122 
123  return false;
124  }
125 
129  public function getPreviousImage()
130  {
131  $current = $this->getCurrentImage();
132  if (!$current) {
133  return false;
134  }
135  $previous = false;
136  foreach ($this->getGalleryCollection() as $image) {
137  if ($image->getValueId() == $current->getValueId()) {
138  return $previous;
139  }
140  $previous = $image;
141  }
142  return $previous;
143  }
144 
148  public function getNextImage()
149  {
150  $current = $this->getCurrentImage();
151  if (!$current) {
152  return false;
153  }
154 
155  $next = false;
156  $currentFind = false;
157  foreach ($this->getGalleryCollection() as $image) {
158  if ($currentFind) {
159  return $image;
160  }
161  if ($image->getValueId() == $current->getValueId()) {
162  $currentFind = true;
163  }
164  }
165  return $next;
166  }
167 
171  public function getPreviousImageUrl()
172  {
173  $image = $this->getPreviousImage();
174  if ($image) {
175  return $this->getUrl('*/*/*', ['_current' => true, 'image' => $image->getValueId()]);
176  }
177  return false;
178  }
179 
183  public function getNextImageUrl()
184  {
185  $image = $this->getNextImage();
186  if ($image) {
187  return $this->getUrl('*/*/*', ['_current' => true, 'image' => $image->getValueId()]);
188  }
189  return false;
190  }
191 }