I need to download multiple images in a Laravel response download function without Zipper because I need to show images on a new page in Angular. Thanks.
If I understand you correctly you want images served by server?
public function get($path)
{
$file = Storage::disk('s3')->get($pathToImage);
return response($file, 200)->header('Content-Type', 'image/png');
}
Related
I am having issues wrapping my head around integrating these 2 products together. I am dealing with how to make the following work.
User uploads 1-10 images via Spatie Media Library Pro (Headless)
Form Request is sent to the backend function
Images are stored (in this case moved) and conversion of image is completed
Log all photos into a batch to be retrieved later
There are a number of ways I have tried, however, the issue is the only way I have figured out how to log the images is as a single entry in the log without a batch ID. This is because when the data is run via media library, its done one by one and I cannot figure out where to start and end the batch so that its stored as expected.
I am not sure what code to share, happy to share anything though. Here is the basics of what I can think would help
PhotosController.php
public function store(Request $request)
{
//store photos using Spatie Media Library
$user = Auth::user();
$user
->addFromMediaLibraryRequest($request->media)
->withCustomProperties('tags')
// ->withResponsiveImages()
->toMediaCollection('user_photos');
session()->flash('flash.banner', 'Photos Uploaded!');
session()->flash('flash.bannerStyle', 'success');
return Redirect::route('dashboard');
}
Listeners/MediaLogger.php
public function handle(MediaHasBeenAdded $event)
{
$media = $event->media;
if ($media->collection_name == 'user_stories') {
return;
}
$user = Auth::user();
$path = $media->getPath();
LogBatch::startBatch();
activity()->causedBy($user)
->performedOn($user)
->event('new_photo')
->withProperties(['photo' => $path])
->log('uploaded new media!');
LogBatch::endBatch();
}
Any help would be forever grateful, Thanks!
this function returns only one image. but I want to show all images which are stored in this folder. can anyone help me?
My controller
public function index()
{
$get=Resturant::all(['id','name','image']);
foreach ($get as $image) {
$path =public_path().'/Images/Resturants/'.$image->image;
$file= File::get($path);
$type= File::mimeType($path);
$response= Response::make($file, 200);
$response->header("Content-Type",$type);
return $response;
}
}
You don't need to be trying to return a file response, since your web server itself can handle serving files in the public folder. Also you wouldn't be trying to return multiple files (somehow) in the response. You can just return an array of the URLs to the files and let what ever is consuming this make the requests to get these files by URL. This would simplify your controller method quite a bit:
return Resturant::pluck('image')
->map(fn ($i) => asset('Images/Resturants/'. $i));
PHP 8 here, but its pretty simple, just have to make URLs from each image.
If you want to have the names to go along with this you can adjust pluck to use a column for the index:
...pluck('image', 'name')...
I am using dompdf with laravel. Wheni have to make PDF, i write the below code in controller.
public function download()
{
$courseoffers = Courseoffer::all()->get();
$pdf = PDF::loadView('admin.courses.pdf', compact('courseoffers'))->setPaper('a4','landscape');
return $pdf->download('courseoffer.pdf');
}
Here, pdf.blade.php is a specific file. But i don't want to use that. i want a specific portion of blade file to make pdf. How do i achieve it.?
$files = $request->file('images'); // return {}
$_FILES['images']; // return {"name":"sample-passport.jpg","type":"image\/jpeg","tmp_name":"D:\\xampp\\tmp\\php4AD9.tmp","error":0,"size":264295}
Have you tried to remove the Content-Type header? According to this Github issue, it seems to be a problem.
So, I set up a new Laravel installation to test this out and it's working fine on my side. Of course, there's no authorisation whatsoever but this shouldn't impact the result too much.
routes/api.php
Route::post('/profile/upload_image', function (Request $request) {
dd($request->file('image'));
});
Postman configs
Your post input attribute type change file after upload you will get a response.enter image description here
If I load this image from Cloudfront into my browser it shows me the correct and latest version:
https://d8z37rm9cmbl3.cloudfront.net/1/groupings/extras/27/FS_27_0003.JPG
However, when I load it programmatically in my Laravel app from my Forge server, it shows an old cached version of the image (which in fact no longer exists on AWS). The call is exactly the same within the program and the app's URL is:
http://imagetester.artlookonline.com/img
The code for the procedure is very simple and uses Intervention Image (but not their cache):
public function serveDirectImage() {
$img = Image::make('https://d8z37rm9cmbl3.cloudfront.net/1/groupings/extras/27/FS_27_0003.JPG');
// create response and add encoded image data
$response = Response::make($img->encode('jpg', 70));
// set content-type etc header
$response->header('Content-Type', 'image/png')
->header("Cache-control", "no-store");
// output
return $response;
}