fredyounan
10/25/2014 - 9:36 AM

upload.php

ini_set("memory_limit","200M");

if( !empty($_SERVER['HTTP_ORIGIN']) ){
	// Enable CORS
	header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
	header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
	header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type');
}

if( $_SERVER['REQUEST_METHOD'] == 'OPTIONS' ){
	exit;
}

$file = Input::file('filedata');

$extension = 'jpg';
$directory = 'img/avatars/temp';
$filename = sha1(time().time()).".{$extension}";
$fullPath = $directory.'/'.$filename;
$uploadSuccess = Image::make($file->getRealPath())->resize(200, 200, true, true)->resizeCanvas(200, 200, 'center', false, 'ffffff')->save($fullPath,80);

if( $uploadSuccess ) {
	$base64 = base64_encode(File::get($fullPath));
	$jsonBody = array(
		'images' => 
		array(
			'filename' => $filename,
			'mime' => $mime,
			'size' => File::size($fullPath),
			'dataURL' => 'data:'. $mime .';base64,'. $base64
		)
	);
    return Response::json($jsonBody, 200);
} else {
    return Response::json('error', 400);
}