self::MIN_HEIGHT, 'minwidth' => self::MIN_WIDTH, 'maxheight' => self::MAX_HEIGHT, 'maxwidth' => self::MAX_WIDTH, ); protected $_baseUrl; protected $_baseDir; protected $_allowedExtensions = array('jpg', 'jpeg' ,'gif', 'png'); protected $_columns; /** * @return Orange35_ImageConstructor_Model_Matches */ protected function _getMatchesModel() { return Mage::getModel('orange35_imageConstructor/matches'); } protected function _getColumns($column) { if(!isset($this->_columns)){ $this->_columns = $this->_getMatchesModel()->getCollection() ->getColumValues($column); } return $this->_columns; } protected function _getMatchesCollection($matchId) { $collection = $this->_getMatchesModel() ->getCollection() ->getItemById($matchId); return $collection; } protected function delTree($dir) { $files = array_diff(scandir($dir), array('.', '..')); foreach ($files as $file) { (is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file"); } return rmdir($dir); } public function getBaseUrl() { if(!isset($this->_baseUrl)){ $this->_baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . self::MEDIA_PATH; } return $this->_baseUrl; } public function getBaseDir() { if(!isset($this->_baseDir)){ $this->_baseDir = Mage::getBaseDir('media') . DS . str_replace("/", DS, self::MEDIA_PATH); } return $this->_baseDir; } public function upload($storeDir, $fieldName, $valueId) { $path = $this->getBaseDir() . $storeDir . DS; try { $uploader = new Orange35_ImageConstructor_Model_File_Uploader($fieldName); $uploader->setAllowRenameFiles(false); $uploader->setFilesDispersion(false); $uploader->setAllowCreateFolders(true); $uploader->setAllowedExtensions($this->_allowedExtensions); $unique = (int)(microtime() * 100000); $fileSavingName = $valueId . "-" . $unique . '.' . $uploader->getFileExtension(); $result = $uploader->save($path, $fileSavingName); } catch (Exception $e) { Mage::logException($e); return false; } return array(isset($result['file']) ? $result['file'] : false, 'name' => $fileSavingName); } /** * [product_id]/[option_id]/[image] * @return Orange35_ImageConstructor_Helper_Matches */ public function flushImagesCache() { $dirs = array(); $matches = $this->_getMatchesModel()->getCollection(); $dirs[0] = array_unique($matches->getColumnValues('product_id')); $dirs[1] = array_unique($matches->getColumnValues('option_id')); $dirs[2] = array_unique($matches->getColumnValues('image')); if (file_exists($this->getBaseDir())){ $this->clearDir($this->getBaseDir(), $dirs, 0); } if(file_exists($this->getBaseDir() . DS . "cache")) { $cacheDir = $this->getBaseDir() . DS . "cache"; foreach(array_diff(scandir($cacheDir), array('.', '..')) as $sizeDir) { $this->clearDir($sizeDir, $dirs, 0); } } return true; } public function clearDir($dir, array $dataDir, $colNum) { $scanDir = array_diff(scandir($dir), array('.', '..')); foreach ($scanDir as $curDir) { if (!in_array($curDir, $dataDir[$colNum])) { (is_dir($dir . DS . $curDir)) ? $this->delTree($dir . DS . $curDir) : unlink($dir . DS . $curDir); } elseif(is_dir($dir . DS . $curDir)) { $this->clearDir($dir . DS . $curDir, $dataDir, $colNum + 1); } } return true; } public function createImagePath($productId, $optionId, $image, $size = "full") { $path = DS . $productId . DS . $optionId . DS . $image; if($size != "full" && $size != ''){ $path = DS . "cache". DS . $size . $path; } return $path; } public function getFullImageUrl($productId, $optionId, $image, $width = "full" , $keepFrame = false) { if ($productId === false || $optionId === false || $image === false){ return false; } if ($width !== "full" && Mage::getStoreConfig('colorpicker_section/image_constructor_tools/enable_cache')){ $this->resizeImg($productId, $optionId, $image, $width, $keepFrame); } return $this->getBaseUrl() . $this->createImagePath($productId, $optionId, $image, $width); } //125x125 //50x50 public function resizeImg($productId, $optionId, $image, $width, $keepFrame = false) { $basePath = $this->getBaseDir() . $this->createImagePath($productId, $optionId, $image); $newPath = $this->getBaseDir() . $this->createImagePath($productId, $optionId, $image , $width); //if width empty then return original size image's URL if ($width != '') { //if image has already resized then just return URL if (file_exists($basePath) && is_file($basePath) && !file_exists($newPath)){ try{ $imageObj = new Varien_Image($basePath); $imageObj->constrainOnly(true); $imageObj->keepAspectRatio(true); $imageObj->keepFrame($keepFrame); $imageObj->keepTransparency(true); $imageObj->resize($width, null); $imageObj->save($newPath); } catch (Exception $e){ Mage::logException($e); } } } } }