Advertisement
plas71k

Image_local.php ioncube7 => decoded

Mar 24th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.07 KB | None | 0 0
  1. <?php
  2. class MagentoGarden_Transparentwatermark_Model_Image extends Mage_Core_Model_Abstract
  3. {
  4.     private $_width = null;
  5.     private $_height = null;
  6.     private $_quality = 90;
  7.     private $_keepAspectRatio = true;
  8.     private $_keepFrame = true;
  9.     private $_keepTransparency = true;
  10.     private $_constrainOnly = false;
  11.     private $_backgroundColor = array(0 => 255, 1 => 255, 2 => 255);
  12.     private $_baseFile = null;
  13.     private $_isBaseFilePlaceholder = null;
  14.     private $_newFile = null;
  15.     private $_processor = null;
  16.     private $_destinationSubdir = null;
  17.     private $_angle = null;
  18.     private $_watermarkFile = null;
  19.     private $_watermarkPosition = null;
  20.     private $_watermarkWidth = null;
  21.     private $_watermarkHeigth = null;
  22.     private $_watermarkImageOpacity = 70;
  23.    
  24.     /**
  25.      * @return Mage_Catalog_Model_Product_Image
  26.      */
  27.     function setWidth($width)
  28.     {
  29.         $this->_width = $width;
  30.         return $this;
  31.     }
  32.    
  33.     function getWidth()
  34.     {
  35.         return $this->_width;
  36.     }
  37.    
  38.     /**
  39.      * @return Mage_Catalog_Model_Product_Image
  40.      */
  41.     function setHeight($height)
  42.     {
  43.         $this->_height = $height;
  44.         return $this;
  45.     }
  46.    
  47.     function getHeight()
  48.     {
  49.         return $this->_height;
  50.     }
  51.    
  52.     /**
  53.      * Set image quality, values in percentage from 0 to 100
  54.      *
  55.      * @param int $quality
  56.      * @return Mage_Catalog_Model_Product_Image
  57.      */
  58.     function setQuality($quality)
  59.     {
  60.         $this->_quality = $quality;
  61.         return $this;
  62.     }
  63.    
  64.     /**
  65.      * Get image quality
  66.      *
  67.      * @return int
  68.      */
  69.     function getQuality()
  70.     {
  71.         return $this->_quality;
  72.     }
  73.    
  74.     /**
  75.      * @return Mage_Catalog_Model_Product_Image
  76.      */
  77.     function setKeepAspectRatio($keep)
  78.     {
  79.         $this->_keepAspectRatio = (string) $keep;
  80.         return $this;
  81.     }
  82.    
  83.     /**
  84.      * @return Mage_Catalog_Model_Product_Image
  85.      */
  86.     function setKeepFrame($keep)
  87.     {
  88.         $this->_keepFrame = (string) $keep;
  89.         return $this;
  90.     }
  91.    
  92.     /**
  93.      * @return Mage_Catalog_Model_Product_Image
  94.      */
  95.     function setKeepTransparency($keep)
  96.     {
  97.         $this->_keepTransparency = (string) $keep;
  98.         return $this;
  99.     }
  100.    
  101.     /**
  102.      * @return Mage_Catalog_Model_Product_Image
  103.      */
  104.     function setConstrainOnly($flag)
  105.     {
  106.         $this->_constrainOnly = (string) $flag;
  107.         return $this;
  108.     }
  109.    
  110.     /**
  111.      * @return Mage_Catalog_Model_Product_Image
  112.      */
  113.     function setBackgroundColor($rgbArray)
  114.     {
  115.         $this->_backgroundColor = $rgbArray;
  116.         return $this;
  117.     }
  118.    
  119.     /**
  120.      * @return Mage_Catalog_Model_Product_Image
  121.      */
  122.     function setSize($size)
  123.     {
  124.         $size_ex = explode('x', strtolower($size), 2);
  125.         $height  = $size_ex[1];
  126.         $width   = $size_ex[0];
  127.         foreach (array(
  128.             $width,
  129.             $height
  130.         ) as $wh) {
  131.             $$wh = (int) $$wh;
  132.            
  133.             if (empty($wh)) {
  134.                 $$wh = null;
  135.                 continue;
  136.             }
  137.         }
  138.        
  139.         $this->setWidth($width)->setHeight($height);
  140.         return $this;
  141.     }
  142.    
  143.     function _checkMemory($file = null)
  144.     {
  145.         return ($this->_getMemoryUsage() + $this->_getNeedMemoryForFile($file) < $this->_getMemoryLimit() || $this->_getMemoryLimit() == 0 - 1);
  146.     }
  147.    
  148.     function _getMemoryLimit()
  149.     {
  150.         $memoryLimit = trim(strtoupper(ini_get('memory_limit')));
  151.        
  152.         if (!isset($memoryLimit[0])) {
  153.             $memoryLimit = '128M';
  154.         }
  155.        
  156.        
  157.         if (substr($memoryLimit, 0 - 1) == 'K') {
  158.             return substr($memoryLimit, 0, 0 - 1) * 1024;
  159.         }
  160.        
  161.        
  162.         if (substr($memoryLimit, 0 - 1) == 'M') {
  163.             return substr($memoryLimit, 0, 0 - 1) * 1024 * 1024;
  164.         }
  165.        
  166.        
  167.         if (substr($memoryLimit, 0 - 1) == 'G') {
  168.             return substr($memoryLimit, 0, 0 - 1) * 1024 * 1024 * 1024;
  169.         }
  170.        
  171.         return $memoryLimit;
  172.     }
  173.    
  174.     function _getMemoryUsage()
  175.     {
  176.         if (function_exists('memory_get_usage')) {
  177.             return memory_get_usage();
  178.         }
  179.        
  180.         return 0;
  181.     }
  182.    
  183.     function _getNeedMemoryForFile($file = null)
  184.     {
  185.         $file = (is_null($file) ? $this->getBaseFile() : $file);
  186.        
  187.         if (!$file) {
  188.             return 0;
  189.         }
  190.        
  191.        
  192.         if ((!file_exists($file) || !is_file($file))) {
  193.             return 0;
  194.         }
  195.        
  196.         $imageInfo = getimagesize($file);
  197.        
  198.         if ((!isset($imageInfo[0]) || !isset($imageInfo[1]))) {
  199.             return 0;
  200.         }
  201.        
  202.        
  203.         if (!isset($imageInfo['channels'])) {
  204.             $imageInfo['channels'] = 4;
  205.         }
  206.        
  207.        
  208.         if (!isset($imageInfo['bits'])) {
  209.             $imageInfo['bits'] = 8;
  210.         }
  211.        
  212.         return round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.64999999999999991118216);
  213.     }
  214.    
  215.     /**
  216.      * Convert array of 3 items (decimal r, g, b) to string of their hex values
  217.      *
  218.      * @param array $rgbArray
  219.      * @return string
  220.      */
  221.     function _rgbToString($rgbArray)
  222.     {
  223.         $result = array();
  224.         foreach ($rgbArray as $value) {
  225.            
  226.             if (null === $value) {
  227.                 $result[] = 'null';
  228.                 continue;
  229.             }
  230.            
  231.             $result[] = sprintf('%02s', dechex($value));
  232.         }
  233.        
  234.         return implode($result);
  235.     }
  236.    
  237.     /**
  238.      * Set filenames for base file and new file
  239.      *
  240.      * @param string $file
  241.      * @return Mage_Catalog_Model_Product_Image
  242.      */
  243.     function setBaseFile($file)
  244.     {
  245.         $this->_isBaseFilePlaceholder = false;
  246.        
  247.         if (($file && 0 !== strpos($file, '/', 0))) {
  248.             $file = '/' . $file;
  249.         }
  250.        
  251.         $baseDir = Mage::getsingleton('catalog/product_media_config')->getBaseMediaPath();
  252.        
  253.         if ('/no_selection' == $file) {
  254.             $file = null;
  255.         }
  256.        
  257.        
  258.         if ($file) {
  259.             if ((!$this->_fileExists($baseDir . $file) || !$this->_checkMemory($baseDir . $file))) {
  260.                 $file = null;
  261.             }
  262.         }
  263.        
  264.        
  265.         if (!$file) {
  266.             $isConfigPlaceholder = Mage::getstoreconfig('' . 'catalog/placeholder/' . $this->getDestinationSubdir() . '_placeholder');
  267.             $configPlaceholder   = '/placeholder/' . $isConfigPlaceholder;
  268.            
  269.             if (($isConfigPlaceholder && $this->_fileExists($baseDir . $configPlaceholder))) {
  270.                 $file = $configPlaceholder;
  271.             } else {
  272.                 $skinBaseDir     = Mage::getdesign()->getSkinBaseDir();
  273.                 $skinPlaceholder = '' . '/images/catalog/product/placeholder/' . $this->getDestinationSubdir() . '.jpg';
  274.                 $file            = $skinPlaceholder;
  275.                
  276.                 if (file_exists($skinBaseDir . $file)) {
  277.                     $baseDir = $skinBaseDir;
  278.                 } else {
  279.                     $baseDir = Mage::getdesign()->getSkinBaseDir(array(
  280.                         '_theme' => 'default'
  281.                     ));
  282.                    
  283.                     if (!file_exists($baseDir . $file)) {
  284.                         $baseDir = Mage::getdesign()->getSkinBaseDir(array(
  285.                             '_theme' => 'default',
  286.                             '_package' => 'base'
  287.                         ));
  288.                     }
  289.                 }
  290.             }
  291.            
  292.             $this->_isBaseFilePlaceholder = true;
  293.         }
  294.        
  295.         $baseFile = $baseDir . $file;
  296.        
  297.         if ((!$file || !file_exists($baseFile))) {
  298.             throw new Exception(Mage::helper('catalog'), ('Image file was not found.'));
  299.         }
  300.        
  301.         $this->_baseFile = $baseFile;
  302.         $path[]          = $this->getDestinationSubdir();
  303.         $path            = array(
  304.             Mage::getsingleton('catalog/product_media_config')->getBaseMediaPath(),
  305.             'cache',
  306.             Mage::app()->getStore()->getId()
  307.         );
  308.        
  309.         if ((!empty($this->_width) || !empty($this->_height))) {
  310.             $path[] = '' . $this->_width . 'x' . $this->_height;
  311.         }
  312.        
  313.         $miscParams = array(
  314.             ($this->_keepAspectRatio ? '' : 'non') . 'proportional',
  315.             ($this->_keepFrame ? '' : 'no') . 'frame',
  316.             ($this->_keepTransparency ? '' : 'no') . 'transparency',
  317.             ($this->_constrainOnly ? 'do' : 'not') . 'constrainonly',
  318.             $this->_rgbToString($this->_backgroundColor),
  319.             'angle' . $this->_angle,
  320.             'quality' . $this->_quality
  321.         );
  322.        
  323.         if ($this->getWatermarkFile()) {
  324.             $miscParams[] = $this->getWatermarkFile();
  325.             $miscParams[] = $this->getWatermarkImageOpacity();
  326.             $miscParams[] = $this->getWatermarkPosition();
  327.             $miscParams[] = $this->getWatermarkWidth();
  328.             $miscParams[] = $this->getWatermarkHeigth();
  329.         }
  330.        
  331.         foreach ($miscParams as $val => $key) {
  332.            
  333.             if (is_array($val)) {
  334.                 $miscParams[$key] = implode('_', $val);
  335.                 continue;
  336.             }
  337.         }
  338.        
  339.         $path[]         = md5(implode('_', $miscParams));
  340.         $this->_newFile = implode('/', $path) . $file;
  341.         return $this;
  342.     }
  343.    
  344.     function getBaseFile()
  345.     {
  346.         return $this->_baseFile;
  347.     }
  348.    
  349.     function getNewFile()
  350.     {
  351.         return $this->_newFile;
  352.     }
  353.    
  354.     /**
  355.      * @return Mage_Catalog_Model_Product_Image
  356.      */
  357.     function setImageProcessor($processor)
  358.     {
  359.         $this->_processor = $processor;
  360.         return $this;
  361.     }
  362.    
  363.     /**
  364.      * @return Varien_Image
  365.      */
  366.     function getImageProcessor()
  367.     {
  368.         if (!$this->_processor) {
  369.             if (Mage::helper('transparentwatermark')->isEnabled()) {
  370.                 $this->_processor = new MagentoGarden_Image($this->getBaseFile());
  371.             } else {
  372.                 $this->_processor = new Varien_Image($this->getBaseFile());
  373.             }
  374.         }
  375.        
  376.         $this->_processor->keepAspectRatio($this->_keepAspectRatio);
  377.         $this->_processor->keepFrame($this->_keepFrame);
  378.         $this->_processor->keepTransparency($this->_keepTransparency);
  379.         $this->_processor->constrainOnly($this->_constrainOnly);
  380.         $this->_processor->backgroundColor($this->_backgroundColor);
  381.         $this->_processor->quality($this->_quality);
  382.         return $this->_processor;
  383.     }
  384.    
  385.     /**
  386.      * @see Varien_Image_Adapter_Abstract
  387.      * @return Mage_Catalog_Model_Product_Image
  388.      */
  389.     function resize()
  390.     {
  391.         if ((is_null($this->getWidth()) && is_null($this->getHeight()))) {
  392.             return $this;
  393.         }
  394.        
  395.         $this->getImageProcessor()->resize($this->_width, $this->_height);
  396.         return $this;
  397.     }
  398.    
  399.     /**
  400.      * @return Mage_Catalog_Model_Product_Image
  401.      */
  402.     function rotate($angle)
  403.     {
  404.         $angle = intval($angle);
  405.         $this->getImageProcessor()->rotate($angle);
  406.         return $this;
  407.     }
  408.    
  409.     /**
  410.      * Set angle for rotating
  411.      *
  412.      * This func actually affects only the cache filename.
  413.      *
  414.      * @param int $angle
  415.      * @return Mage_Catalog_Model_Product_Image
  416.      */
  417.     function setAngle($angle)
  418.     {
  419.         $this->_angle = $angle;
  420.         return $this;
  421.     }
  422.    
  423.     /**
  424.      * Add watermark to image
  425.      * size param in format 100x200
  426.      *
  427.      * @param string $file
  428.      * @param string $position
  429.      * @param string $size
  430.      * @param int $width
  431.      * @param int $heigth
  432.      * @param int $imageOpacity
  433.      * @return Mage_Catalog_Model_Product_Image
  434.      */
  435.     function setWatermark($file, $position = null, $size = null, $width = null, $heigth = null, $imageOpacity = null, $_is_transparent_watermark = false, $_position_type = 0)
  436.     {
  437.         if ($this->_isBaseFilePlaceholder) {
  438.             return $this;
  439.         }
  440.        
  441.        
  442.         if ($file) {
  443.             $this->setWatermarkFile($file);
  444.         } else {
  445.             return $this;
  446.         }
  447.        
  448.        
  449.         if ($position) {
  450.             $this->setWatermarkPosition($position);
  451.         }
  452.        
  453.        
  454.         if ($size) {
  455.             $this->setWatermarkSize($size);
  456.         }
  457.        
  458.        
  459.         if ($width) {
  460.             $this->setWatermarkWidth($width);
  461.         }
  462.        
  463.        
  464.         if ($heigth) {
  465.             $this->setWatermarkHeigth($heigth);
  466.         }
  467.        
  468.        
  469.         if ($imageOpacity) {
  470.             $this->setImageOpacity($imageOpacity);
  471.         }
  472.        
  473.         $filePath = $this->_getWatermarkFilePath();
  474.        
  475.         if ($filePath) {
  476.             if (Mage::helper('transparentwatermark')->isEnabled()) {
  477.                 $this->getImageProcessor()->setWatermarkPosition($this->getWatermarkPosition())->setWatermarkImageOpacity($this->getWatermarkImageOpacity())->setWatermarkWidth($this->getWatermarkWidth())->setWatermarkHeigth($this->getWatermarkHeigth())->setIsCategoryWatermark($_is_transparent_watermark)->setPositionType($_position_type)->watermark($filePath);
  478.             } else {
  479.                 $this->getImageProcessor()->setWatermarkPosition($this->getWatermarkPosition())->setWatermarkImageOpacity($this->getWatermarkImageOpacity())->setWatermarkWidth($this->getWatermarkWidth())->setWatermarkHeigth($this->getWatermarkHeigth())->watermark($filePath);
  480.             }
  481.         }
  482.        
  483.         return $this;
  484.     }
  485.    
  486.     /**
  487.      * @return Mage_Catalog_Model_Product_Image
  488.      */
  489.     function saveFile()
  490.     {
  491.         $filename = $this->getNewFile();
  492.         $this->getImageProcessor()->save($filename);
  493.         Mage::helper('core/file_storage_database')->saveFile($filename);
  494.         return $this;
  495.     }
  496.    
  497.     /**
  498.      * @return string
  499.      */
  500.     function getUrl()
  501.     {
  502.         $baseDir = Mage::getbasedir('media');
  503.         $path    = str_replace($baseDir . DS, '', $this->_newFile);
  504.         return Mage::getbaseurl('media') . str_replace(DS, '/', $path);
  505.     }
  506.    
  507.     function push()
  508.     {
  509.         $this->getImageProcessor()->display();
  510.     }
  511.    
  512.     /**
  513.      * @return Mage_Catalog_Model_Product_Image
  514.      */
  515.     function setDestinationSubdir($dir)
  516.     {
  517.         $this->_destinationSubdir = $dir;
  518.         return $this;
  519.     }
  520.    
  521.     /**
  522.      * @return string
  523.      */
  524.     function getDestinationSubdir()
  525.     {
  526.         return $this->_destinationSubdir;
  527.     }
  528.    
  529.     function isCached()
  530.     {
  531.         return $this->_fileExists($this->_newFile);
  532.     }
  533.    
  534.     /**
  535.      * Set watermark file name
  536.      *
  537.      * @param string $file
  538.      * @return Mage_Catalog_Model_Product_Image
  539.      */
  540.     function setWatermarkFile($file)
  541.     {
  542.         $this->_watermarkFile = $file;
  543.         return $this;
  544.     }
  545.    
  546.     /**
  547.      * Get watermark file name
  548.      *
  549.      * @return string
  550.      */
  551.     function getWatermarkFile()
  552.     {
  553.         return $this->_watermarkFile;
  554.     }
  555.    
  556.     /**
  557.      * Get relative watermark file path
  558.      * or false if file not found
  559.      *
  560.      * @return string | bool
  561.      */
  562.     function _getWatermarkFilePath()
  563.     {
  564.         $filePath = false;
  565.         if (!$file = $this->getWatermarkFile()) {
  566.             return $filePath;
  567.         }
  568.        
  569.         $baseDir = Mage::getsingleton('catalog/product_media_config')->getBaseMediaPath();
  570.        
  571.         if ($this->_fileExists($baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file)) {
  572.             $filePath = $baseDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . $file;
  573.         } else {
  574.             if ($this->_fileExists($baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file)) {
  575.                 $filePath = $baseDir . '/watermark/websites/' . Mage::app()->getWebsite()->getId() . $file;
  576.             } else {
  577.                 if ($this->_fileExists($baseDir . '/watermark/default/' . $file)) {
  578.                     $filePath = $baseDir . '/watermark/default/' . $file;
  579.                 } else {
  580.                     if ($this->_fileExists($baseDir . '/watermark/' . $file)) {
  581.                         $filePath = $baseDir . '/watermark/' . $file;
  582.                     } else {
  583.                         if ($this->_fileExists(Mage::getbasedir('media') . DS . $file)) {
  584.                             $filePath = Mage::getbasedir('media') . DS . $file;
  585.                         } else {
  586.                             $baseDir = Mage::getdesign()->getSkinBaseDir();
  587.                            
  588.                             if ($this->_fileExists($baseDir . $file)) {
  589.                                 $filePath = $baseDir . $file;
  590.                             }
  591.                         }
  592.                     }
  593.                 }
  594.             }
  595.         }
  596.        
  597.         return $filePath;
  598.     }
  599.    
  600.     /**
  601.      * Set watermark position
  602.      *
  603.      * @param string $position
  604.      * @return Mage_Catalog_Model_Product_Image
  605.      */
  606.     function setWatermarkPosition($position)
  607.     {
  608.         $this->_watermarkPosition = $position;
  609.         return $this;
  610.     }
  611.    
  612.     /**
  613.      * Get watermark position
  614.      *
  615.      * @return string
  616.      */
  617.     function getWatermarkPosition()
  618.     {
  619.         return $this->_watermarkPosition;
  620.     }
  621.    
  622.     /**
  623.      * Set watermark image opacity
  624.      *
  625.      * @param int $imageOpacity
  626.      * @return Mage_Catalog_Model_Product_Image
  627.      */
  628.     function setWatermarkImageOpacity($imageOpacity)
  629.     {
  630.         $this->_watermarkImageOpacity = $imageOpacity;
  631.         return $this;
  632.     }
  633.    
  634.     /**
  635.      * Get watermark image opacity
  636.      *
  637.      * @return int
  638.      */
  639.     function getWatermarkImageOpacity()
  640.     {
  641.         return $this->_watermarkImageOpacity;
  642.     }
  643.    
  644.     /**
  645.      * Set watermark size
  646.      *
  647.      * @param array $size
  648.      * @return Mage_Catalog_Model_Product_Image
  649.      */
  650.     function setWatermarkSize($size)
  651.     {
  652.         if (is_array($size)) {
  653.             $this->setWatermarkWidth($size['width'])->setWatermarkHeigth($size['heigth']);
  654.         }
  655.        
  656.         return $this;
  657.     }
  658.    
  659.     /**
  660.      * Set watermark width
  661.      *
  662.      * @param int $width
  663.      * @return Mage_Catalog_Model_Product_Image
  664.      */
  665.     function setWatermarkWidth($width)
  666.     {
  667.         $this->_watermarkWidth = $width;
  668.         return $this;
  669.     }
  670.    
  671.     /**
  672.      * Get watermark width
  673.      *
  674.      * @return int
  675.      */
  676.     function getWatermarkWidth()
  677.     {
  678.         return $this->_watermarkWidth;
  679.     }
  680.    
  681.     /**
  682.      * Set watermark heigth
  683.      *
  684.      * @param int $heigth
  685.      * @return Mage_Catalog_Model_Product_Image
  686.      */
  687.     function setWatermarkHeigth($heigth)
  688.     {
  689.         $this->_watermarkHeigth = $heigth;
  690.         return $this;
  691.     }
  692.    
  693.     /**
  694.      * Get watermark heigth
  695.      *
  696.      * @return string
  697.      */
  698.     function getWatermarkHeigth()
  699.     {
  700.         return $this->_watermarkHeigth;
  701.     }
  702.    
  703.     function clearCache()
  704.     {
  705.         $directory = Mage::getbasedir('media') . DS . 'catalog' . DS . 'product' . DS . 'cache' . DS;
  706.         $io        = new Varien_Io_File();
  707.         $io->rmdir($directory, true);
  708.         Mage::helper('core/file_storage_database')->deleteFolder($directory);
  709.     }
  710.    
  711.     /**
  712.      * First check this file on FS
  713.      * If it doesn't exist - try to download it from DB
  714.      *
  715.      * @param string $filename
  716.      * @return bool
  717.      */
  718.     function _fileExists($filename)
  719.     {
  720.         $_version = explode('.', Mage::getversion());
  721.        
  722.         if ($_version[1] <= 4) {
  723.             return file_exists($filename);
  724.         }
  725.        
  726.        
  727.         if (file_exists($filename)) {
  728.             return true;
  729.         }
  730.        
  731.         return Mage::helper('core/file_storage_database')->saveFileToFilesystem($filename);
  732.     }
  733. }
  734.  
  735. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement