1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_XmlConnect_Model_Catalog_Category_Image extends Mage_Catalog_Model_Product_Image
35: {
36: 37: 38: 39: 40: 41:
42: public function setBaseFile($file)
43: {
44: $this->_isBaseFilePlaceholder = false;
45:
46: if (($file) && (0 !== strpos($file, '/', 0))) {
47: $file = '/' . $file;
48: }
49: $baseDir = Mage::getSingleton('xmlconnect/catalog_category_media_config')->getBaseMediaPath();
50:
51: if ('/no_selection' == $file) {
52: $file = null;
53: }
54: if ($file) {
55: if ((!file_exists($baseDir . $file)) || !$this->_checkMemory($baseDir . $file)) {
56: $file = null;
57: }
58: }
59: if (!$file) {
60:
61: $isConfigPlaceholder = Mage::getStoreConfig(
62: 'catalog/placeholder/' . $this->getDestinationSubdir() . '_placeholder'
63: );
64: $configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
65: if ($isConfigPlaceholder && file_exists($baseDir . $configPlaceholder)) {
66: $file = $configPlaceholder;
67: } else {
68:
69: $skinBaseDir = Mage::getDesign()->getSkinBaseDir();
70: $skinPlaceholder = '/images/xmlconnect/catalog/category/placeholder/' . $this->getDestinationSubdir()
71: . '.jpg';
72:
73: $file = $skinPlaceholder;
74: if (file_exists($skinBaseDir . $file)) {
75: $baseDir = $skinBaseDir;
76: } else {
77: $baseDir = Mage::getDesign()->getSkinBaseDir(array('_theme' => 'default'));
78: if (!file_exists($baseDir . $file)) {
79: $baseDir = Mage::getDesign()
80: ->getSkinBaseDir(array('_theme' => 'default', '_package' => 'base'));
81: }
82: }
83: }
84: $this->_isBaseFilePlaceholder = true;
85: }
86:
87: $baseFile = $baseDir . $file;
88:
89: if ((!$file) || (!file_exists($baseFile))) {
90: Mage::throwException(Mage::helper('xmlconnect')->__('Image file was not found.'));
91: }
92:
93: $this->_baseFile = $baseFile;
94:
95:
96: $path = array(Mage::getSingleton('xmlconnect/catalog_category_media_config')->getBaseMediaPath(), 'cache',
97: Mage::app()->getStore()->getId(), $path[] = $this->getDestinationSubdir()
98: );
99: if ((!empty($this->_width)) || (!empty($this->_height))) {
100: $path[] = "{$this->_width}x{$this->_height}";
101: }
102:
103:
104: $miscParams = array(($this->_keepAspectRatio ? '' : 'non') . 'proportional',
105: ($this->_keepFrame ? '' : 'no') . 'frame', ($this->_keepTransparency ? '' : 'no') . 'transparency',
106: ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly', $this->_rgbToString($this->_backgroundColor),
107: 'angle' . $this->_angle, 'quality' . $this->_quality
108: );
109:
110:
111: if ($this->getWatermarkFile()) {
112: $miscParams[] = $this->getWatermarkFile();
113: $miscParams[] = $this->getWatermarkImageOpacity();
114: $miscParams[] = $this->getWatermarkPosition();
115: $miscParams[] = $this->getWatermarkWidth();
116: $miscParams[] = $this->getWatermarkHeigth();
117: }
118:
119: $path[] = md5(implode('_', $miscParams));
120:
121:
122: $this->_newFile = implode('/', $path) . $file;
123:
124: return $this;
125: }
126:
127: 128: 129: 130: 131: 132:
133: protected function _getWatermarkFilePath()
134: {
135: $filePath = false;
136:
137: if (!$file = $this->getWatermarkFile()) {
138: return $filePath;
139: }
140:
141: $baseDir = Mage::getSingleton('xmlconnect/catalog_category_media_config')->getBaseMediaPath();
142:
143: if (file_exists($baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file)) {
144: $filePath = $baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file;
145: } elseif (file_exists($baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file)) {
146: $filePath = $baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file;
147: } elseif (file_exists($baseDir . '/watermark/default/' . $file)) {
148: $filePath = $baseDir . '/watermark/default/' . $file;
149: } elseif (file_exists($baseDir . '/watermark/' . $file)) {
150: $filePath = $baseDir . '/watermark/' . $file;
151: } else {
152: $baseDir = Mage::getDesign()->getSkinBaseDir();
153: if (file_exists($baseDir . $file)) {
154: $filePath = $baseDir . $file;
155: }
156: }
157:
158: return $filePath;
159: }
160:
161: 162: 163: 164: 165:
166: public function clearCache()
167: {
168: $directory = Mage::getBaseDir('media') . DS . 'catalog' . DS . 'category' . DS . 'cache' . DS;
169: $ioFile = new Varien_Io_File();
170: $ioFile->rmdir($directory, true);
171: }
172:
173: 174: 175: 176: 177: 178:
179: protected function _rgbToString($rgbArray)
180: {
181: $result = array();
182: foreach ($rgbArray as $value) {
183: if (null === $value) {
184: $result[] = 'null';
185: } else {
186: $result[] = sprintf('%02s', dechex($value));
187: }
188: }
189: return implode($result);
190: }
191: }
192: