Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intervention Image - how to upload images with MIME type: application/octet-stream

Im using Laravel framework with Intervention Image library to upload and process images on my page. Everything works fine until I try to update .jpg images taken from mobile devices or from cameras. These images have MIME type application/octet-stream instead of image/jpeg. When I try to upload image with this command:

Image::make($thumb)->resize($this->twidth, $this->theight)->save($this->destination."/".$filename);

I get error:

Unable to read image from file

One uncomfortable solution is open that images in MS Paint and resave them, which will change MIME type to 'image/jpeg' and make it possible to upload it. But I would like to evade this solution at all cost.

EDIT:

I was trying to fix it by this command:

$img = imagecreatefromstring($thumb);

but it returns error: imagecreatefromstring(): Empty string or invalid image

Is there a way how to handle this type of images?

like image 699
Tomas Turan Avatar asked Dec 18 '14 09:12

Tomas Turan


People also ask

What is application octet stream MIME type?

The application/octet-stream MIME type is used for unknown binary files. It preserves the file contents, but requires the receiver to determine file type, for example, from the filename extension. The Internet media type for an arbitrary byte stream is application/octet-stream .

What is MIME type for JPG?

The header contains information about the message and body. 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.

What is a MIME type what does it consist of and what is it used for provide an example?

A MIME type has two parts: a type and a subtype. They are separated by a slash (/). For example, the MIME type for Microsoft Word files is application and the subtype is msword. Together, the complete MIME type is application/msword.


2 Answers

Check upload_max_filesize in php.ini. It was reason in my case.

like image 136
G.Baghashvili Avatar answered Oct 17 '22 21:10

G.Baghashvili


Well, In such a case you can remove Laravel validation rule mimes, and instead, you can validate the image by its extension using PHP.

Your validation rule will look like this.

$request->validate([
    'image' => 'required',
]);

Validating image using file extension will look like this

if (! in_array($request->file('image')->clientExtension(), ['jpg', 'jpeg', 'png'])) {
   // not a valid image
}
like image 27
Kamlesh Suthar Avatar answered Oct 17 '22 19:10

Kamlesh Suthar