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: class Mage_Catalog_Helper_Image extends Mage_Core_Helper_Abstract
33: {
34: 35: 36: 37: 38:
39: protected $_model;
40:
41: 42: 43: 44: 45:
46: protected $_scheduleResize = false;
47:
48: 49: 50: 51: 52:
53: protected $_scheduleRotate = false;
54:
55: 56: 57: 58: 59:
60: protected $_angle;
61:
62: 63: 64: 65: 66:
67: protected $_watermark;
68:
69: 70: 71: 72: 73:
74: protected $_watermarkPosition;
75:
76: 77: 78: 79: 80:
81: protected $_watermarkSize;
82:
83: 84: 85: 86: 87:
88: protected $_watermarkImageOpacity;
89:
90: 91: 92: 93: 94:
95: protected $_product;
96:
97: 98: 99: 100: 101:
102: protected $_imageFile;
103:
104: 105: 106: 107: 108:
109: protected $_placeholder;
110:
111: 112: 113: 114: 115:
116: protected function _reset()
117: {
118: $this->_model = null;
119: $this->_scheduleResize = false;
120: $this->_scheduleRotate = false;
121: $this->_angle = null;
122: $this->_watermark = null;
123: $this->_watermarkPosition = null;
124: $this->_watermarkSize = null;
125: $this->_watermarkImageOpacity = null;
126: $this->_product = null;
127: $this->_imageFile = null;
128: return $this;
129: }
130:
131: 132: 133: 134: 135: 136: 137: 138:
139: public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
140: {
141: $this->_reset();
142: $this->_setModel(Mage::getModel('catalog/product_image'));
143: $this->_getModel()->setDestinationSubdir($attributeName);
144: $this->setProduct($product);
145:
146: $this->setWatermark(
147: Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_image")
148: );
149: $this->setWatermarkImageOpacity(
150: Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_imageOpacity")
151: );
152: $this->setWatermarkPosition(
153: Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_position")
154: );
155: $this->setWatermarkSize(
156: Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_size")
157: );
158:
159: if ($imageFile) {
160: $this->setImageFile($imageFile);
161: } else {
162:
163: $this->_getModel()->setBaseFile($this->getProduct()->getData($this->_getModel()->getDestinationSubdir()));
164: }
165: return $this;
166: }
167:
168: 169: 170: 171: 172: 173: 174: 175: 176:
177: public function resize($width, $height = null)
178: {
179: $this->_getModel()->setWidth($width)->setHeight($height);
180: $this->_scheduleResize = true;
181: return $this;
182: }
183:
184: 185: 186: 187: 188: 189:
190: public function setQuality($quality)
191: {
192: $this->_getModel()->setQuality($quality);
193: return $this;
194: }
195:
196: 197: 198: 199: 200: 201: 202: 203: 204:
205: public function keepAspectRatio($flag)
206: {
207: $this->_getModel()->setKeepAspectRatio($flag);
208: return $this;
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222:
223: public function keepFrame($flag, $position = array('center', 'middle'))
224: {
225: $this->_getModel()->setKeepFrame($flag);
226: return $this;
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240:
241: public function keepTransparency($flag, $alphaOpacity = null)
242: {
243: $this->_getModel()->setKeepTransparency($flag);
244: return $this;
245: }
246:
247: 248: 249: 250: 251: 252: 253: 254:
255: public function constrainOnly($flag)
256: {
257: $this->_getModel()->setConstrainOnly($flag);
258: return $this;
259: }
260:
261: 262: 263: 264: 265: 266: 267: 268: 269: 270:
271: public function backgroundColor($colorRGB)
272: {
273:
274: if (!is_array($colorRGB)) {
275: $colorRGB = func_get_args();
276: }
277: $this->_getModel()->setBackgroundColor($colorRGB);
278: return $this;
279: }
280:
281: 282: 283: 284: 285: 286:
287: public function rotate($angle)
288: {
289: $this->setAngle($angle);
290: $this->_getModel()->setAngle($angle);
291: $this->_scheduleRotate = true;
292: return $this;
293: }
294:
295: 296: 297: 298: 299: 300: 301: 302: 303: 304:
305: public function watermark($fileName, $position, $size=null, $imageOpacity=null)
306: {
307: $this->setWatermark($fileName)
308: ->setWatermarkPosition($position)
309: ->setWatermarkSize($size)
310: ->setWatermarkImageOpacity($imageOpacity);
311: return $this;
312: }
313:
314: 315: 316: 317: 318: 319:
320: public function placeholder($fileName)
321: {
322: $this->_placeholder = $fileName;
323: }
324:
325: 326: 327: 328: 329:
330: public function getPlaceholder()
331: {
332: if (!$this->_placeholder) {
333: $attr = $this->_getModel()->getDestinationSubdir();
334: $this->_placeholder = 'images/catalog/product/placeholder/'.$attr.'.jpg';
335: }
336: return $this->_placeholder;
337: }
338:
339: 340: 341: 342: 343:
344: public function __toString()
345: {
346: try {
347: $model = $this->_getModel();
348:
349: if ($this->getImageFile()) {
350: $model->setBaseFile($this->getImageFile());
351: } else {
352: $model->setBaseFile($this->getProduct()->getData($model->getDestinationSubdir()));
353: }
354:
355: if ($model->isCached()) {
356: return $model->getUrl();
357: } else {
358: if ($this->_scheduleRotate) {
359: $model->rotate($this->getAngle());
360: }
361:
362: if ($this->_scheduleResize) {
363: $model->resize();
364: }
365:
366: if ($this->getWatermark()) {
367: $model->setWatermark($this->getWatermark());
368: }
369:
370: $url = $model->saveFile()->getUrl();
371: }
372: } catch (Exception $e) {
373: $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
374: }
375: return $url;
376: }
377:
378: 379: 380: 381: 382: 383:
384: protected function _setModel($model)
385: {
386: $this->_model = $model;
387: return $this;
388: }
389:
390: 391: 392: 393: 394:
395: protected function _getModel()
396: {
397: return $this->_model;
398: }
399:
400: 401: 402: 403: 404: 405:
406: protected function setAngle($angle)
407: {
408: $this->_angle = $angle;
409: return $this;
410: }
411:
412: 413: 414: 415: 416:
417: protected function getAngle()
418: {
419: return $this->_angle;
420: }
421:
422: 423: 424: 425: 426: 427:
428: protected function setWatermark($watermark)
429: {
430: $this->_watermark = $watermark;
431: $this->_getModel()->setWatermarkFile($watermark);
432: return $this;
433: }
434:
435: 436: 437: 438: 439:
440: protected function getWatermark()
441: {
442: return $this->_watermark;
443: }
444:
445: 446: 447: 448: 449: 450:
451: protected function setWatermarkPosition($position)
452: {
453: $this->_watermarkPosition = $position;
454: $this->_getModel()->setWatermarkPosition($position);
455: return $this;
456: }
457:
458: 459: 460: 461: 462:
463: protected function getWatermarkPosition()
464: {
465: return $this->_watermarkPosition;
466: }
467:
468: 469: 470: 471: 472: 473: 474:
475: public function setWatermarkSize($size)
476: {
477: $this->_watermarkSize = $size;
478: $this->_getModel()->setWatermarkSize($this->parseSize($size));
479: return $this;
480: }
481:
482: 483: 484: 485: 486:
487: protected function getWatermarkSize()
488: {
489: return $this->_watermarkSize;
490: }
491:
492: 493: 494: 495: 496: 497:
498: public function setWatermarkImageOpacity($imageOpacity)
499: {
500: $this->_watermarkImageOpacity = $imageOpacity;
501: $this->_getModel()->setWatermarkImageOpacity($imageOpacity);
502: return $this;
503: }
504:
505: 506: 507: 508: 509:
510: protected function getWatermarkImageOpacity()
511: {
512: if ($this->_watermarkImageOpacity) {
513: return $this->_watermarkImageOpacity;
514: }
515:
516: return $this->_getModel()->getWatermarkImageOpacity();
517: }
518:
519: 520: 521: 522: 523: 524:
525: protected function setProduct($product)
526: {
527: $this->_product = $product;
528: return $this;
529: }
530:
531: 532: 533: 534: 535:
536: protected function getProduct()
537: {
538: return $this->_product;
539: }
540:
541: 542: 543: 544: 545: 546:
547: protected function setImageFile($file)
548: {
549: $this->_imageFile = $file;
550: return $this;
551: }
552:
553: 554: 555: 556: 557:
558: protected function getImageFile()
559: {
560: return $this->_imageFile;
561: }
562:
563: 564: 565: 566: 567: 568:
569: protected function parseSize($string)
570: {
571: $size = explode('x', strtolower($string));
572: if (sizeof($size) == 2) {
573: return array(
574: 'width' => ($size[0] > 0) ? $size[0] : null,
575: 'heigth' => ($size[1] > 0) ? $size[1] : null,
576: );
577: }
578: return false;
579: }
580:
581: 582: 583: 584: 585:
586: public function getOriginalWidth()
587: {
588: return $this->_getModel()->getImageProcessor()->getOriginalWidth();
589: }
590:
591: 592: 593: 594: 595: 596:
597: public function getOriginalHeigh()
598: {
599: return $this->getOriginalHeight();
600: }
601:
602: 603: 604: 605: 606:
607: public function getOriginalHeight()
608: {
609: return $this->_getModel()->getImageProcessor()->getOriginalHeight();
610: }
611:
612: 613: 614: 615: 616: 617:
618: public function getOriginalSizeArray()
619: {
620: return array(
621: $this->getOriginalWidth(),
622: $this->getOriginalHeight()
623: );
624: }
625:
626: 627: 628: 629: 630: 631: 632:
633: public function validateUploadFile($filePath) {
634: if (!getimagesize($filePath)) {
635: Mage::throwException($this->__('Disallowed file type.'));
636: }
637:
638: $_processor = new Varien_Image($filePath);
639: return $_processor->getMimeType() !== null;
640: }
641:
642: }
643: