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_Image extends Mage_Core_Model_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_config;
43:
44: 45: 46: 47: 48:
49: protected $_image;
50:
51: 52: 53: 54: 55:
56: protected $_tmpImage;
57:
58: 59: 60: 61: 62:
63: protected $_params = array();
64:
65:
66: protected function _construct()
67: {
68: $this->_init('media/image');
69: }
70:
71: 72: 73: 74: 75: 76:
77: public function setConfig(Mage_Media_Model_Image_Config_Interface $config)
78: {
79: $this->_config = $config;
80: return $this;
81: }
82:
83: 84: 85: 86: 87:
88: public function getConfig()
89: {
90: return $this->_config;
91: }
92:
93: public function getImage()
94: {
95: if(is_null($this->_image)) {
96: $this->_image = $this->_getResource()->getImage($this);
97: }
98:
99: return $this->_image;
100: }
101:
102: public function getTmpImage()
103: {
104: if(is_null($this->_image)) {
105: $this->_tmpImage = $this->_getResource()->getTmpImage($this);
106: }
107:
108: return $this->_tmpImage;
109: }
110:
111: 112: 113: 114: 115:
116: public function getDimensions()
117: {
118: if(!$this->getData('dimensions')) {
119: $this->setData('dimensions', $this->_getResource()->getDimensions($this));
120: }
121: return $this->getData('dimensions');
122: }
123:
124: 125: 126: 127: 128:
129: public function getDestanationDimensions()
130: {
131: if(!$this->getData('destanation_dimensions')) {
132: $this->setData('destanation_dimensions', clone $this->getDimensions());
133: }
134:
135: return $this->getData('destanation_dimensions');
136: }
137:
138: public function getExtension()
139: {
140: return substr($this->getFileName(), strrpos($this->getFileName(), '.')+1);
141: }
142:
143: public function getFilePath($useParams=false)
144: {
145: if($useParams && sizeof($this->getParams())) {
146: $changes = '.' . $this->getParamsSum();
147: } else {
148: $changes = '';
149: }
150:
151: return $this->getConfig()->getBaseMediaPath() . DS . $this->getName() . $changes . '.'
152: . ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
153: }
154:
155: public function getFileUrl($useParams=false)
156: {
157: if($useParams && sizeof($this->getParams())) {
158: $changes = '.' . $this->getParamsSum();
159: } else {
160: $changes = '';
161: }
162:
163: return $this->getConfig()->getBaseMediaUrl() . '/' . $this->getName() . $changes . '.'
164: . ( ( $useParams && $this->getParam('extension')) ? $this->getParam('extension') : $this->getExtension() );
165: }
166:
167: public function getName()
168: {
169: return substr($this->getFileName(), 0, strrpos($this->getFileName(), '.'));
170: }
171:
172: public function addParam($param, $value=null)
173: {
174: if(is_array($param)) {
175: $this->_params = array_merge($this->_params, $param);
176: } else {
177: $this->_params[$param] = $value;
178: }
179:
180: return $this;
181: }
182:
183: public function setParam($param, $value=null)
184: {
185: if(is_array($param)) {
186: $this->_params = $param;
187: } else {
188: $this->_params[$param] = $value;
189: }
190:
191: return $this;
192: }
193:
194: public function getParam($param)
195: {
196: if(isset($this->_params[$param])) {
197: return $this->_params[$param];
198: }
199:
200: return null;
201: }
202:
203: public function getParams()
204: {
205: return $this->_params;
206: }
207:
208: public function getParamsSum()
209: {
210: return md5(serialize($this->_params));
211: }
212:
213: 214: 215: 216: 217: 218: 219: 220: 221:
222: public function getSpecialLink($file, $size, $extension=null, $watermark=null)
223: {
224: $this->_removeResources();
225: $this->setData(array());
226: $this->setParam(array());
227: $this->setFileName($file);
228:
229: $this->addParam('size', $size);
230: $this->addParam('watermark', $watermark);
231: $this->addParam('extension', $extension);
232:
233: if(!$this->hasSpecialImage()) {
234: if (strpos($size, 'x')!==false) {
235: list($width, $height) = explode('x', $size);
236: } else {
237: $width = $size;
238: $height = $this->getDimensions()->getHeight();
239: }
240:
241: $sizeHRate = $width / $this->getDimensions()->getWidth();
242: $sizeVRate = $height / $this->getDimensions()->getHeight();
243:
244: $rate = min($sizeHRate, $sizeVRate);
245:
246: if ($rate > 1) {
247: $rate = 1;
248: }
249:
250: $this->getDestanationDimensions()
251: ->setWidth($rate*$this->getDimensions()->getWidth())
252: ->setHeight($rate*$this->getDimensions()->getHeight());
253:
254:
255: $this->_getResource()->resize($this);
256: $this->_getResource()->watermark($this);
257: $this->_getResource()->saveAs($this, $extension);
258: $this->_removeResources();
259: }
260:
261: return $this->getFileUrl(true);
262: }
263:
264: public function hasSpecialImage()
265: {
266: return $this->_getResource()->hasSpecialImage($this);
267: }
268:
269: protected function _removeResources()
270: {
271: if ($this->_image) {
272: $this->_getResource()->destroyResource($this->_image);
273: $this->_image = null;
274: }
275:
276: if ($this->_tmpImage) {
277: $this->_getResource()->destroyResource($this->_tmpImage);
278: $this->_tmpImage = null;
279: }
280: }
281:
282: }
283: