echo "ERROR: File not uploaded. Try again.";
    exit;
}
// Include the file that houses all of our custom image functions
include_once "ak_php_img_lib_1.0.php";
// ---------- Start Universal Image Resizing Function --------
$target_file = "uploads/{$fileName}";
$resized_file = "uploads/resized_{$fileName}";
$wmax = 500;
$hmax = 500;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
// ----------- End Universal Image Resizing Function ----------
// ---------- Start Convert to JPG Function --------
if (strtolower($fileExt) != "jpg") {
    $target_file = "uploads/resized_{$fileName}";
    $new_jpg = "uploads/resized_" . $kaboom[0] . ".jpg";
    ak_img_convert_to_jpg($target_file, $new_jpg, $fileExt);
}
// ----------- End Convert to JPG Function -----------
// ---------- Start Image Watermark Function --------
$target_file = "uploads/resized_" . $kaboom[0] . ".jpg";
$wtrmrk_file = "watermark.png";
$new_file = "uploads/protected_" . $kaboom[0] . ".jpg";
ak_img_watermark($target_file, $wtrmrk_file, $new_file);
// ----------- End Image Watermark Function -----------
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>{$fileName}</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>{$fileSize}</strong> bytes in size.<br /><br />";
echo "It is an <strong>{$fileType}</strong> type of file.<br /><br />";
echo "The file extension is <strong>{$fileExt}</strong><br /><br />";
echo "The Error Message output for this upload is: {$fileErrorMsg}";
 public function processpictureAction()
 {
     // disable rendering of the view and layout so that we can just echo the AJAX output
     $this->_helper->layout->disableLayout();
     $this->_helper->viewRenderer->setNoRender(TRUE);
     $session = SessionWrapper::getInstance();
     $config = Zend_Registry::get("config");
     $this->_translate = Zend_Registry::get("translate");
     $formvalues = $this->_getAllParams();
     //debugMessage($this->_getAllParams());
     $user = new UserAccount();
     $user->populate(decode($this->_getParam('id')));
     // only upload a file if the attachment field is specified
     $upload = new Zend_File_Transfer();
     // set the file size in bytes
     $upload->setOptions(array('useByteString' => false));
     // Limit the extensions to the specified file extensions
     $upload->addValidator('Extension', false, $config->uploads->photoallowedformats);
     $upload->addValidator('Size', false, $config->uploads->photomaximumfilesize);
     // base path for profile pictures
     $destination_path = BASE_PATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . "users" . DIRECTORY_SEPARATOR . "user_";
     // determine if user has destination avatar folder. Else user is editing there picture
     if (!is_dir($destination_path . $user->getID())) {
         // no folder exits. Create the folder
         mkdir($destination_path . $user->getID(), 0777);
     }
     // set the destination path for the image
     $profilefolder = $user->getID();
     $destination_path = $destination_path . $profilefolder . DIRECTORY_SEPARATOR . "avatar";
     if (!is_dir($destination_path)) {
         mkdir($destination_path, 0777);
     }
     // create archive folder for each user
     $archivefolder = $destination_path . DIRECTORY_SEPARATOR . "archive";
     if (!is_dir($archivefolder)) {
         mkdir($archivefolder, 0777);
     }
     $oldfilename = $user->getProfilePhoto();
     //debugMessage($destination_path);
     $upload->setDestination($destination_path);
     // the profile image info before upload
     $file = $upload->getFileInfo('profileimage');
     $uploadedext = findExtension($file['profileimage']['name']);
     $currenttime = time();
     $currenttime_file = $currenttime . '.' . $uploadedext;
     $thefilename = $destination_path . DIRECTORY_SEPARATOR . 'base_' . $currenttime_file;
     $thelargefilename = $destination_path . DIRECTORY_SEPARATOR . 'large_' . $currenttime_file;
     $updateablefile = $destination_path . DIRECTORY_SEPARATOR . 'base_' . $currenttime;
     $updateablelarge = $destination_path . DIRECTORY_SEPARATOR . 'large_' . $currenttime;
     //debugMessage($thefilename);
     // rename the base image file
     $upload->addFilter('Rename', array('target' => $thefilename, 'overwrite' => true));
     // exit();
     // process the file upload
     if ($upload->receive()) {
         // debugMessage('Completed');
         $file = $upload->getFileInfo('profileimage');
         // debugMessage($file);
         $basefile = $thefilename;
         // convert png to jpg
         if (in_array(strtolower($uploadedext), array('png', 'PNG', 'gif', 'GIF'))) {
             ak_img_convert_to_jpg($thefilename, $updateablefile . '.jpg', $uploadedext);
             unlink($thefilename);
         }
         $basefile = $updateablefile . '.jpg';
         // new profilenames
         $newlargefilename = "large_" . $currenttime_file;
         // generate and save thumbnails for sizes 250, 125 and 50 pixels
         resizeImage($basefile, $destination_path . DIRECTORY_SEPARATOR . 'large_' . $currenttime . '.jpg', 400);
         resizeImage($basefile, $destination_path . DIRECTORY_SEPARATOR . 'medium_' . $currenttime . '.jpg', 165);
         // unlink($thefilename);
         unlink($destination_path . DIRECTORY_SEPARATOR . 'base_' . $currenttime . '.jpg');
         // exit();
         // update the user with the new profile images
         try {
             $user->setProfilePhoto($currenttime . '.jpg');
             $user->save();
             // check if user already has profile picture and archive it
             $ftimestamp = current(explode('.', $user->getProfilePhoto()));
             $allfiles = glob($destination_path . DIRECTORY_SEPARATOR . '*.*');
             $currentfiles = glob($destination_path . DIRECTORY_SEPARATOR . '*' . $ftimestamp . '*.*');
             // debugMessage($currentfiles);
             $deletearray = array();
             foreach ($allfiles as $value) {
                 if (!in_array($value, $currentfiles)) {
                     $deletearray[] = $value;
                 }
             }
             // debugMessage($deletearray);
             if (count($deletearray) > 0) {
                 foreach ($deletearray as $afile) {
                     $afile_filename = basename($afile);
                     rename($afile, $archivefolder . DIRECTORY_SEPARATOR . $afile_filename);
                 }
             }
             $session->setVar(SUCCESS_MESSAGE, $this->_translate->translate("global_update_success"));
             $this->_helper->redirector->gotoUrl($this->view->baseUrl("profile/picture/id/" . encode($user->getID()) . '/crop/1'));
         } catch (Exception $e) {
             $session->setVar(ERROR_MESSAGE, $e->getMessage());
             $session->setVar(FORM_VALUES, $this->_getAllParams());
             $this->_helper->redirector->gotoUrl($this->view->baseUrl('profile/picture/id/' . encode($user->getID())));
         }
     } else {
         // debugMessage($upload->getMessages());
         $uploaderrors = $upload->getMessages();
         $customerrors = array();
         if (!isArrayKeyAnEmptyString('fileUploadErrorNoFile', $uploaderrors)) {
             $customerrors['fileUploadErrorNoFile'] = "Please browse for image on computer";
         }
         if (!isArrayKeyAnEmptyString('fileExtensionFalse', $uploaderrors)) {
             $custom_exterr = sprintf($this->_translate->translate('global_invalid_ext_error'), $config->uploads->photoallowedformats);
             $customerrors['fileExtensionFalse'] = $custom_exterr;
         }
         if (!isArrayKeyAnEmptyString('fileUploadErrorIniSize', $uploaderrors)) {
             $custom_exterr = sprintf($this->_translate->translate('global_invalid_size_error'), formatBytes($config->uploads->photomaximumfilesize, 0));
             $customerrors['fileUploadErrorIniSize'] = $custom_exterr;
         }
         if (!isArrayKeyAnEmptyString('fileSizeTooBig', $uploaderrors)) {
             $custom_exterr = sprintf($this->_translate->translate('global_invalid_size_error'), formatBytes($config->uploads->photomaximumfilesize, 0));
             $customerrors['fileSizeTooBig'] = $custom_exterr;
         }
         $session->setVar(ERROR_MESSAGE, 'The following errors occured <ul><li>' . implode('</li><li>', $customerrors) . '</li></ul>');
         $session->setVar(FORM_VALUES, $this->_getAllParams());
         $this->_helper->redirector->gotoUrl($this->view->baseUrl('profile/picture/id/' . encode($user->getID())));
     }
     // exit();
 }