Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Session;
- use Illuminate\Support\Facades\Redirect;
- use Illuminate\Support\Facades\Storage;
- use Intervention\Image\ImageManager;
- use Ramsey\Uuid\Uuid;
- use App\Images;
- use \diversen\gps;
- class UploadController extends Controller
- {
- //
- private $model;
- private $maxfilesize = 10000000;
- private $error = false;
- private $tempfilepath;
- public function __construct(Request $request)
- {
- $this->img = new ImageManager;
- $this->model = new Images;
- $this->request = $request;
- $this->storage = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
- }
- public function uploadForm()
- {
- $data['max_size'] = $this->maxfilesize / 1024 . ' MB';
- $data['error'] = Session::get('error');
- return view('upload', $data);
- }
- public function save(){
- $this->tempfilepath = $this->request->file('image')->getPathName();
- if(!$this->request->hasFile('image')) {
- $this->error = 'Error: No file included';
- }
- if($this->request->file('image')->getSize() > config('image.max_size_in_bytes')) {
- $this->error = 'File too large!';
- }
- if(!$this->getOrientation()){
- $this->error = 'Error: Could not get dimensions';
- }
- // If there's an error, redirect back with messagae
- if (!$this->error == false) {
- Session::flash('message', $this->error);
- Session::flash('alert-class', 'alert-danger');
- return Redirect::back();
- }
- $this->getGps();
- $this->saveExtendedData();
- $this->saveToDatabase();
- // Save fullsize PNG
- $this->img->make($this->tempfilepath)->save($this->storage . 'original/' . $this->model->hash . '.png');
- // Save thumbnail
- $thumb = $this->resize($this->storage . 'original/' . $this->model->hash . '.png', Config('targets.thumb.x'));
- $thumb->save(storage_path() . '/app/public/thumbnail/' . $this->model->hash . '.jpg', 70);
- // Save screensized
- $screen = $this->resize($this->storage . 'original/' . $this->model->hash . '.png', Config('targets.iphone6s.x'));
- $screen->save(storage_path() . '/app/public/screen/' . $this->model->hash . '.jpg', 70);
- // header ('Content-type: Text/plain');
- // return $this->model->id;
- Session::flash('message', $this->model->hash);
- Session::flash('alert-class', 'alert-success');
- return Redirect('/upload');
- }
- private function getGps(){
- $g = new gps();
- $gps = $g->getGpsPosition($this->tempfilepath);
- if(empty($gps)){
- $this->model->latitude = null;
- $this->model->longitude = null;
- return false;
- }
- $this->model->latitude = $gps['latitude'];
- $this->model->longitude = $gps['longitude'];
- return true;
- }
- private function getOrientation(){
- $info = getimagesize($this->tempfilepath);
- $this->model->width = $info[0];
- $this->model->height = $info[1];
- $this->model->orientation = 'square';
- if ($info[0] > $info[1]) {
- $this->model->orientation = 'portrait';
- }
- if ($info[0] < $info[1]){
- $this->model->orientation = 'landscape';
- }
- return $info;
- }
- private function saveToDatabase(){
- $this->model->filename = Uuid::uuid4()->toString();
- $this->model->hash = hash_file('sha256', $this->tempfilepath);
- $this->model->original_name = $this->request->file('image')->getClientOriginalName();
- $this->model->save();
- return $this->model->id;
- }
- private function saveExtendedData(){
- $exif = $this->img->make($this->tempfilepath)->exif();
- if($exif['Make']){
- $this->model->camera_make = $exif['Make'];
- }
- if($exif['Model']){
- $this->model->camera_model = $exif['Model'];
- }
- if($exif['DateTime']) {
- $this->model->date_taken = $exif['DateTime'];
- }
- if($exif['FNumber']){
- $fstop = explode('/', $exif['FNumber']);
- $this->model->fstop = $fstop[0]/$fstop[1];
- }
- if($exif['ExposureTime']){
- $this->model->shutter_speed = $exif['ExposureTime'];
- }
- }
- private function resize($image, $width){
- $img = $this->img->make($image);
- return $img->resize($width, null, function ($constraint) {
- $constraint->aspectRatio();
- $constraint->upsize();
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement