<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LayoutPicker
 *
 * @author messiasandre
 */
class PG_Controller_Plugin_ImageUploader2
extends Zend_Controller_Plugin_Abstract
{
    public  $file_ext ;
    public  $file_tmp ;
    public  $dir ;
    public  $file_name ;
    public  $filePrex ;
    public  $max_width ;
    public  $thum_max_width ;
    public $cropPercent;
    public $newImageWidth ;
    public $newImageHeight ;
    public $transparent = false ;
    public $grayscale = false ;
    public function Upload()
        {
         //set directory structure
    
           $uploads = UPLOADS_PATH;
           $uploadsFolder = $uploads.$this->dir;
           if(!is_dir ( $uploadsFolder))
               mkdir($uploadsFolder);
           $thunbFolder = $uploadsFolder."thumb/";
            $thunbFolderBlack = $uploadsFolder."thumb/gray/";
            if(!is_dir ( $thunbFolder))
               mkdir($thunbFolder);

               $ext = ".".substr($this->file_name, strrpos($this->file_name, '.') + 1);
               $newfilename = $this->filePrex.md5(uniqid().time()).$ext;

               $large_image_location = $uploadsFolder.'large/'.$newfilename;
			   
			   
			   
			   
              $imageUploaded = move_uploaded_file($this->file_tmp, $large_image_location); // upload the image

               if($imageUploaded){
                 
				 

                $height = $this->getHeight($large_image_location) ;
                $width = $this->getWidth($large_image_location) ;
                $max_width = $this->max_width;
                // resize uploaded image
                  $ok =false;
                if ($width > $max_width){
				$scale = $max_width/$width;
				$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);

			}else{
				$scale = 1;
				$uploaded = $this->resizeImage($large_image_location,$width,$height,$scale);

			}
                        $scale = 90/$width;
                    $thumbOk = $this->resizeThumbnailImage($thunbFolder.$newfilename, $large_image_location, $width, $height, $scale);
                    if($thumbOk){
                        if($this->grayscale == true){
                        //gray scale image genarator
                        $im = $this->imagecreateFrom($thunbFolder.$newfilename,$this->file_ext);

                        if($im && imagefilter($im, IMG_FILTER_GRAYSCALE))
                        {
                            $this->imagetype($im, $thunbFolderBlack.$newfilename,$this->file_ext);
                        }
                        }
                    }
               return $newfilename;

               }else{
               	echo "eroor";
               }


    }


    private function imagecreateFrom($filename,$ext){

        switch ($ext){
           case 'image/jpeg':
                 return imagecreatefromjpeg($filename);

           case 'image/gif':
                 return imagecreatefromgif($filename);

           case 'image/png':
                 return imagecreatefrompng($filename);
        }
        
    }

    private function imagetype($im,$filename,$ext){

        switch ($ext){
           case 'image/jpeg':
                 return imagejpeg($im,$filename);

           case 'image/gif':
                 return imagegif($im,$filename);

           case 'image/png':
                 return imagepng($im,$filename);
        }
        
    }

    private  function resizeImage($image,$width,$height,$scale) {
	$newImageWidth = ceil($width * $scale);
	$newImageHeight = ceil($height * $scale);
	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
	$source = $this->imagecreateFrom($image,$this->file_ext);
if($this->transparent ==true){
        $this->setTransparency($newImage,$source);
}
	imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
	//imagepng($newImage,$image,90);
	chmod($image, 0777);
	return $image;
       }

//You do not need to alter these functions
   private  function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $scale){


//getting the image dimensions
   list($width, $height) = getimagesize($image);

       if($width > $height) $biggestSide = $width; //find biggest length
       else $biggestSide = $height;

//The crop size will be half that of the largest side
   $cropPercent = $this->cropPercent; // This will zoom in to 50% zoom (crop)
   $cropWidth   = $biggestSide*$cropPercent;
   $cropHeight  = $biggestSide*$cropPercent;


//getting the top left coordinate
   $x = ($width-$cropWidth)/2;
   $y = ($height-$cropHeight)/2;

  
	
	$max_width = $this->newImageWidth;
$max_height = $this->newImageHeight;

 list($width, $height) = getimagesize($image);
$ratioh = $max_height/$height;
$ratiow = $max_width/$width;
$ratio = min($ratioh, $ratiow);
// New dimensions
$newImageWidth = intval($ratio*$width);
$newImageHeight = intval($ratio*$height); 
	
	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
	//create image from the jpeg
        $source = $this->imagecreateFrom($image,$this->file_ext);
        if($this->transparent ==true){
        $this->setTransparency($newImage,$source);
        }
	imagecopyresampled($newImage,$source,0,0,$x,$y,$newImageWidth,$newImageHeight,$cropWidth,$cropHeight);

        $this->imagetype($newImage,$thumb_image_name,$this->file_ext);
	chmod($thumb_image_name, 0777);
	return $thumb_image_name;
}
//You do not need to alter these functions
   private  function getHeight($image) {
	$sizes = getimagesize($image);
	$height = $sizes[1];
	return $height;
}
//You do not need to alter these functions
   private  function getWidth($image) {
	$sizes = getimagesize($image);
	$width = $sizes[0];
	return $width;
}

//get the trasparancy
private function setTransparency($new_image,$image_source)
           {

            $transparencyIndex = imagecolortransparent($image_source);
            $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);

            if ($transparencyIndex >= 0) {
                $transparencyColor    = imagecolorsforindex($image_source, $transparencyIndex);
            }

            $transparencyIndex    = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']);
            imagefill($new_image, 0, 0, $transparencyIndex);
             imagecolortransparent($new_image, $transparencyIndex);

           }

}
?>
