<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LayoutPicker
 *
 * @author messiasandre
 */
class PG_Controller_Plugin_ImageUploader
extends Zend_Controller_Plugin_Abstract
{
	
	
	    public  $fileName ;
  	    public  $fileTmpLoc ;
		public  $fileType ;
		public  $fileSize;
		public  $fileErrorMsg;
		public  $resize ;
		public  $resizeMxWidth;
		public  $resizeMxHeight;
		public  $thumb ;
		public  $thumbMxWidth;
		public  $thumbMxHeight;
		
	    private  $dir =  "/gallery/";
	
   public function uploadimage(){
			   	  $uploads = UPLOADS_PATH;
				  $uploadsFolder = $uploads.$this->dir;
				  $kaboom = explode(".", $this->fileName); // Split file name into an array using the dot
				  $fileExt = end($kaboom); // Now target the last array element to get the file extension
				  
				  
				  // START PHP Image Upload Error Handling --------------------------------------------------
					if (!$this->fileTmpLoc) { // if file not chosen
					    echo "<p class='ErrorResponce'>ERROR: Please browse for a file.</p>";
					    exit();
					} else if($this->fileSize > 5242880) { // if file size is larger than 5 Megabytes
					    echo "<p class='ErrorResponce'>ERROR: Your file was larger than 5 MB</p>";
					    unlink($this->fileTmpLoc); // Remove the uploaded file from the PHP temp folder
					    exit();
					} else if (!preg_match("/.(gif|jpg|png)$/i", $this->fileName) ) {
					     // This condition is only if you wish to allow uploading of specific file types    
					     echo "<p class='ErrorResponce'>ERROR: Your image was not .gif, .jpg, or .png.</p>";
					     unlink($this->fileTmpLoc); // Remove the uploaded file from the PHP temp folder
					     exit();
					} else if ($this->fileErrorMsg == 1) { // if file upload error key is equal to 1
					    echo "<p class='ErrorResponce'>ERROR: An error occured. Try again.</p>";
					    exit();
					}
						
						$newfilename = $this->filePrex.md5(uniqid().time()).".".$fileExt;
					// END PHP Image Upload Error Handling ---------------------------------
					// Place it into your "uploads" folder mow using the move_uploaded_file() function
					$moveResult = move_uploaded_file($this->fileTmpLoc, $uploadsFolder.$newfilename);
					// Check to make sure the move result is true before continuing
					if ($moveResult != true) {
					    echo "ERROR: File not uploaded. Try again.";
					    unlink($this->fileTmpLoc); // Remove the uploaded file from the PHP temp folder
					    exit();
					}
					unlink($this->fileTmpLoc); // Remove the uploaded file from the PHP temp folder
					if($this->resize ==true){
						$target_file = $uploadsFolder.$newfilename;
					$resized_file = $uploadsFolder."main_".$newfilename;
					$wmax = 814;
					$hmax = 302;
					$this->ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
					}
					if($this->resize ==true){
						$target_file = $uploadsFolder.$newfilename;
					$resized_resize = $uploadsFolder."resize_".$newfilename;
					$wmax = 300;
					$hmax = 300;
					$true = $this->ak_img_resize($target_file, $resized_resize, $wmax, $hmax, $fileExt);
							  if(file_exists($resized_resize)){
							$target_file = $resized_resize;
							$thumbnail = $uploadsFolder."thumb_".$newfilename;
							$wthumb = 150;
							$hthumb = 150;
							$this->ak_img_thumb($target_file, $thumbnail, $wthumb, $hthumb, $fileExt);
							}
					}
					
					return $newfilename;
					unlink($uploadsFolder.$newfilename);
   	
				
			
			
			 		
   }


private function ak_img_resize($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $scale_ratio = $w_orig / $h_orig;
    if (($w / $h) > $scale_ratio) {
           $w = $h * $scale_ratio;
    } else {
           $h = $w / $scale_ratio;
    }
    $img = "";
    $ext = strtolower($ext);
    if ($ext == "gif"){ 
      $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
      $img = imagecreatefrompng($target);
    } else { 
      $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    // imagecopyresampled(dst_img, src_img, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)
    imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig);
    imagejpeg($tci, $newcopy, 80);
}


private function ak_img_thumb($target, $newcopy, $w, $h, $ext) {
    list($w_orig, $h_orig) = getimagesize($target);
    $src_x = ($w_orig / 2) - ($w / 2);
    $src_y = ($h_orig / 2) - ($h / 2);
    $ext = strtolower($ext);
    $img = "";
    if ($ext == "gif"){ 
    $img = imagecreatefromgif($target);
    } else if($ext =="png"){ 
    $img = imagecreatefrompng($target);
    } else { 
    $img = imagecreatefromjpeg($target);
    }
    $tci = imagecreatetruecolor($w, $h);
    imagecopyresampled($tci, $img, 0, 0, $src_x, $src_y, $w, $h, $w, $h);
    if ($ext == "gif"){ 
        imagegif($tci, $newcopy);
    } else if($ext =="png"){ 
        imagepng($tci, $newcopy);
    } else { 
        imagejpeg($tci, $newcopy, 84);
    }
}

}
?>
