I'm using axios to send a base64 string via ajax. Using the way below, I lose a lot of data somehow when it's encoded from base64 data back to a jpg. How can I send this without the data loss?
I grab the file from an input and send it to
var reader = new FileReader();
reader.readAsDataURL(file);
and that base64 string is sent as ajax with axios as
axios.post('url', {main: img})
A php script receives the post as:
$incoming = json_decode(file_get_contents('php://input'))->main;
$mainImage = str_replace('data:image/jpeg;base64,', '', $incoming);
$img = imagecreatefromstring(base64_decode($mainImage));
$imageSave = imagejpeg($img, './uploaded.jpg');
A recent file, for example, saved on the server is only 14k, but the original file I uploaded to the input field was 19k. I'm outputting the uploaded base64 on the client side to a preview div, and that image saves as a 19k jpg, so I assume it's the php script. Any ideas on what's causing the data loss? Maybe some axios config value?
base64_decode($mainImage)in a file directly, like:file_put_contents('./uploaded.jpg', base64_decode($mainImage));. Currently, you're creating a new Jpeg image, which will be slightly more compressed when you save it. - M. Eriksson.jpg-extension, like my example), you will get an exact copy of the uploaded image. - M. Eriksson