selimppc

Image upload in Laravel 5.1

Jan 15th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1.  public function image_upload($image,$file_type_required,$destinationPath)
  2.     {
  3.         if ($image != '') {
  4.             $img_name = ($_FILES['image']['name']);
  5.             $random_number = rand(111, 999);
  6.             $thumb_name = 'thumb_400x400_'.$random_number.'_'.$img_name;
  7.             $newWidth=400;
  8.             $targetFile=$destinationPath.$thumb_name;
  9.             $originalFile=$image;
  10.             $resizedImages  = ImageResize::resize($newWidth, $targetFile,$originalFile);
  11.             $thumb_image_destination=$destinationPath;
  12.             $thumb_image_name=$thumb_name;
  13.             //$rules = array('image' => 'required|mimes:png,jpeg,jpg');
  14.             $rules = array('image' => 'required|mimes:'.$file_type_required);
  15.             $validator = Validator::make(array('image' => $image), $rules);
  16.             if ($validator->passes()) {
  17.                 // Files destination
  18.                 //$destinationPath = 'uploads/slider_image/';
  19.                 // Create folders if they don't exist
  20.                 if (!file_exists($destinationPath)) {
  21.                     mkdir($destinationPath, 0777, true);
  22.                 }
  23.                 $image_original_name = $image->getClientOriginalName();
  24.                 $image_name = rand(111, 999) . $image_original_name;
  25.                 $upload_success = $image->move($destinationPath, $image_name);
  26.                 $file=array($destinationPath . $image_name, $thumb_image_destination.$thumb_image_name);
  27.                 if ($upload_success) {
  28.                     return $file_name = $file;
  29.                 }
  30.                 else{
  31.                     return $file_name = '';
  32.                 }
  33.             }
  34.             else{
  35.                 return $file_name = '';
  36.             }
  37.         }
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment