Advertisement
Twissel

ImageController

Aug 23rd, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.86 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4. use Illuminate\Routing\Controller as BaseController;
  5. use Illuminate\Support\Facades\Request as Request;
  6. use Illuminate\Support\Facades\View as  View;
  7. use Illuminate\Support\Facades\Validator;
  8. use Illuminate\Support\Facades\Input;
  9. use Illuminate\Support\Facades\Redirect;
  10. use Illuminate\Support\Str;
  11. use Config;
  12. use DB;
  13. use App\Models\Photo;
  14. use App\Url;
  15.  
  16.  
  17. class ImageController extends BaseController {
  18.  
  19.  
  20.     public function getIndex()
  21.     {
  22.  
  23.         //Let's load the form view
  24.         return View::make('tpl.index');
  25.     }
  26.     public function postIndex()
  27.     {
  28.  
  29.         //Let's validate the form first with the rules which a reset at the model
  30.         $validation = Validator::make(Request::all(),Photo::$upload_rules);
  31.         $url = new Url;
  32.  
  33.         //If the validation fails, we redirect the user to theindex page, with the error messages
  34.         if($validation->fails()) {
  35.             return Redirect::to('/')->withInput()->withErrors($validation);
  36.         }
  37.         else {
  38.  
  39.             //If the validation passes, we upload the image to the database and process it
  40.             $image = Input::file('image');
  41.  
  42.             //This is the original uploaded client name of the image
  43.             $filename = $image->getClientOriginalName();
  44.             //Because Symfony API does not provide filename//without extension, we will be using raw PHP here
  45.             $filename = pathinfo($filename, PATHINFO_FILENAME);
  46.  
  47.             //We should salt and make an url-friendly version of//the filename
  48.             //(In ideal application, you should check the filename//to be unique)
  49.             $fullname = Str::slug(Str::random(8).$filename).'.'.$image->getClientOriginalExtension();
  50.  
  51.             //We upload the image first to the upload folder, thenget make a thumbnail from the uploaded image
  52.             $upload = $image->move(config('app.upload_folder'),$fullname);
  53.  
  54.             //Our model that we've created is named Photo, thislibrary has an alias named Image, don't mix them two!
  55.             //These parameters are related to the image processing class that we've included, not really related toLaravel
  56.             //Image::make(Config::get( 'image.upload_folder').'/'.$fullname)->resize(Config::get( 'image.thumb_width'),null, true)->save(Config::get( 'image.thumb_folder').'/'.$fullname);
  57.  
  58.             //If the file is now uploaded, we show an error message to the user, else we add a new column to the databaseand show the success message
  59.             if($upload) {
  60.  
  61.                 //image is now uploaded, we first need to add columnto the database
  62.                 $insert_id = DB::table('photos')->insertGetId(
  63.                     array(
  64.                         'title' => Input::get('title'),
  65.                         'image' => $fullname
  66.                     )
  67.                 );
  68.  
  69.                 //Now we redirect to the image's permalink
  70.                 return Redirect::to(URL::to('snatch/'.$insert_id))->with('success','Your image is uploaded successfully!');
  71.             } else {
  72.                 //image cannot be uploaded
  73.                 return Redirect::to('/')->withInput()->with('error','Sorry, the image could not be uploaded, please try again later');
  74.             }
  75.         }
  76.     }
  77.     public function getSnatch($id) {
  78.         //Let's try to find the image from database first
  79.         $image = Photo::find($id);
  80.         //If found, we load the view and pass the image info as parameter, else we redirect to main page with error message
  81.         if($image) {
  82.             return View::make('tpl.permalink')->with('image',$image);
  83.         } else {
  84.             return Redirect::to('/')->with('error','Image not found');
  85.         }
  86.     }
  87.  
  88.  
  89.     public function getAll(){
  90.  
  91.         //Let's first take all images with a pagination feature
  92.         $all_images = DB::table('photos')->orderBy('id','desc')->paginate(6);
  93.  
  94.         //Then let's load the view with found data and pass the variable to the view
  95.         return View::make('tpl.all_images')->with('images',$all_images);
  96.     }
  97.     public function getDelete($id) {
  98.         //Let's first find the image
  99.         $image = Photo::find($id);
  100.  
  101.         //If there's an image, we will continue to the deleting process
  102.         if($image) {
  103.             //First, let's delete the images from FTP
  104.             File::delete(Config::get('image.upload_folder').'/'.$image->image);
  105.     File::delete(Config::get('image.thumb_folder').'/'.$image->image);
  106.  
  107.     //Now let's delete the value from database
  108.     $image->delete();
  109.  
  110.     //Let's return to the main page with a success message
  111.     return Redirect::to('/')->with('success','Image deleted successfully');
  112.  
  113.   } else {
  114.             //Image not found, so we will redirect to the index page with an error message flash data.
  115.             return Redirect::to('/')->with('error','No image with given ID found');
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement