probir17's avatar

How do I validate base64 images

I'm using vue as my front-end.

I'm trying to upload image with vue-picture-input package

when i dd() my request in laravel, i get request something like this

"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4gKgSUNDX1BST0ZJTEUAAQEAAAKQbGNtcwQwAABtbnRyUkdCIFhZWiAH5AADAAEADwAmAB1hY3NwQVBQTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9tYAAQAAAADTLWxjbXMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAtkZXNjAAABCAAAADhjcHJ0AAABQAAAAE53dHB0AAABkAAAABRjaGFkAAABpAAAACxyWFlaAAAB0AAAABRiWFlaAAAB5AAAABRnWFlaAAAB+AAAABRyVFJDAAACDAAAACBnVFJDAAACLAAAACBiVFJDAAACTAAAACBjaHJtAAACbAAAACRtbHVjAAAAAAAAAAEAAAAMZW5VUwAAABwAAAAcAHMAUgBHAEIAIABiAHUAaQBsAHQALQBpAG4AAG1sdWMAAAAAAAAAAQAAAAxlblVTAAAAMgAAABwATgBvACAAYwBvAHAAeQByAGkAZwBoAHQALAAgAHU

So, my question is how do I validate this as my image data or to be precise how do I actaully know this is an image. User can put whatever he wants,since now i'm only using required as validation.

i tried with mimes:jpeg,bmp,png,jpg validation but it shows me error.

Any suggestion would be helpfull.

0 likes
3 replies
probir17's avatar

I'm not storing my images in my database,it's save in folder.

Using Image Intervention Package for making the image.

Just concern about my Image Validation.

probir17's avatar

I'm not sure what that random guy said in the comment by your given link

I stumbled upon this answer from doing a code review. Please don't use this in a production environment, as it is highly unsafe. You should never use one of the imagecreate functions on unknown data as it allocates a bitmap buffer for the entire image. It is very easy to create small png files of 100+MP that will ruin your day. Instead, write the image to a temp file and call getimagesize on it. (with the caveats noted here: php.net/getimagesize) If you have to allocate an image, then just use imagesx and imagesy to query the dimensions directly. 

Please or to participate in this conversation.