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?
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 .
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.
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.
Check upload_max_filesize in php.ini. It was reason in my case.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With