<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



class Single_file_upload {



	private $ci ;

	

	public function __construct()

	{

		$this->ci = & get_instance();

		

		$this->ci->load->library(array('upload','image_lib'));

		

	}

	

	public function do_upload($field_name ='img',$max_size='3000',$thumb='0')

	{
	

		$config = array(

				'upload_path' => './assets/images/',

				'allowed_types' => 'gif|jpg|jpeg' ,

				'max_size'	=> $max_size , // in Kb

				//'max_width'  => $max_width ,

				//'max_height'  => $max_height

				//'overwrite' => TRUE

				) ;

		

		$this->ci->upload->initialize($config);



		if ( ! $this->ci->upload->do_upload($field_name))

		{

			return array('upload'=> 0, 'errors' => $this->ci->upload->display_errors() );

		}

		else

		{

			$upload_data = $this->ci->upload->data() ;

			

			$thumb_name = '';

			

			if($thumb==1)

			{

				if($this->create_thumb($upload_data)!==FALSE)

				{

					$thumb_name = $this->create_thumb($upload_data);

				}

				else

				{

					$thumb_name = '';

				}

			}



			return array('upload'=> 1, 'filename' => $upload_data['file_name'],'thumb'=>$thumb_name );

		}

	}

	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public function do_upload_main($field_name ='img',$max_size='3000',$max_width='0',$max_height='0',$thumb='0')

	{
	

		$config = array(

				'upload_path' => './assets/images/',

				'allowed_types' => 'gif|jpg|jpeg' ,

				'max_size'	=> $max_size , // in Kb

				//'max_width'  => $max_width ,

				//'max_height'  => $max_height

				//'overwrite' => TRUE

				) ;

		

		$this->ci->upload->initialize($config);



		if ( ! $this->ci->upload->do_upload($field_name))

		{

			return array('upload'=> 0, 'errors' => $this->ci->upload->display_errors() );

		}

		else

		{

			$upload_data = $this->ci->upload->data() ;

			

			$thumb_name = '';

			

			if($thumb==1)

			{

				if($this->create_thumb($upload_data)!==FALSE)

				{

					 $thumb_name = $this->create_thumb($upload_data);

				}

				else

				{

					$thumb_name = '';

				}

			}



			return array('upload'=> 1, 'thumb'=> $thumb_name, 'filename' => $upload_data['file_name'] );

		}

	}




	

	

	public function create_thumb($upload_data=array())

	{

		$thumb_name = 'thumb_'.$upload_data['file_name'] ;

        $source= $upload_data['full_path'];      
        $width=350;
        $height=300;
        $size = getimagesize($source);
        $resize_height=($size[1]*350)/$size[0];
	   
	    $image_preheight = round($resize_height, 0);
        $dest = FALSE;
  
		// set the resize config

		$resize_conf = array(

			// it's something like "/full/path/to/the/image.jpg" maybe

			'source_image'  => $upload_data['full_path'], 

			// and it's "/full/path/to/the/" + "thumb_" + "image.jpg

			// or you can use 'create_thumbs' => true option instead

			'new_image'     => $upload_data['file_path'].$thumb_name,

			'width'         => 350,

			'height'        =>  $image_preheight,

			'maintain_ratio'=> FALSE,

			'quality'		=> 70,

			//'create_thumb' => TRUE,

			//'thumb_marker' => '_thumb',

			);

		

		

		// initializing

		$this->ci->image_lib->initialize($resize_conf);

		



		// do it!

		if ( ! $this->ci->image_lib->resize())

		{

			// if got fail.

			//$error['resize'][] = $this->image_lib->display_errors();

			return false;

		}

		else

		{

			// otherwise, put each upload data to an array.

			//$success[] = $upload_data;/

			return $thumb_name;

		}

		

	}

	



}









