Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get mime type of image from storage?

I'm getting a file from storage using:

$image = Storage::get('test.jpg');

How can I get it's mime type?

I've tried:

$mime = Storage::mimeType($image);

With no luck.

I need the mime type so I can return the image as a response:

$response = Response::make($image, 200);
$response->header('Content-Type', $mime);
return $response;
like image 495
panthro Avatar asked Feb 26 '19 16:02

panthro


People also ask

How do I find MIME type files?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

How can I get MIME type from uploaded file in PHP?

The mime_content_type() function is an inbuilt function in PHP which is used to get the MIME content-type of a file. Parameters: This function accepts single parameter $file which specifies the path of the file which MIME details to be find. Return Value: This function returns the MIME content type or False on failure.

What is MIME type for image?

One of the most important pieces of data, called the MIME type specifies what the body of text describes. For instance, a GIF image is given the MIME type of image/gif, a JPEG image is image/jpg, and postscript is application/postscript.


1 Answers

When using the Storage facade in a controller, you can return the image as a response in Laravel 5.7. The response() function will automatically set the headers required rather than attempting to do it yourself.

// Option #1
return Storage::response('test.jpg'); // Local storage
// Option #2
return Storage::disk('s3')->response('test.jpg'); // If you are using an S3 driver
// Option #3
return Storage::cloud()->response('test.jpg'); // Also S3 driver unless otherwise configured

This is not documented, but can be found by looking at the underlying Laravel code:

/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php

If you still need to get the mime type for any reason, you should be able to retrieve it by calling:

$mimeType = Storage::mimeType('test.jpg');
like image 192
vanjamlin Avatar answered Sep 20 '22 11:09

vanjamlin