Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MagentoGarden_Transparentwatermark_Model_Image extends Mage_Core_Model_Abstract
- {
- private $_width = null;
- private $_height = null;
- private $_quality = 90;
- private $_keepAspectRatio = true;
- private $_keepFrame = true;
- private $_keepTransparency = true;
- private $_constrainOnly = false;
- private $_backgroundColor = array(0 => 255, 1 => 255, 2 => 255);
- private $_baseFile = null;
- private $_isBaseFilePlaceholder = null;
- private $_newFile = null;
- private $_processor = null;
- private $_destinationSubdir = null;
- private $_angle = null;
- private $_watermarkFile = null;
- private $_watermarkPosition = null;
- private $_watermarkWidth = null;
- private $_watermarkHeigth = null;
- private $_watermarkImageOpacity = 70;
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWidth($width)
- {
- $this->_width = $width;
- return $this;
- }
- function getWidth()
- {
- return $this->_width;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setHeight($height)
- {
- $this->_height = $height;
- return $this;
- }
- function getHeight()
- {
- return $this->_height;
- }
- /**
- * Set image quality, values in percentage from 0 to 100
- *
- * @param int $quality
- * @return Mage_Catalog_Model_Product_Image
- */
- function setQuality($quality)
- {
- $this->_quality = $quality;
- return $this;
- }
- /**
- * Get image quality
- *
- * @return int
- */
- function getQuality()
- {
- return $this->_quality;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setKeepAspectRatio($keep)
- {
- $this->_keepAspectRatio = (string) $keep;
- return $this;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setKeepFrame($keep)
- {
- $this->_keepFrame = (string) $keep;
- return $this;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setKeepTransparency($keep)
- {
- $this->_keepTransparency = (string) $keep;
- return $this;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setConstrainOnly($flag)
- {
- $this->_constrainOnly = (string) $flag;
- return $this;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setBackgroundColor($rgbArray)
- {
- $this->_backgroundColor = $rgbArray;
- return $this;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setSize($size)
- {
- $size_ex = explode('x', strtolower($size), 2);
- $height = $size_ex[1];
- $width = $size_ex[0];
- foreach (array(
- $width,
- $height
- ) as $wh) {
- $$wh = (int) $$wh;
- if (empty($wh)) {
- $$wh = null;
- continue;
- }
- }
- $this->setWidth($width)->setHeight($height);
- return $this;
- }
- function _checkMemory($file = null)
- {
- return ($this->_getMemoryUsage() + $this->_getNeedMemoryForFile($file) < $this->_getMemoryLimit() || $this->_getMemoryLimit() == 0 - 1);
- }
- function _getMemoryLimit()
- {
- $memoryLimit = trim(strtoupper(ini_get('memory_limit')));
- if (!isset($memoryLimit[0])) {
- $memoryLimit = '128M';
- }
- if (substr($memoryLimit, 0 - 1) == 'K') {
- return substr($memoryLimit, 0, 0 - 1) * 1024;
- }
- if (substr($memoryLimit, 0 - 1) == 'M') {
- return substr($memoryLimit, 0, 0 - 1) * 1024 * 1024;
- }
- if (substr($memoryLimit, 0 - 1) == 'G') {
- return substr($memoryLimit, 0, 0 - 1) * 1024 * 1024 * 1024;
- }
- return $memoryLimit;
- }
- function _getMemoryUsage()
- {
- if (function_exists('memory_get_usage')) {
- return memory_get_usage();
- }
- return 0;
- }
- function _getNeedMemoryForFile($file = null)
- {
- $file = (is_null($file) ? $this->getBaseFile() : $file);
- if (!$file) {
- return 0;
- }
- if ((!file_exists($file) || !is_file($file))) {
- return 0;
- }
- $imageInfo = getimagesize($file);
- if ((!isset($imageInfo[0]) || !isset($imageInfo[1]))) {
- return 0;
- }
- if (!isset($imageInfo['channels'])) {
- $imageInfo['channels'] = 4;
- }
- if (!isset($imageInfo['bits'])) {
- $imageInfo['bits'] = 8;
- }
- return round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.64999999999999991118216);
- }
- /**
- * Convert array of 3 items (decimal r, g, b) to string of their hex values
- *
- * @param array $rgbArray
- * @return string
- */
- function _rgbToString($rgbArray)
- {
- $result = array();
- foreach ($rgbArray as $value) {
- if (null === $value) {
- $result[] = 'null';
- continue;
- }
- $result[] = sprintf('%02s', dechex($value));
- }
- return implode($result);
- }
- /**
- * Set filenames for base file and new file
- *
- * @param string $file
- * @return Mage_Catalog_Model_Product_Image
- */
- function setBaseFile($file)
- {
- $this->_isBaseFilePlaceholder = false;
- if (($file && 0 !== strpos($file, '/', 0))) {
- $file = '/' . $file;
- }
- $baseDir = Mage::getsingleton('catalog/product_media_config')->getBaseMediaPath();
- if ('/no_selection' == $file) {
- $file = null;
- }
- if ($file) {
- if ((!$this->_fileExists($baseDir . $file) || !$this->_checkMemory($baseDir . $file))) {
- $file = null;
- }
- }
- if (!$file) {
- $isConfigPlaceholder = Mage::getstoreconfig('' . 'catalog/placeholder/' . $this->getDestinationSubdir() . '_placeholder');
- $configPlaceholder = '/placeholder/' . $isConfigPlaceholder;
- if (($isConfigPlaceholder && $this->_fileExists($baseDir . $configPlaceholder))) {
- $file = $configPlaceholder;
- } else {
- $skinBaseDir = Mage::getdesign()->getSkinBaseDir();
- $skinPlaceholder = '' . '/images/catalog/product/placeholder/' . $this->getDestinationSubdir() . '.jpg';
- $file = $skinPlaceholder;
- if (file_exists($skinBaseDir . $file)) {
- $baseDir = $skinBaseDir;
- } else {
- $baseDir = Mage::getdesign()->getSkinBaseDir(array(
- '_theme' => 'default'
- ));
- if (!file_exists($baseDir . $file)) {
- $baseDir = Mage::getdesign()->getSkinBaseDir(array(
- '_theme' => 'default',
- '_package' => 'base'
- ));
- }
- }
- }
- $this->_isBaseFilePlaceholder = true;
- }
- $baseFile = $baseDir . $file;
- if ((!$file || !file_exists($baseFile))) {
- throw new Exception(Mage::helper('catalog'), ('Image file was not found.'));
- }
- $this->_baseFile = $baseFile;
- $path[] = $this->getDestinationSubdir();
- $path = array(
- Mage::getsingleton('catalog/product_media_config')->getBaseMediaPath(),
- 'cache',
- Mage::app()->getStore()->getId()
- );
- if ((!empty($this->_width) || !empty($this->_height))) {
- $path[] = '' . $this->_width . 'x' . $this->_height;
- }
- $miscParams = array(
- ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
- ($this->_keepFrame ? '' : 'no') . 'frame',
- ($this->_keepTransparency ? '' : 'no') . 'transparency',
- ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
- $this->_rgbToString($this->_backgroundColor),
- 'angle' . $this->_angle,
- 'quality' . $this->_quality
- );
- if ($this->getWatermarkFile()) {
- $miscParams[] = $this->getWatermarkFile();
- $miscParams[] = $this->getWatermarkImageOpacity();
- $miscParams[] = $this->getWatermarkPosition();
- $miscParams[] = $this->getWatermarkWidth();
- $miscParams[] = $this->getWatermarkHeigth();
- }
- foreach ($miscParams as $val => $key) {
- if (is_array($val)) {
- $miscParams[$key] = implode('_', $val);
- continue;
- }
- }
- $path[] = md5(implode('_', $miscParams));
- $this->_newFile = implode('/', $path) . $file;
- return $this;
- }
- function getBaseFile()
- {
- return $this->_baseFile;
- }
- function getNewFile()
- {
- return $this->_newFile;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setImageProcessor($processor)
- {
- $this->_processor = $processor;
- return $this;
- }
- /**
- * @return Varien_Image
- */
- function getImageProcessor()
- {
- if (!$this->_processor) {
- if (Mage::helper('transparentwatermark')->isEnabled()) {
- $this->_processor = new MagentoGarden_Image($this->getBaseFile());
- } else {
- $this->_processor = new Varien_Image($this->getBaseFile());
- }
- }
- $this->_processor->keepAspectRatio($this->_keepAspectRatio);
- $this->_processor->keepFrame($this->_keepFrame);
- $this->_processor->keepTransparency($this->_keepTransparency);
- $this->_processor->constrainOnly($this->_constrainOnly);
- $this->_processor->backgroundColor($this->_backgroundColor);
- $this->_processor->quality($this->_quality);
- return $this->_processor;
- }
- /**
- * @see Varien_Image_Adapter_Abstract
- * @return Mage_Catalog_Model_Product_Image
- */
- function resize()
- {
- if ((is_null($this->getWidth()) && is_null($this->getHeight()))) {
- return $this;
- }
- $this->getImageProcessor()->resize($this->_width, $this->_height);
- return $this;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function rotate($angle)
- {
- $angle = intval($angle);
- $this->getImageProcessor()->rotate($angle);
- return $this;
- }
- /**
- * Set angle for rotating
- *
- * This func actually affects only the cache filename.
- *
- * @param int $angle
- * @return Mage_Catalog_Model_Product_Image
- */
- function setAngle($angle)
- {
- $this->_angle = $angle;
- return $this;
- }
- /**
- * Add watermark to image
- * size param in format 100x200
- *
- * @param string $file
- * @param string $position
- * @param string $size
- * @param int $width
- * @param int $heigth
- * @param int $imageOpacity
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWatermark($file, $position = null, $size = null, $width = null, $heigth = null, $imageOpacity = null, $_is_transparent_watermark = false, $_position_type = 0)
- {
- if ($this->_isBaseFilePlaceholder) {
- return $this;
- }
- if ($file) {
- $this->setWatermarkFile($file);
- } else {
- return $this;
- }
- if ($position) {
- $this->setWatermarkPosition($position);
- }
- if ($size) {
- $this->setWatermarkSize($size);
- }
- if ($width) {
- $this->setWatermarkWidth($width);
- }
- if ($heigth) {
- $this->setWatermarkHeigth($heigth);
- }
- if ($imageOpacity) {
- $this->setImageOpacity($imageOpacity);
- }
- $filePath = $this->_getWatermarkFilePath();
- if ($filePath) {
- if (Mage::helper('transparentwatermark')->isEnabled()) {
- $this->getImageProcessor()->setWatermarkPosition($this->getWatermarkPosition())->setWatermarkImageOpacity($this->getWatermarkImageOpacity())->setWatermarkWidth($this->getWatermarkWidth())->setWatermarkHeigth($this->getWatermarkHeigth())->setIsCategoryWatermark($_is_transparent_watermark)->setPositionType($_position_type)->watermark($filePath);
- } else {
- $this->getImageProcessor()->setWatermarkPosition($this->getWatermarkPosition())->setWatermarkImageOpacity($this->getWatermarkImageOpacity())->setWatermarkWidth($this->getWatermarkWidth())->setWatermarkHeigth($this->getWatermarkHeigth())->watermark($filePath);
- }
- }
- return $this;
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function saveFile()
- {
- $filename = $this->getNewFile();
- $this->getImageProcessor()->save($filename);
- Mage::helper('core/file_storage_database')->saveFile($filename);
- return $this;
- }
- /**
- * @return string
- */
- function getUrl()
- {
- $baseDir = Mage::getbasedir('media');
- $path = str_replace($baseDir . DS, '', $this->_newFile);
- return Mage::getbaseurl('media') . str_replace(DS, '/', $path);
- }
- function push()
- {
- $this->getImageProcessor()->display();
- }
- /**
- * @return Mage_Catalog_Model_Product_Image
- */
- function setDestinationSubdir($dir)
- {
- $this->_destinationSubdir = $dir;
- return $this;
- }
- /**
- * @return string
- */
- function getDestinationSubdir()
- {
- return $this->_destinationSubdir;
- }
- function isCached()
- {
- return $this->_fileExists($this->_newFile);
- }
- /**
- * Set watermark file name
- *
- * @param string $file
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWatermarkFile($file)
- {
- $this->_watermarkFile = $file;
- return $this;
- }
- /**
- * Get watermark file name
- *
- * @return string
- */
- function getWatermarkFile()
- {
- return $this->_watermarkFile;
- }
- /**
- * Get relative watermark file path
- * or false if file not found
- *
- * @return string | bool
- */
- function _getWatermarkFilePath()
- {
- $filePath = false;
- if (!$file = $this->getWatermarkFile()) {
- return $filePath;
- }
- $baseDir = Mage::getsingleton('catalog/product_media_config')->getBaseMediaPath();
- if ($this->_fileExists($baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file)) {
- $filePath = $baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file;
- } else {
- if ($this->_fileExists($baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file)) {
- $filePath = $baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file;
- } else {
- if ($this->_fileExists($baseDir . '/watermark/default/' . $file)) {
- $filePath = $baseDir . '/watermark/default/' . $file;
- } else {
- if ($this->_fileExists($baseDir . '/watermark/' . $file)) {
- $filePath = $baseDir . '/watermark/' . $file;
- } else {
- if ($this->_fileExists(Mage::getbasedir('media') . DS . $file)) {
- $filePath = Mage::getbasedir('media') . DS . $file;
- } else {
- $baseDir = Mage::getdesign()->getSkinBaseDir();
- if ($this->_fileExists($baseDir . $file)) {
- $filePath = $baseDir . $file;
- }
- }
- }
- }
- }
- }
- return $filePath;
- }
- /**
- * Set watermark position
- *
- * @param string $position
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWatermarkPosition($position)
- {
- $this->_watermarkPosition = $position;
- return $this;
- }
- /**
- * Get watermark position
- *
- * @return string
- */
- function getWatermarkPosition()
- {
- return $this->_watermarkPosition;
- }
- /**
- * Set watermark image opacity
- *
- * @param int $imageOpacity
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWatermarkImageOpacity($imageOpacity)
- {
- $this->_watermarkImageOpacity = $imageOpacity;
- return $this;
- }
- /**
- * Get watermark image opacity
- *
- * @return int
- */
- function getWatermarkImageOpacity()
- {
- return $this->_watermarkImageOpacity;
- }
- /**
- * Set watermark size
- *
- * @param array $size
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWatermarkSize($size)
- {
- if (is_array($size)) {
- $this->setWatermarkWidth($size['width'])->setWatermarkHeigth($size['heigth']);
- }
- return $this;
- }
- /**
- * Set watermark width
- *
- * @param int $width
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWatermarkWidth($width)
- {
- $this->_watermarkWidth = $width;
- return $this;
- }
- /**
- * Get watermark width
- *
- * @return int
- */
- function getWatermarkWidth()
- {
- return $this->_watermarkWidth;
- }
- /**
- * Set watermark heigth
- *
- * @param int $heigth
- * @return Mage_Catalog_Model_Product_Image
- */
- function setWatermarkHeigth($heigth)
- {
- $this->_watermarkHeigth = $heigth;
- return $this;
- }
- /**
- * Get watermark heigth
- *
- * @return string
- */
- function getWatermarkHeigth()
- {
- return $this->_watermarkHeigth;
- }
- function clearCache()
- {
- $directory = Mage::getbasedir('media') . DS . 'catalog' . DS . 'product' . DS . 'cache' . DS;
- $io = new Varien_Io_File();
- $io->rmdir($directory, true);
- Mage::helper('core/file_storage_database')->deleteFolder($directory);
- }
- /**
- * First check this file on FS
- * If it doesn't exist - try to download it from DB
- *
- * @param string $filename
- * @return bool
- */
- function _fileExists($filename)
- {
- $_version = explode('.', Mage::getversion());
- if ($_version[1] <= 4) {
- return file_exists($filename);
- }
- if (file_exists($filename)) {
- return true;
- }
- return Mage::helper('core/file_storage_database')->saveFileToFilesystem($filename);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement