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:
35: class Mage_Media_Model_File_Image extends Mage_Core_Model_Resource_Abstract
36: {
37: 38: 39:
40: protected function _construct()
41: {
42: return $this;
43: }
44:
45: 46: 47:
48: protected function _getReadAdapter()
49: {
50: return false;
51: }
52:
53: 54: 55:
56: protected function _getWriteAdapter()
57: {
58: return false;
59: }
60:
61: public function load(Mage_Media_Model_Image $object, $file, $field=null)
62: {
63:
64: return $this;
65: }
66:
67: public function save(Mage_Media_Model_Image $object)
68: {
69:
70: return $this;
71: }
72:
73: public function delete(Mage_Media_Model_Image $object)
74: {
75: return $this;
76: }
77:
78: 79: 80: 81: 82: 83:
84: public function getImage(Mage_Media_Model_Image $object)
85: {
86: $resource = false;
87: switch(strtolower($object->getExtension())) {
88: case 'jpg':
89: case 'jpeg':
90: $resource = imagecreatefromjpeg($object->getFilePath());
91: break;
92:
93: case 'gif':
94: $resource = imagecreatefromgif($object->getFilePath());
95: break;
96:
97: case 'png':
98: $resource = imagecreatefrompng($object->getFilePath());
99: break;
100: }
101:
102: if(!$resource) {
103: Mage::throwException(Mage::helper('media')->__('The image does not exist or is invalid.'));
104: }
105:
106:
107: return $resource;
108: }
109:
110: 111: 112: 113: 114: 115:
116: public function getTmpImage(Mage_Media_Model_Image $object)
117: {
118: $resource = imagecreatetruecolor($object->getDestanationDimensions()->getWidth(), $object->getDestanationDimensions()->getHeight());
119: return $resource;
120: }
121:
122: 123: 124: 125: 126: 127:
128: public function resize(Mage_Media_Model_Image $object)
129: {
130: $tmpImage = $object->getTmpImage();
131: $sourceImage = $object->getImage();
132:
133: imagecopyresampled(
134: $tmpImage,
135: $sourceImage,
136: 0, 0, 0, 0,
137: $object->getDestanationDimensions()->getWidth(),
138: $object->getDestanationDimensions()->getHeight(),
139: $object->getDimensions()->getWidth(),
140: $object->getDimensions()->getHeight()
141: );
142:
143: return $this;
144: }
145:
146: 147: 148: 149: 150: 151:
152: public function watermark(Mage_Media_Model_Image $object)
153: {
154: return $this;
155: }
156:
157: 158: 159: 160: 161: 162: 163:
164: public function saveAs(Mage_Media_Model_Image $object, $extension=null)
165: {
166: if(is_null($extension)) {
167: $extension = $object->getExtension();
168: }
169:
170: $result = false;
171: switch (strtolower($extension)) {
172: case 'jpg':
173: case 'jpeg':
174: $result = imagejpeg($object->getTmpImage(), $object->getFilePath(true), 80);
175: break;
176: case 'gif':
177: $result = imagegif($object->getTmpImage(), $object->getFilePath(true));
178: break;
179: case 'png':
180: $result = imagepng($object->getTmpImage(), $object->getFilePath(true));
181: break;
182: }
183:
184: if(!$result) {
185: Mage::throwException(Mage::helper('media')->__('An error occurred while creating the image.'));
186: }
187:
188: return $this;
189: }
190:
191: 192: 193: 194: 195: 196:
197: public function getDimensions(Mage_Media_Model_Image $object)
198: {
199: $info = @getimagesize($object->getFilePath());
200: if(!$info) {
201: Mage::throwException(Mage::helper('media')->__('The image does not exist or is invalid.'));
202: }
203:
204: $info = array('width'=>$info[0], 'height'=>$info[1], 'type'=>$info[2]);
205: return new Varien_Object($info);
206: }
207:
208: 209: 210: 211: 212:
213: public function destroyResource(&$resource)
214: {
215: imagedestroy($resource);
216: return $this;
217: }
218:
219: 220: 221: 222: 223:
224: public function hasSpecialImage(Mage_Media_Model_Image $object)
225: {
226: if(file_exists($object->getFilePath(true))) {
227: return true;
228: }
229:
230: return false;
231: }
232:
233:
234: }
235: