Using URL as HTML <img src /> - php

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.

Related

php encoding and decoding string

I have an app where users can upload an image to be used as their avatar. The way is set things up are as follows. There's a folder named "Uploads" and when the user selects an image it gets uploaded and stored there and renamed to the user's username then a reference in the database is made referring to that image, so the string in the database will look like this
" ../uploads/username.jpg"
I am using PHP's rawurlencode(); function to handle user names with spaces in them, such as "User admin" so when this user uploads a picture it will be stored in the database like this ../uploads/user%20admin.jpg now when I wanna display that image i'm using rawurldecode(); function but it just isn't working properly as it's decoding in this manner "uploads/user"admin.jpg
This is my PHP to display the image.
<?php
echo '<td class="col-md-3">
<input type="checkbox" name="check_list[]" value="' .$row2['buss_id'].'">'
. '<img src=' . rawurldecode($row2['pic']) .'> '. $row2['username']; ?> </td>
To be more clear, this is how the string is being decoded in the browser
<img src="../uploads/user" admin.jpg>
Thanks.
You're missing quotes. This part of your code:
'<img src=' . rawurldecode($row2['pic']) .'> '
should be this:
'<img src="' . rawurldecode($row2['pic']) .'"> '
The issue with your current code is that it emits something like this:
<img src=../uploads/user admin>
The browser treats the space as a separator between attributes, so it interprets that this way:
<img src="../uploads/user" admin>

base64 image not printing properly

When I print $src I am getting following result:
echo $src;
Result:
data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR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2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+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+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTQyIiBoZWlnaHQ9IjkzIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDIgNzMgYyAtMC4wMiAtMC4xMiAtMSAtNC43NSAtMSAtNyBjIDAgLTIuMjUgMC40MyAtNC43IDEgLTcgYyAxLjc3IC03LjA5IDQuMzggLTEzLjc4IDYgLTIxIGMgMi4xMSAtOS40IDIuODMgLTE5LjA3IDUgLTI4IGMgMC43NSAtMy4wOSAyLjkxIC03LjkxIDQgLTkgYyAwLjQgLTAuNCAyLjYyIDEuMDkgMyAyIGMgMi40NCA1Ljg2IDMuODYgMTUuMjUgNyAyMiBjIDMuMzYgNy4yMiA4Ljc0IDE0Ljg3IDEzIDIxIGwgMyAyIi8+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTSAxNiA0MCBsIDggLTQiLz48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDY3IDIgYyAwLjIxIDAuNjMgOC4zOSAyMy41MiAxMiAzNiBjIDUuMyAxOC4zNSAxNCA1NCAxNCA1NCIvPjxwYXRoIGZpbGw9Im5vbmUiIHN0cm9rZT0iIzAwMDAwMCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+PC9zdmc+">
your missing the data: in your src attribute you need that for it to render properly ...
<img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcm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iBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+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+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMTQyIiBoZWlnaHQ9IjkzIj48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDIgNzMgYyAtMC4wMiAtMC4xMiAtMSAtNC43NSAtMSAtNyBjIDAgLTIuMjUgMC40MyAtNC43IDEgLTcgYyAxLjc3IC03LjA5IDQuMzggLTEzLjc4IDYgLTIxIGMgMi4xMSAtOS40IDIuODMgLTE5LjA3IDUgLTI4IGMgMC43NSAtMy4wOSAyLjkxIC03LjkxIDQgLTkgYyAwLjQgLTAuNCAyLjYyIDEuMDkgMyAyIGMgMi40NCA1Ljg2IDMuODYgMTUuMjUgNyAyMiBjIDMuMzYgNy4yMiA4Ljc0IDE0Ljg3IDEzIDIxIGwgMyAyIi8+PHBhdGggZmlsbD0ibm9uZSIgc3Ryb2tlPSIjMDAwMDAwIiBzdHJva2Utd2lkdGg9IjIiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIgZD0iTSAxNiA0MCBsIDggLTQiLz48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDY3IDIgYyAwLjIxIDAuNjMgOC4zOSAyMy41MiAxMiAzNiBjIDUuMyAxOC4zNSAxNCA1NCAxNCA1NCIvPjxwYXRoIGZpbGw9Im5vbmUiIHN0cm9rZT0iIzAwMDAwMCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+PC9zdmc+';
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+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjE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AwMCIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIGQ9Ik0gMTAyIDIxIGMgLTAuMDkgMC40MiAtMy4wNCAxNS44MiAtNSAyNCBjIC0xLjgyIDcuNjEgLTMuODIgMTUuMTggLTYgMjIgYyAtMC4zNSAxLjA4IC0yLjA4IDMgLTIgMyBjIDAuMDkgMCAxLjg4IC0yLjg0IDMgLTMgYyA2LjYxIC0wLjk0IDE3LjU4IC0yLjM2IDI1IC0xIGMgNy43MSAxLjQyIDI0IDEwIDI0IDEwIi8+PC9zdmc+';
?>
<img src="<?php echo $src; ?>">

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']. "'/>";

Image Search In Folder PHP

I am getting an image from user and storing it in a folder. Now using the image name i have to search the image in that same folder.The image is stored correctly in the folder.For search i am using some thing like this :
echo '<img src="upload/'.$thumb.'" " />';
the variable $thumb is having the image name.It is getting the image name like that :
$thumb=$_POST['thumbnailPic'];
if(isset($_REQUEST['thumbnailPic'])) {
$thumb=$_REQUEST['thumbnailPic'];
echo '<img src="upload/'.$thumb.'" />';
}
else {
die("no thumbnailPic is found!");
}
I think you have extra quotes in the image src :
echo '<img src="upload/'.$thumb.'" " />';
^---- this is extra quotes
Should be:
echo '<img src="upload/'.$thumb.'" />';
That quotes makes your img tag inavlid. Also check images permission

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..

Categories