Example #1
0
 /**
  * This function is used to upload an image after resizing it.
  * @return string On success file name,on failure error message
  */
 public static function imageUploadAfterResize()
 {
     $max_size = 250;
     // maxim size for image file, in KiloBytes
     $allowtype = array('pdf', 'docx', 'rtf', 'odx', 'doc', 'txt', 'odt');
     /** Uploading the image **/
     $rezultat = '';
     $result_status = '';
     $result_msg = '';
     $type = end(explode(".", strtolower($_FILES['form_attachment']['name'])));
     if (in_array($type, $allowtype)) {
         if ($_FILES['form_attachment']['size'] <= $max_size * 1000) {
             //File Upload
             $logo = "";
             $path = FORM_ATTACHMENT_PREVIEW_PATH;
             $upload = new Zend_File_Transfer_Adapter_Http();
             $upload->setDestination($path);
             //Constructor needs one parameter, the destination path is a good idea
             $renameFilter = new Zend_Filter_File_Rename($path);
             $files = $upload->getFileInfo();
             $logo = "";
             foreach ($files as $fileID => $fileInfo) {
                 if (!$fileInfo['name'] == '') {
                     $varaible = explode('.', $fileInfo['name']);
                     $extension = end($varaible);
                     $logo = md5(uniqid(rand(), true)) . '.' . $extension;
                     $renameFilter->addFile(array('source' => $fileInfo['tmp_name'], 'target' => $logo, 'overwrite' => true));
                 }
                 // add filters to Zend_File_Transfer_Adapter_Http
                 $upload->addFilter($renameFilter);
                 // receive all files
                 try {
                     $upload->receive();
                     //Image Resize
                     $frontobj = Zend_Controller_Front::getInstance();
                     if ($frontobj->getRequest()->getParam('form_attachment') != '') {
                         $image = new Zend_Resize($path . '/' . $logo);
                         $image->resizeImage(84, 84, 'crop');
                         $image->saveImage($path . '/' . $logo, 100);
                     }
                     //End Image Resize
                 } catch (Zend_File_Transfer_Exception $e) {
                     $rezultat = '';
                     $result_status = 'error';
                     $result_msg = $e->getMessage();
                 }
                 //upto here
                 $rezultat = $logo;
                 $result_status = 'success';
                 $result_msg = 'Uploaded Succesfully.';
             }
             //End File Upload
         } else {
             $rezultat = '';
             $result_status = 'error';
             $result_msg = 'The file exceeds the maximum permitted size ' . $max_size . ' KB.';
         }
     } else {
         $rezultat = '';
         $result_status = 'error';
         $result_msg = 'The file ' . $filename . ' has not an allowed extension.';
     }
     $result = array('result' => $result_status, 'img' => $rezultat, 'msg' => $result_msg);
     return $result;
 }
 public function imageupload()
 {
     $savefolder = USER_PREVIEW_UPLOAD_PATH;
     // folder for upload
     $max_size = 1024;
     // maxim size for image file, in KiloBytes
     // Allowed image types
     $allowtype = array('gif', 'jpg', 'jpeg', 'png');
     /** Uploading the image **/
     $rezultat = '';
     $result_status = '';
     $result_msg = '';
     // if is received a valid file
     if (isset($_FILES['profile_photo'])) {
         // checks to have the allowed extension
         $type = explode(".", strtolower($_FILES['profile_photo']['name']));
         if (in_array($type[1], $allowtype)) {
             // check its size
             if ($_FILES['profile_photo']['size'] <= $max_size * 1024) {
                 // if no errors
                 if ($_FILES['profile_photo']['error'] == 0) {
                     $newname = 'preview_' . date("His") . '.' . $type[1];
                     $thefile = $savefolder . "/" . $_FILES['profile_photo']['name'];
                     $newfilename = $savefolder . "/" . $newname;
                     $filename = $newname;
                     if (!move_uploaded_file($_FILES['profile_photo']['tmp_name'], $newfilename)) {
                         $rezultat = '';
                         $result_status = 'error';
                         $result_msg = 'The file cannott be uploaded, try again.';
                     } else {
                         $rezultat = $filename;
                         $image = new Zend_Resize($newfilename);
                         $image->resizeImage(200, 200, 'crop');
                         $image->saveImage($newfilename, 100);
                         $result_status = 'success';
                         $result_msg = '';
                     }
                 }
             } else {
                 $rezultat = '';
                 $result_status = 'error';
                 $result_msg = 'The file exceeds the maximum permitted size ' . $max_size . ' KB.';
             }
         } else {
             $rezultat = '';
             $result_status = 'error';
             $result_msg = 'Please upload only .gif, .jpg, .jpeg, .png images.';
         }
     } else {
         $rezultat = '';
         $result_status = 'error';
         $result_msg = 'Please upload only .gif, .jpg, .jpeg, .png images.';
     }
     $result = array('result' => $result_status, 'img' => $rezultat, 'msg' => $result_msg);
     return $result;
 }