Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.75 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Session;
  7. use Illuminate\Support\Facades\Redirect;
  8. use Illuminate\Support\Facades\Storage;
  9. use Intervention\Image\ImageManager;
  10. use Ramsey\Uuid\Uuid;
  11. use App\Images;
  12. use \diversen\gps;
  13.  
  14. class UploadController extends Controller
  15. {
  16.     //
  17.     private $model;
  18.     private $maxfilesize = 10000000;
  19.     private $error = false;
  20.     private $tempfilepath;
  21.  
  22.     public function __construct(Request $request)
  23.     {
  24.         $this->img  = new ImageManager;
  25.         $this->model = new Images;
  26.         $this->request = $request;
  27.         $this->storage = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
  28.     }
  29.  
  30.     public function uploadForm()
  31.     {
  32.  
  33.         $data['max_size'] = $this->maxfilesize / 1024 . ' MB';
  34.         $data['error'] = Session::get('error');
  35.  
  36.         return view('upload', $data);
  37.     }
  38.  
  39.     public function save(){
  40.         $this->tempfilepath = $this->request->file('image')->getPathName();
  41.  
  42.         if(!$this->request->hasFile('image')) {
  43.             $this->error = 'Error: No file included';
  44.         }
  45.         if($this->request->file('image')->getSize() > config('image.max_size_in_bytes')) {
  46.             $this->error = 'File too large!';
  47.         }
  48.         if(!$this->getOrientation()){
  49.             $this->error = 'Error: Could not get dimensions';
  50.         }
  51.  
  52.         // If there's an error, redirect back with messagae
  53.         if (!$this->error == false) {
  54.             Session::flash('message', $this->error);
  55.             Session::flash('alert-class', 'alert-danger');
  56.             return Redirect::back();
  57.         }
  58.  
  59.         $this->getGps();
  60.         $this->saveExtendedData();
  61.         $this->saveToDatabase();
  62.  
  63.         // Save fullsize PNG
  64.         $this->img->make($this->tempfilepath)->save($this->storage . 'original/' . $this->model->hash . '.png');
  65.  
  66.         // Save thumbnail
  67.         $thumb = $this->resize($this->storage . 'original/' . $this->model->hash . '.png', Config('targets.thumb.x'));
  68.         $thumb->save(storage_path() . '/app/public/thumbnail/' . $this->model->hash . '.jpg', 70);
  69.  
  70.         // Save screensized
  71.         $screen = $this->resize($this->storage . 'original/' . $this->model->hash . '.png', Config('targets.iphone6s.x'));
  72.         $screen->save(storage_path() . '/app/public/screen/' . $this->model->hash . '.jpg', 70);
  73.  
  74. //        header ('Content-type: Text/plain');
  75. //        return $this->model->id;
  76.  
  77.         Session::flash('message', $this->model->hash);
  78.         Session::flash('alert-class', 'alert-success');
  79.        
  80.         return Redirect('/upload');
  81.      }
  82.  
  83.      private function getGps(){
  84.         $g = new gps();
  85.         $gps = $g->getGpsPosition($this->tempfilepath);
  86.         if(empty($gps)){
  87.             $this->model->latitude = null;
  88.             $this->model->longitude = null;
  89.             return false;
  90.         }
  91.         $this->model->latitude = $gps['latitude'];
  92.         $this->model->longitude = $gps['longitude'];
  93.  
  94.         return true;
  95.      }
  96.  
  97.      private function getOrientation(){
  98.         $info = getimagesize($this->tempfilepath);
  99.         $this->model->width = $info[0];
  100.         $this->model->height = $info[1];
  101.         $this->model->orientation = 'square';
  102.         if ($info[0] > $info[1]) {
  103.             $this->model->orientation = 'portrait';
  104.         }
  105.         if ($info[0] < $info[1]){
  106.             $this->model->orientation = 'landscape';
  107.         }
  108.         return $info;
  109.     }
  110.  
  111.      private function saveToDatabase(){
  112.         $this->model->filename = Uuid::uuid4()->toString();
  113.         $this->model->hash = hash_file('sha256', $this->tempfilepath);
  114.         $this->model->original_name = $this->request->file('image')->getClientOriginalName();
  115.         $this->model->save();
  116.         return $this->model->id;
  117.      }
  118.  
  119.      private function saveExtendedData(){
  120.         $exif = $this->img->make($this->tempfilepath)->exif();
  121.  
  122.         if($exif['Make']){
  123.             $this->model->camera_make = $exif['Make'];
  124.         }
  125.  
  126.         if($exif['Model']){
  127.             $this->model->camera_model = $exif['Model'];
  128.         }
  129.  
  130.         if($exif['DateTime']) {
  131.             $this->model->date_taken = $exif['DateTime'];
  132.         }
  133.  
  134.         if($exif['FNumber']){
  135.             $fstop = explode('/', $exif['FNumber']);
  136.             $this->model->fstop = $fstop[0]/$fstop[1];
  137.         }
  138.  
  139.         if($exif['ExposureTime']){
  140.             $this->model->shutter_speed = $exif['ExposureTime'];
  141.         }
  142.      }
  143.  
  144.      private function resize($image, $width){
  145.          $img = $this->img->make($image);
  146.          return $img->resize($width, null, function ($constraint) {
  147.              $constraint->aspectRatio();
  148.              $constraint->upsize();
  149.          });
  150.      }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement