0

I have a base64 image that i need to display via php. It's to be used on a wordperss/woocommerce site, at the cart.

The string is correct and working fine when inserting directly into plain html img tag. The string starts as such: data:image/png;base64...............

But when it's inserted as src, via php, it doesn't include the beginning "data:" word. It displays as: image/png;base64...............

I have tested the string with plain echo, and it does include data: when just echoed out, but as soon as it's palced inside src or href, data: goes away.

I have no idea why this is happening and search results in no meaningful information.

Added code snippet:

$img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................";
    echo '<img src="'.$img.''" />';

data: disappears, echoes out as:

<img src="image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................" />

But if i do this:

$img = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................"
    echo $img;

It works and echoes as:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyAAAAImCAYAAACrXu7BAAAgAE................
  • Have you see this post....? [https://stackoverflow.com/questions/16262098/displaying-a-base64-images-from-a-database-via-php](https://stackoverflow.com/questions/16262098/displaying-a-base64-images-from-a-database-via-php). And it's more telling for us to see your code. – Juan Feb 09 '23 at 11:31
  • I did. But it does not help. As far as posting code, there is nothing to it really. I'm jsut trying to echo out a string with "data:...." and data: disappears. – daniel ochr Feb 09 '23 at 11:46
  • 2
    You can probably answer this question yourself if you try to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). In case you can't, after creating one, you can put it in your question, given us something concrete to work with. – KIKO Software Feb 09 '23 at 11:47
  • It's fairly simple vis-a-vis the code. I have added something in the question. Hope that is enough. There is 0 code that alters the string elsewhere. – daniel ochr Feb 09 '23 at 11:52
  • I tried your code, and get a different result: https://3v4l.org/lKQg4 – KIKO Software Feb 09 '23 at 11:54
  • Ok, then it's realted to wordpress/woocommerce (i did include that it was used in that env.) – daniel ochr Feb 09 '23 at 11:55
  • Possibly, but it looks to me that you have to look at your example a bit closer. Your output is missing the whole image tag. Is that really what happens? Did you actually test the code in your question? Did you notice the syntax error you made? – KIKO Software Feb 09 '23 at 11:57
  • Sorry about that. There is no syntax error, i just didn't include the img tag in the output because i thought it was a given. – daniel ochr Feb 09 '23 at 12:05
  • There is a syntax error in the code in your question, the first lines are missing the semi-colon at the end. I guess it is possible that some Javascript manipulates the content of the `src` attributes of the image tag. But what you're saying is very specific, and I deem it unlikely. – KIKO Software Feb 09 '23 at 12:09
  • You could check the source of the HTML in your browser, to see if the "data:" is there, or if it is already missing. If it is there you know it disappears in the browser and PHP has little to do with it. – KIKO Software Feb 09 '23 at 12:12
  • No syntax error. That code is just as an example. That is not the real string. I didn't think this question warranted the example to be that specific. Help is greatly appreciated, but you need not focus on the syntax that much. I also use an ide ofc. and it would have told me if such a simple error was there. – daniel ochr Feb 09 '23 at 12:15
  • 1
    I get that, but it shows to me that you didn't test the code your put in your question. Anyway, have you looked at the HTML source, as it is produced by PHP? – KIKO Software Feb 09 '23 at 12:20
  • Welcome to Stack Overflow! With respect, questions about code bugs almost always benefit from having the code included in the question. And, the browser's View Source and Inspect Element functions (right click) are really useful for troubleshooting this kind of thing. – O. Jones Feb 09 '23 at 12:25
  • I have looked at the html source, yes. The data: is not included. As far as the rest it works. But as soon as the string is inside an img or a tags, it removes "data:" – daniel ochr Feb 09 '23 at 12:29
  • That does not make sense to me. There's something going on here we're not aware of, or you're not telling us. I don't believe this is a Wordpress/Woocommerce thing, it's more likely you're missing something because you think this is "fairly simple" code. – KIKO Software Feb 09 '23 at 12:32
  • 1
    In other words: I cannot explain this. I guess it could be a plugin in Wordpress, buffering the output, and fiddling with the image `src` attribute. – KIKO Software Feb 09 '23 at 12:33
  • @KIKOSoftware is right. There is indeed a syntax error in the proposed code. A syntax error that prevents a PHP script using this code from running. And in pure PHP there is **no reason** (once the error is fixed) for the word "data" to be removed. None – Juan Feb 09 '23 at 12:58
  • No syntax errors. I have stated this... Also turn out there is a reason that data: will get removed... wordpress/woocommerce. Se my answer. – daniel ochr Feb 09 '23 at 13:23

1 Answers1

0

I found the issue.

It was indeed wordpress/woocommerce that was the issue, specifically in the cart and not syntax errors...

The issue is do to: wp_filter_post_kses

I will mark this as resolved...

Thanks to those who actually tried to help and understand that the code snippet was not the real code and that there was no syntax errors, as i stated.

Maybe people should not be overly pedantic when trying to help? Just a thought...

  • `echo '';` This is your code in your question. It's not pedantic to point out that there's a syntax error in this code, it's not pedantic but just objective. When we try to solve a problem we sometimes take it step by step. And the first steps can be code to fix. – Juan Feb 09 '23 at 13:38
  • Nice to see that you found it. Well done. I can understand your irritation about the syntax errors, but they were, and still are, at this very moment, part of your question. From my point of view you seem to have a cavalier attitude towards programming code, something I would not have mentioned until you called me pedantic. Whenever you ask a question here, you can expect people to ask you for a code example, and if that example contains errors they will be pointed out. It's the standard process of trying to understand, and replicate, the problem. – KIKO Software Feb 09 '23 at 13:47
  • That code was just and example and not the real code. I get that the code have to be syntax free in order to help. But you guys keep focusing (still!?) on something i told you wasn't the real code, just and example. And i told you that the ide would have warned me of that error if it had existed, and the output would not have been what i described (certainly not removing data: only)... and what about the string itself, i shortened it for the example... that is also a syntax error, yet no one pointed that out... I thought a little logical thinking was in place here, if only here :S – daniel ochr Feb 09 '23 at 13:50
  • 1
    I dropped the issue quite quickly, I think? Some things are allowed in code examples: replacing passwords, domains, etc. Replacing a long base64 string by dots is also reasonable, we don't need the image data. – KIKO Software Feb 09 '23 at 13:53
  • I don't really have a cavalier attitude towards code... just this specific question, that is true. I falsely assumed it would make sense from just describing it, since the immediate issue was fairly simple... a missing word. And btw. nothing i could have included vis-a-vis code woudl have helped here. I do find it funny though, that i found the issue ion a forum without code examples... But thanks for trying to help anyways. – daniel ochr Feb 09 '23 at 13:55
  • We are not focused on a syntax issue. And that's considering us fools in addition to pedants for the remark about the unterminated base-64 encoded string. We are not focused on a syntax error but the least we can do when posting code is to put correct code. Signed "a pedant who tried in vain to help you" – Juan Feb 09 '23 at 14:04