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

class UploadImage extends CI_Controller {
	
	public function __construct(){
		parent::__construct();
	
	}

	public function index(){
		if(isset($_FILES['image'])){
			//Get the image array of details
			$dir = explode("\\",dirname(__FILE__));
            $pathPrefix = implode("\\",array_slice($dir, 0, array_search("application", $dir))).'\ug_images\\upload\\'; 
			$img = $_FILES['image'];   
			//The new path of the uploaded image, rand is just used for the sake of it
			$imgLink = base_url().'ug_images/upload/';
			$imgName = time().$img["name"];
			//Move the file to our new path
			move_uploaded_file($img['tmp_name'],$pathPrefix.$imgName);
			//The direct link to the uploaded image, this might varyu depending on your script location    
			$link = base_url().'ug_images/upload/'.$imgName;
			//Get image info, reuiqred to biuld the JSON object
			$data = getimagesize($link);
			
			//Here we are constructing the JSON Object
			$res = array("upload" => array(
									"links" => array("original" => $link),
									"image" => array("width" => $data[0],
													 "height" => $data[1]
													)                              
						));
			//echo out the response :)
			echo json_encode($res);
		}
	}
}