Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Media_Helper_Data
  • Mage_Media_Model_File_Image
  • Mage_Media_Model_Image

Interfaces

  • Mage_Media_Model_Image_Config_Interface
  • Overview
  • Package
  • Class
  • Tree
  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_Media
 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: /**
 29:  * Media library Image model
 30:  *
 31:  * @category   Mage
 32:  * @package    Mage_Media
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Media_Model_Image extends Mage_Core_Model_Abstract
 36: {
 37:     /**
 38:      * Image config instance
 39:      *
 40:      * @var Mage_Media_Model_Image_Config_Interface
 41:      */
 42:     protected $_config;
 43: 
 44:     /**
 45:      * Image resource
 46:      *
 47:      * @var resource
 48:      */
 49:     protected $_image;
 50: 
 51:     /**
 52:      * Tmp image resource
 53:      *
 54:      * @var resource
 55:      */
 56:     protected $_tmpImage;
 57: 
 58:     /**
 59:      * Params for filename generation
 60:      *
 61:      * @var array
 62:      */
 63:     protected $_params = array();
 64: 
 65: 
 66:     protected function _construct()
 67:     {
 68:         $this->_init('media/image');
 69:     }
 70: 
 71:     /**
 72:      * Set media image config instance
 73:      *
 74:      * @param Mage_Media_Model_Image_Config_Interface $config
 75:      * @return unknown
 76:      */
 77:     public function setConfig(Mage_Media_Model_Image_Config_Interface $config)
 78:     {
 79:         $this->_config = $config;
 80:         return $this;
 81:     }
 82: 
 83:     /**
 84:      * Retrive media image config instance
 85:      *
 86:      * @return Mage_Media_Model_Image_Config_Interface
 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:      * Retrive source dimensions object
113:      *
114:      * @return Varien_Object
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:      * Retrive destanation dimensions object
126:      *
127:      * @return Varien_Object
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:      * Return special link (with creating image if not exists)
215:      *
216:      * @param string $file
217:      * @param string $size
218:      * @param string $extension
219:      * @param string $watermark
220:      * @return string
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) { // If image smaller than needed
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: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0