Пример #1
0
 public function uploadAction()
 {
     if ($datas = $this->getRequest()->getParams() and !empty($_FILES)) {
         try {
             $folder = Core_Model_Directory::getTmpDirectory(true) . '/';
             $params = array();
             $params['validators'] = array('Extension' => array('jpg', 'png', 'jpeg', 'gif', 'case' => false), 'Size' => array('min' => 100, 'max' => 8000000), 'ImageSize' => array('minwidth' => 20, 'minheight' => 20, 'maxwidth' => 2000, 'maxheight' => 2000));
             $params['destination_folder'] = $folder;
             $params['uniq'] = 1;
             $params['uniq_prefix'] = '';
             //param customs
             foreach ($params['validators']['ImageSize'] as $key => $value) {
                 if (isset($datas[$key])) {
                     $params['validators']['ImageSize'][$key] = $datas[$key];
                 }
             }
             if (isset($datas['uniq_prefix'])) {
                 $params['uniq_prefix'] = $datas['uniq_prefix'];
             }
             if (isset($datas['desired_name']) && $datas['desired_name'] != '') {
                 $params['desired_name'] = $datas['desired_name'];
             }
             $uploader = new Core_Model_Lib_Uploader();
             $file = $uploader->upload($params);
             $image_sizes = getimagesize(Core_Model_Directory::getTmpDirectory(true) . '/' . $file);
             $datas = array('success' => 1, 'files' => $file, 'source_width' => $image_sizes[0], 'source_height' => $image_sizes[1]);
         } catch (Exception $e) {
             $datas = array('error' => 1, 'message' => $e->getMessage());
         }
         $this->getLayout()->setHtml(Zend_Json::encode($datas));
     }
 }
Пример #2
0
 public function uploadAction()
 {
     try {
         if (empty($_FILES) || empty($_FILES['file']['name'])) {
             throw new Exception("No file has been sent");
         }
         $base_path = Core_Model_Directory::getTmpDirectory(true);
         if (!is_dir($base_path)) {
             mkdir($base_path, 0777, true);
         }
         $params = array("validators" => array("Size" => array("min" => 100, "max" => 8000000)), "destination_folder" => $base_path, "uniq" => 1);
         $sizes = array("minwidth", "minheight", "maxwidth", "maxheight");
         foreach ($sizes as $key) {
             if ($size = $this->getRequest()->getPost($key)) {
                 $params["validators"]["ImageSize"][$key] = $size;
             }
         }
         $uploader = new Core_Model_Lib_Uploader();
         $file = $uploader->upload($params);
         if ($file) {
             $this->_sendHtml(array("success" => 1, "message" => $this->_("Your image has been successfully saved"), "filename" => $file));
         } else {
             $message = $this->_("An error occurred during the process. Please try again later.");
             throw new Exception($message);
         }
     } catch (Exception $e) {
         $data = array("error" => 1, "message" => $e->getMessage());
         $this->_sendHtml($data);
     }
 }