base64 image not printing properly - php

When I print $src I am getting following result:
echo $src;
Result:
data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTQyIiBoZWlnaHQ9IjkzIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDIgNzMgYyAtMC4wMiAtMC4xMiAtMSAtNC43NSAtMSAtNyBjIDAgLTIuMjUgMC40MyAtNC43IDEgLTcgYyAxLjc3IC03LjA5IDQuMzggLTEzLjc4IDYgLTIxIGMgMi4xMSAtOS40IDIuODMgLTE5LjA3IDUgLTI4IGMgMC43NSAtMy4wOSAyLjkxIC03LjkxIDQgLTkgYyAwLjQgLTAuNCAyLjYyIDEuMDkgMyAyIGMgMi40NCA1Ljg2IDMuODYgMTUuMjUgNyAyMiBjIDMuMzYgNy4yMiA4Ljc0IDE0Ljg3IDEzIDIxIGwgMyAyIi8+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTSAxNiA0MCBsIDggLTQiLz48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDY3IDIgYyAwLjIxIDAuNjMgOC4zOSAyMy41MiAxMiAzNiBjIDUuMyAxOC4zNSAxNCA1NCAxNCA1NCIvPjxwYXRoIGZpbGw9Im5vbmUiIHN0cm9rZT0iIzAwMDAwMCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+PC9zdmc+
But if I print the $src inside tag image is not rendering properly. string 'data:' is automatically cutting off from the url.
<img src="image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTQyIiBoZWlnaHQ9IjkzIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDIgNzMgYyAtMC4wMiAtMC4xMiAtMSAtNC43N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BzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDY3IDIgYyAwLjIxIDAuNjMgOC4zOSAyMy41MiAxMiAzNiBjIDUuMyAxOC4zNSAxNCA1NCAxNCA1NCIvPjxwYXRoIGZpbGw9Im5vbmUiIHN0cm9rZT0iIzAwMDAwMCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+PC9zdmc+" />
I don't see how that would be getting cut off.

I've tested this both ways, and it works. If it's not working for you something else is going on.
If you are echoing the image, using PHP, try;
$src = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTQyIiBoZWlnaHQ9IjkzIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDIgNzMgYyAtMC4wMiAtMC4xMiA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echo '<img src="'.$src.'">';
If you are calling the image up in the body of the page,, the following should work:
<?php
$src = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTQyIiBoZWlnaHQ9IjkzIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDIgNzMgYyAtMC4wMiAtMC4xMiAtMSAtNC43NSAtMSAtNyBjIDAgLTIuMjUgMC40MyAtNC43IDEgLTcgYyAxLjc3IC03LjA5IDQuMzggLTEzLjc4IDYgLTIxIGMgMi4xMSAtOS40IDIuODMgLTE5LjA3IDUgLTI4IGMgMC43NSAtMy4wOSAyLjkxIC03LjkxIDQgLTkgYyAwLjQgLTAuNCAyLjYyIDEuMDkgMyAyIGMgMi40NCA1Ljg2IDMuODYgMTUuMjUgNyAyMiBjIDMuMzYgNy4yMiA4Ljc0IDE0Ljg3IDEzIDIxIGwgMyAyIi8+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTSAxNiA0MCBsIDggLTQiLz48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDY3IDIgYyAwLjIxIDAuNjMgOC4zOSAyMy41MiAxMiAzNiBjIDUuMyAxOC4zNSAxNCA1NCAxNCA1NCIvPjxwYXRoIGZpbGw9Im5vbmUiIHN0cm9rZT0iIzAwMDAwMCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+PC9zdmc+';
?>
<img src="<?php echo $src; ?>">

Related

Can't display image in PHP

I am using this code to display an image.
echo "<img src=\"".$row["image"]."\">";
Printing $row["image"] gives:
https://www.lesechos.fr/medias/2017/04/19/2080617_hopital-les-candidats-a-la-presidentielle-divergent-sur-les-effectifs-web-0211985257133_300x160.jpg
Which is a valid source when I type it in my browser.
However, no image is displayed... What is wrong?
if you want to show an image, you need to use a img tag. An URL won't be enough. What about :
<?php
echo'<img src=" '.$row["image"].' " alt="my image" />';
/* after edit, I saw that you are using code below */
// echo "<img src=\"".$row["image"]."\">";
// seems fine though
// what about this ->
$path_to_img = $row['image'];
echo "<img src=\"$path_to_img\" alt=\"my image\" />"; /* just added `alt` and last slash to be fully compliant */
?>
what does source inspector show as html output ?
I just tested and hotlinking is fine, imgshows up (kind of an hospital hallway).
you have to print the image url in html src attribute:
echo "<img src='images/." $row['image']. "'/>";

Using URL as HTML <img src />

I'm trying to use a URL which just contains an image to use the image on my site. I am using PHP, this is my code:
Echo '<img src= "$link["Image_Link"]" />';`
$link["Image_Link"] points to this link.
I also have many similar links which contain different images, and are called like this one. When I call this on my site I just get a broken image symbol where the real image should be.
Single and double quotes are not interchangable in PHP.
This should work:
Echo "<img src='$link[Image_Link]' />";
or
Echo '<img src="' . $link["Image_Link"] . '" />';
You need to concatenate your variable with your string:
Echo '<img src= "'.$link["Image_Link"].'" />';
Without it your HTML code in final result will look like:
<img src="$link[" Image_Link"]" />
And this is invalid HTML.

to echo a <img > tag and send the code through email+ php

I have a php variable which contain the whole source code of image.What I want is to echo the tag with its attributes src,height & width and send the source code of image through mail (i.e ).
$imagename = abc.jpg;
$concatpath = SITE_URL."/uploads/affiliatesAdv/".$imagename;
$imagesrc = '<img src="'.$concatpath.'" height="200px" width="200px">';
Now when I echo $imagesrc it displays the image. How can I show the source code instead?
Try this,
echo "<pre>";
echo $imagesrc;
echo "</pre>";
You need to use html encoding.
Some thing like this
<img src="abc.jpg"/>
to
<img src="abc.jpg"/>
See the reference.
You can try this site to do it online..

Simple question about htmlentities and a small editing on my code for better <img src> results

I have a foreach loop (see below) that encodes the values to htmlentities() and then it is send through the form as a url.
for example www.domain.com/file.php?title=XXX&pics=XXX&content=XXX
This is my code:
foreach($results as $image){
$encodedpics = '<img src="'.$image.'"><br>';
echo htmlentities($encodedpics, ENT_QUOTES);
Then in the submitted form, I decode using html_entity_decode() and appeared inside the textarea. The problem is that the url images appear like these and can not be displayed later.
<img src=\"http://www.mypic.jpg\">
<img alt=\"\" src=\"http://www.mypic.png\">
<img border=\"0\" src=\"http://mypic.jpg\">
My question is how can I have only a <img src="www.mypic.jpg"> without \ or borders or alt?
Thank you all.
run htmlentities() on $image, not the entire string.
foreach($results as $image){
$encodedpics = '<img src="'.htmlentities($image, ENT_QUOTES, 'UTF-8).'"><br>';
echo $encodedpics;

Replacing the image within an image src tag

I have the following situation, I have an html file with image tags about 10 or more, but it varies. Now what I want to achieve is to replace the image src with a PHP function as example below
I want to replace something like this
<img src="image1.png" ....
<img src="image2.png" ....
with this
<img src="<?=imageResize('image1.png',20,15)?>" ...
<img src="<?=imageResize('image2.png',20,15)?>" ..`.
Is this possible?
Let's assume that the current page source is contained in $source. Try some regular expressions:
<?php
preg_replace ('/<img src="(.+)"/Ui', '<img src="<?=imageResize(\'\\1\',20,15)?>"', $source);
?>
I see 2 cases:
Your imageResize function resizes the image, creating a new one with a different name (something like image120x15.png) and returning a string with the image name. In this case you should do something like:
<img src="<?php echo imageResize('image1.png',20,15);?>" ...
Your imageResize actually resizes the initial image and overriding it. In this case, do something like:
<?php imageResize('image1.png',20,15);?>
<img src="image1.png" ...
Capture output with output buffer and then apply regex for replace.
<?ob_strart();?>
<img src="image1.png" />
<img src="image2.png" />
<?php
$cnt = ob_get_clean();
$cnt = preg_replace ('/<img src="([^"]+)"/', '<img src="<?=imageResize(\'\\1\',20,15)?>"', $cnt);?>
For execute function imageresize you must run output in eval().

Categories