1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Catalog
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Product gallery
29: *
30: * @category Mage
31: * @package Mage_Catalog
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Catalog_Block_Product_Gallery extends Mage_Core_Block_Template
35: {
36: protected function _prepareLayout()
37: {
38: if ($headBlock = $this->getLayout()->getBlock('head')) {
39: $headBlock->setTitle($this->getProduct()->getMetaTitle());
40: }
41: return parent::_prepareLayout();
42: }
43: public function getProduct()
44: {
45: return Mage::registry('product');
46: }
47:
48: public function getGalleryCollection()
49: {
50: return $this->getProduct()->getMediaGalleryImages();
51: }
52:
53: public function getCurrentImage()
54: {
55: $imageId = $this->getRequest()->getParam('image');
56: $image = null;
57: if ($imageId) {
58: $image = $this->getGalleryCollection()->getItemById($imageId);
59: }
60:
61: if (!$image) {
62: $image = $this->getGalleryCollection()->getFirstItem();
63: }
64: return $image;
65: }
66:
67: public function getImageUrl()
68: {
69: return $this->getCurrentImage()->getUrl();
70: }
71:
72: public function getImageFile()
73: {
74: return $this->getCurrentImage()->getFile();
75: }
76:
77: /**
78: * Retrieve image width
79: *
80: * @return bool|int
81: */
82: public function getImageWidth()
83: {
84: $file = $this->getCurrentImage()->getPath();
85: if (file_exists($file)) {
86: $size = getimagesize($file);
87: if (isset($size[0])) {
88: if ($size[0] > 600) {
89: return 600;
90: } else {
91: return $size[0];
92: }
93: }
94: }
95:
96: return false;
97: }
98:
99: public function getPreviusImage()
100: {
101: $current = $this->getCurrentImage();
102: if (!$current) {
103: return false;
104: }
105: $previus = false;
106: foreach ($this->getGalleryCollection() as $image) {
107: if ($image->getValueId() == $current->getValueId()) {
108: return $previus;
109: }
110: $previus = $image;
111: }
112: return $previus;
113: }
114:
115: public function getNextImage()
116: {
117: $current = $this->getCurrentImage();
118: if (!$current) {
119: return false;
120: }
121:
122: $next = false;
123: $currentFind = false;
124: foreach ($this->getGalleryCollection() as $image) {
125: if ($currentFind) {
126: return $image;
127: }
128: if ($image->getValueId() == $current->getValueId()) {
129: $currentFind = true;
130: }
131: }
132: return $next;
133: }
134:
135: public function getPreviusImageUrl()
136: {
137: if ($image = $this->getPreviusImage()) {
138: return $this->getUrl('*/*/*', array('_current'=>true, 'image'=>$image->getValueId()));
139: }
140: return false;
141: }
142:
143: public function getNextImageUrl()
144: {
145: if ($image = $this->getNextImage()) {
146: return $this->getUrl('*/*/*', array('_current'=>true, 'image'=>$image->getValueId()));
147: }
148: return false;
149: }
150: }
151: