/** * Handler for pictures/video upload interface * @return array */ private function _uploadImages($savePath = null, $resize = true) { $miscConfig = Zend_Registry::get('misc'); if (!$savePath) { //useful if file submited directly to this method $savePath = $this->_getSavePath(); } $this->_uploadHandler->clearValidators()->addValidator('Extension', false, array('jpeg', 'jpg', 'png', 'gif'))->addValidator('ImageSize', false, array('maxwidth' => $miscConfig['imgMaxWidth'], 'maxheight' => $miscConfig['imgMaxWidth'])); if ($this->_checkMime) { $this->_uploadHandler->addValidator(new Validators_MimeType(array('image/gif', 'image/jpeg', 'image/jpg', 'image/png')), false); } $receivePath = $resize ? $savePath . DIRECTORY_SEPARATOR . 'original' : $savePath; if ($this->_uploadHandler->isUploaded() && $this->_uploadHandler->isValid()) { if (!is_dir($receivePath)) { try { Tools_Filesystem_Tools::mkDir($receivePath); } catch (Exceptions_SeotoasterException $e) { error_log($e->getMessage()); } } if (!$this->_uploadHandler->hasFilter('Rename')) { /** * Renaming file if additional field 'name' was submited with file */ $filterChain = new Zend_Filter(); $filterChain->addFilter(new Zend_Filter_StringTrim())->addFilter(new Zend_Filter_StringToLower('UTF-8'))->addFilter(new Zend_Filter_PregReplace(array('match' => '/[^\\w\\d_]+/u', 'replace' => '-'))); // filtering the img name $expFileName = explode('.', $this->getRequest()->getParam('name', false)); $fileExt = array_pop($expFileName); $name = implode($expFileName); $newName = $filterChain->filter($name) . '.' . $fileExt; if (false !== $newName) { $this->_uploadHandler->addFilter('Rename', array('target' => $receivePath . DIRECTORY_SEPARATOR . $newName, 'overwrite' => true)); } else { $this->_uploadHandler->addFilter('Rename', array('target' => $receivePath, 'overwite' => true)); } } if ($this->_uploadHandler->receive()) { $fileInfo = current($this->_uploadHandler->getFileInfo()); } else { return array('error' => true, 'result' => $this->_uploadHandler->getMessages()); } if ($resize) { $status = Tools_Image_Tools::batchResize($fileInfo['tmp_name'], $savePath); } else { $status = true; } if (isset($this->_helper->session->imageQualityPreview)) { unset($this->_helper->session->imageQualityPreview); Tools_Image_Tools::optimizeImage($fileInfo['tmp_name'], self::PREVIEW_IMAGE_OPTIMIZE); } if (isset($this->_helper->session->imageQuality)) { Tools_Image_Tools::optimizeOriginalImage($fileInfo['tmp_name'], $savePath, $this->_helper->session->imageQuality); } return array('error' => $status !== true, 'result' => $status); } return array('error' => true, 'result' => $this->_uploadHandler->getMessages()); }
protected function _applyMedia($themeName = false) { if (!$themeName) { $themeName = $this->_configHelper->getConfig('currentTheme'); } $toasterRoot = $this->_websiteHelper->getPath(); $themePath = $toasterRoot . $this->_themesConfig['path'] . $themeName; $themeMediaPath = $themePath . DIRECTORY_SEPARATOR . $this->_websiteHelper->getMedia(); $themePageTeasersPath = $themePath . DIRECTORY_SEPARATOR . $this->_websiteHelper->getPreview(); //processing images from media folder if (is_dir($themeMediaPath)) { $mediaFiles = glob($themeMediaPath . join(DIRECTORY_SEPARATOR, array('*', '*.{jpeg,jpg,png,gif}')), GLOB_BRACE); $toasterMedia = $toasterRoot . $this->_websiteHelper->getMedia(); foreach ($mediaFiles as $originalFile) { $filepath = str_replace($themeMediaPath, '', $originalFile); $filepath = explode(DIRECTORY_SEPARATOR, $filepath); if (!is_array($filepath)) { continue; } list($folderName, $fileName) = $filepath; $destFolderPath = $toasterMedia . $folderName; if (!is_dir($destFolderPath)) { if (Tools_Filesystem_Tools::mkDir($destFolderPath)) { Tools_Filesystem_Tools::mkDir($destFolderPath . DIRECTORY_SEPARATOR . 'original'); } } $destImgPath = $destFolderPath . DIRECTORY_SEPARATOR . 'original' . DIRECTORY_SEPARATOR . $fileName; if (Tools_Filesystem_Tools::copy($originalFile, $destImgPath, true)) { Tools_Image_Tools::batchResize($destImgPath, $destFolderPath); } unset($filepath, $destFolderPath, $folderName, $fileName); } } //processing page preview images if (is_dir($themePageTeasersPath)) { $destinationPreview = $toasterRoot . $this->_websiteHelper->getPreview(); if (!is_dir($destinationPreview)) { Tools_Filesystem_Tools::mkDir($destinationPreview); } Tools_Filesystem_Tools::copy($themePageTeasersPath, $destinationPreview, array(), true); } }