0

Can anyone tell me why the base64 image code linked in here is invalid? Some kind of issue with the syntax I believe?

My file looks like this:

/9j/4AAQSkZJR..........39b/lRk5HGVz8

I have truncated it (the "...") since the entirety of the file won't fit in the question.

Here is my Javascript Fetch API POST code:

function submitPhoto(){ 


    console.log("name: "+name);
    console.log("email: "+email);
    console.log("market: "+market);
    //console.log("base64: "+b64data);


    fetch('http://fanbeauties.com/app/submit-photo.php?pass=MY_PASS', { 
    method: "POST", 
    headers: {
      "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
    },
    body: '&name='+name+'&email='+email+'&market='+market+'&picture='+b64data
     });


};
Mike Barbaro
  • 245
  • 1
  • 17
  • Do not link to external resources and expect us to click them. Post the relevant information here. The question should be able to be answered without going to any external sites. – zero298 Mar 07 '18 at 21:58
  • 1
    what a funny comment. Did you even checked the link? And why people put downvotes without even understanding question? – Mishel Parkour Mar 07 '18 at 21:59
  • The question seems valid to me. reason he has not put link in question is because link is about his base64 coded image. Like it would be like tons of AgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5jp6vLz9PX29/j5v/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/dAAQBP/aAAwDAQACEQMRAD in question (: – Mishel Parkour Mar 07 '18 at 22:00
  • @zero298 The link is my base64 code and is too many characters to embed within the post. – Mike Barbaro Mar 07 '18 at 22:01
  • You know guys you can always remove your down votes if you find out it was wrong to downvote – Mishel Parkour Mar 07 '18 at 22:02
  • Then post how you generated the encoded image and truncate the file. – zero298 Mar 07 '18 at 22:02
  • 1
    dude he is not looking for a way to fix the code generator in the first place. maybe he have found it somewhere the question is simple like if this string is a valid base64 encoded image or im having trouble using it and the code is correct. @zero298 – Mishel Parkour Mar 07 '18 at 22:05
  • @zero298 anyway i understand you want to help and make stackoverflow a better place but im not sure the down voting. for such reason would improve anything. You see I have put the answer so means the question was valid and clear enough – Mishel Parkour Mar 07 '18 at 22:07
  • 1
    @MishelParkour: As devastating as it may be to you, the downvotes are warranted. The question is expected to have all relevant information here so that it's useful to future readers when the link is no longer valid. –  Mar 07 '18 at 22:08
  • @doodlemeister, dear meister. I assume you have not checked the link. the link it self provide no usefull information. like bunch of AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/bAEMBAQEBAQEBAQEBAQEBAQEB. and its too long to be used here. but ok i will try to suggest an edit – Mishel Parkour Mar 07 '18 at 22:10
  • @MishelParkour: I assume you don't know how to make a live code demo inside a post, which scrolls content when it gets too long. –  Mar 07 '18 at 22:10
  • @doodlemeister dear doodle i know what is your point so i suggested an edit we need to wait for it to be accepted. its like `/9j/4AAQSkZJRgABAQKhvdG9z............ a lot of same thing ...........0HqB3zfNH5fd/XOeo4/A59smrcotWSem2v/AA` so its not full codes just start and end which is reason of error and which is usefull. So i dont agree with you to put whole spammy codes here. just start and end should be more than enough – Mishel Parkour Mar 07 '18 at 22:14
  • @MishelParkour: All relevant information should be present. The OP has not only posted off-site, but didn't actually include *how* the data is used. No matter how you spin it, the OP isn't including the necessary relevant info. –  Mar 07 '18 at 22:15
  • anyway man its opinions in my opinion it was ok. So if you think its not you can put downvotes. we really need to stop this thing. the thing we are doing is waste of time and deserves downvote. (: – Mishel Parkour Mar 07 '18 at 22:16
  • @MishelParkour I don't know if your edit was rejected, or why it isn't showing up, but I went ahead and added something similar just to give at least some link rot resistant context to the question. – zero298 Mar 08 '18 at 15:54
  • @MishelParkour I found that the issue is that the generated base64 is ok but when posting to the server the string gets truncated. I edited the post with my Javascript Fetch code. Is there anyway I can send the POST data differently? – Mike Barbaro Mar 08 '18 at 22:49
  • @MikeBarbaro i did post a new answer. so please check it. if the new answer fits for your question, you can accept it. and just upvote the previous one. (: – Mishel Parkour Mar 09 '18 at 00:17

2 Answers2

1

Yes I believe the syntax is not correct and it may not be a valid image or some parts are removed.

check this link https://jsfiddle.net/casiano/xadvz/

<img src="data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5NWvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78lzYLgjAJ8vAP9fX/+MjMUcAN8zM/9wcM8ZGcATEL+QePdZWf/29uc/P9cmJu9MTDImIN+/r7+/vz8/P8VNQGNugV8AAF9fX8swMNgTAFlDOICAgPNSUnNWSMQ5MBAQEJE3QPIGAM9AQMqGcG9vb6MhJsEdGM8vLx8fH98AANIWAMuQeL8fABkTEPPQ0OM5OSYdGFl5jo+Pj/+pqcsTE78wMFNGQLYmID4dGPvd3UBAQJmTkP+8vH9QUK+vr8ZWSHpzcJMmILdwcLOGcHRQUHxwcK9PT9DQ0O/v70w5MLypoG8wKOuwsP/g4P/Q0IcwKEswKMl8aJ9fX2xjdOtGRs/Pz+Dg4GImIP8gIH0sKEAwKKmTiKZ8aB/f39Wsl+LFt8dgUE9PT5x5aHBwcP+AgP+WltdgYMyZfyywz78AAAAAAAD///8AAP9mZv///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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B1keqwUhoCSK374wbujvOSu4QG6UvxBRydcpKsav++Ca6G8A6Pr1x2kVMyHwsVxUALDq/krnrhPSOzXG1lUTIoffqGR7Goi2MAxbv6O2kEG56I7CSlRsEFKFVyovDJoIRTg7sugNRDGqCJzJgcKE0ywc0ELm6KBCCJo8DIPFeCWNGcyqNFE06ToAfV0HBRgxsvLThHn1oddQMrXj5DyAQgjEHSAJMWZwS3HPxT/QMbabI/iBCliMLEJKX2EEkomBAUCxRi42VDADxyTYDVogV+wSChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0BIlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIFgByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUXFUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAOw==">

the format should likely began with this: data:image/gif;base64, or just this image/gif;base64, so first section shows image type. in your case it should be image/jpeg and the ;base64, is like defining that its a base64 inline image.

And at the end of the code or just end of anybase64 coded content there should be like == or = but you should not remove it its part of the coded content.

Mishel Parkour
  • 599
  • 5
  • 16
  • Thank you Mishel. I've actually been using that fiddle to try and debug. My base64 is generated from an iOS app and sent to the server. – Mike Barbaro Mar 07 '18 at 22:04
  • @MikeBarbaro ok mike now at this point you may need to look for ways to fix the generator if its the problem or you may post another question about the exact problem and with codes of what you have tried so far. – Mishel Parkour Mar 07 '18 at 22:08
0

So this is another answer. I post it separately, cause your question is changed.

There is two way to achieve this. First is to use URL-encode and URL-decode. So before you send the base64 string containing your image data u do encode it then on the server side you first echo is to see how it looks. Then you use PHP urldecode to get your string back.

URL decode/encode with javascript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

URL decode/encode in PHP:

http://php.net/manual/en/function.urlencode.php

https://stackoverflow.com/a/4744917/9453736

But the other way which should fit better in this situation is to use the post body when u wanna post something. But as you are already using a library to do that I guess its just the limitation of your library. So try to check if there are other ways to set parameters for POST with that library. Like you are adding the parameters to body but the way you do it (the library) just limits you. so for example you cant use some characters when you do this:

{
     body: '&name='+name+'&email='+email+'&market='+market+'&picture='+b64data
{

So in case you failed to find other ways to do the request. Like you can do it with form object with pure javascript and other ways. so in case you failed just go with the first one try to url encode the data.

example of url encode in javascript:

// encodes characters such as ?,=,/,&,:
console.log(encodeURIComponent('?x=шеллы'));
// expected output: "%3Fx%3D%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"

console.log(encodeURIComponent('?x=test'));
// expected output: "%3Fx%3Dtest"

example of urldecode in PHP:

$query = "my=apples&are=green+and+red";

foreach (explode('&', $query) as $chunk) {
    $param = explode("=", $chunk);

    if ($param) {
        printf("La valeur du paramètre \"%s\" est \"%s\"<br/>\n", urldecode($param[0]), urldecode($param[1]));
    }
}
Mishel Parkour
  • 599
  • 5
  • 16