Skip to content

Instantly share code, notes, and snippets.

@notflip
Created October 13, 2015 14:15
Show Gist options
  • Save notflip/c335f55099d7dde5437a to your computer and use it in GitHub Desktop.
Save notflip/c335f55099d7dde5437a to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use File;
use Image;
class UploadController extends Controller {
public function upload(Request $request)
{
$file = $request->file('file');
$rules = ['file' => 'image|max:2000'];
$allowed = ['jpg','jpeg','png'];
$tempPath = '/uploads/temp/';
$originalPath = 'uploads/';
$validator = \Validator::make([$file], $rules);
if($validator->fails()) {
return response()->json("error", 500);
}
$extension = strtolower($file->getClientOriginalExtension());
if($file) {
if(!in_array($extension, $allowed)) {
return response()->json("Gelieve een afbeelding te selecteren", 400);
}
$filename = sha1(str_random()) . ".$extension";
var_dump($filename);
$destination = public_path() . $tempPath;
if($upload = $file->move($destination, $filename)) {
$image = Image::make($destination . $filename);
$width = $image->width();
$height = $image->height();
if($width >= $height) {
$image->resize(750, null, function($constraint) {
$constraint->aspectRatio();
})->save($originalPath.$filename);
} else {
$image->resize(null, 750, function($constraint) {
$constraint->aspectRatio();
})->save($originalPath.$filename);
}
}
} else {
return response()->json("error", 400);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment