sheenam12

Untitled

Oct 16th, 2017
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. // upload multiple images
  2. public function uploadImages($vendor_id){
  3. return View::make('adminpages/uploadImages', array('vendor_id'=>$vendor_id));
  4. }
  5.  
  6.  
  7. // imgupload for real weddings
  8. public function imageUpload(){
  9.  
  10. //store all the inputs in a variable
  11. $input = Input::all();
  12.  
  13. //define the image rules
  14. $rules = array(
  15. 'file' => 'image|max:50000',
  16. );
  17.  
  18. //pass input and rules to validation class
  19. $validation = Validator::make($input, $rules);
  20.  
  21. //if validation fails return json response as error
  22. if ($validation->fails()) {
  23. return Response::json('Invalid images', 400);
  24. }
  25.  
  26. //store file input in a varaible
  27. $file = Input::file('file');
  28.  
  29. //get extension of file
  30. $extension =$file->getClientOriginalExtension();
  31.  
  32. $directory = '/uploads/images/';
  33.  
  34. //change filename to a random sha1 plus current time
  35. $filename = "vendor_" . substr(sha1(rand(0,5).time()), 0, 15) . rand(0,100000) .".{$extension}";
  36.  
  37. //Move the uploaded file to temp directory
  38. // $upload_success = $file->move($directory, $filename);
  39.  
  40. $s3 = AWS::get('s3');
  41.  
  42. $upload_success = $s3->putObject([
  43. 'ACL' => 'public-read',
  44. 'Bucket' => 'glam360',
  45. 'Key' => $directory.$filename,
  46. 'Body' => fopen($file->getPathname(), 'r'),
  47. 'ContentType' => 'image/'. $extension
  48. ]);
  49.  
  50. $s3 = \AWS::get('s3');
  51.  
  52. // $upload_success = $s3->putObject([
  53. // 'ACL' => 'public-read',
  54. // 'Bucket' => 'wedimages',
  55. // 'Key' => $directory . $filename,
  56. // 'Body' => fopen($file->getPathname(), 'r'),
  57. // 'ContentType' => 'image/'. $extension,
  58. // ]);
  59.  
  60. // resize and image and overrite it in its current location
  61. // $img = Image::make('images/'.$filename)->resize(500,500)->save();
  62. // $img = Image::make('images/'.$filename);
  63.  
  64. //if upload is success
  65. if( $upload_success )
  66. {
  67. //if imageArray session exists
  68. if (Session::has('tempImages2'))
  69. {
  70. //get images array from session
  71. $imagesArray = Session::get('tempImages2');
  72. }
  73.  
  74. //else create a new imagesArray and store it in session
  75. else
  76. {
  77. $imagesArray= array();
  78. Session::put('tempImages2', $imagesArray);
  79. }
  80.  
  81. //get imagesArray from session
  82. $imagesArray = Session::get('tempImages2');
  83.  
  84. //push the new image name into imageArray
  85. array_push($imagesArray, $filename);
  86.  
  87. //Override the the current session with thye array which has new image
  88. Session::put('tempImages2', $imagesArray);
  89.  
  90. return Response::json(array('success' => 1, 'data' => $filename), 200);
  91.  
  92. }
  93.  
  94. //if upload is not success
  95. else
  96. {
  97. //return json response as error
  98. return Response::json('error');
  99. }
  100. }
  101.  
  102. public function uploadImagesPost(){
  103.  
  104. $input = Input::all();
  105. //Check if images are set or not
  106. if( !(Session::has('tempImages2')) OR !(count(Session::get('tempImages2')) > 0) ){
  107. $input['no_images']="Please upload a few images.";
  108. return Redirect::back()->withInput($input);
  109. }
  110.  
  111. $tempImages= Session::get('tempImages2');
  112. $single_arr = json_encode($tempImages);
  113.  
  114. $session_data = Session::get('admin_logged_in');
  115.  
  116. Session::forget('tempImages2');
  117.  
  118. foreach ($tempImages as $value) {
  119. // $data = DB::table('vendor_images')
  120. // ->insertGetId(array(
  121. // 'vendor_id' => $input['vendor_id'],
  122. // 'image' => $value,
  123. // 'updated_at' => new DateTime,
  124. // 'created_at' => new DateTime
  125. // ));
  126.  
  127. $data[] = array('vendor_id' => $input['vendor_id'],
  128. 'image' => $value,
  129. 'updated_at' => new DateTime,
  130. 'created_at' => new DateTime
  131. );
  132. }
  133.  
  134. $vendor_images_id = DB::table('vendor_images')->insert($data);
  135.  
  136. $input['successMsg'] = 'Added successfully!';
  137.  
  138. return Redirect::route('admin.dashboard')->withInput($input);
  139.  
  140. }
Add Comment
Please, Sign In to add comment