Assign Image SRC in php - php

After retrieve image path from database I want to pass it to <img> tag to show up,
I read this solution, but I do not want to echo it, I want to assign it as src for image tag
I tried this:
$image = mysqli_fetch_array($user_images);
$img1 = $image['file_path'];
$imageData = base64_encode(file_get_contents($img1));
$src = 'data: '.mime_content_type($img1).';base64,'.$imageData;
How can I assign $src to image, I tried this but no luck :(
<img id="img1" width="150" height="150" src="' . $src . '"/>
Thanks in advance.

Inside the HTML you still need to echo the value of $src. This should work:
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>

Try with PHP tags like below:-
<img id="img1" width="150" height="150" src="<?php echo $src;?>"/>

Reasonable solution I can think of is
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
but the code where you get the image source has to be above the image tag, so you have the $src.

You are using PHP language and in Php, you can't pass variable directly in the Tag you have to set a variable in the PHP environment.
To create Php environment you can right <?php ?> or you can use to echo variable <?= ?> between these PHP tags you can pas your variables to echo and assign or anything else what you want to do.
<img id="img1" width="150" height="150" src="<?= $src ?>"/>

Related

Can't display the image in browser

$images['result'][0]['image_path'] - here I have path of image. It is in public folder.
<img width="150" height="150" alt="150x150" src="<?php BASEPATH."../public/" echo $images['result'][0]['image_path'];?>" />
Can anyone help me to display this image?
Try This,
<img width="150" height="150" alt="150x150" src="<?php echo base_url('your/Folder/Structure/').$images['result'][0]['image_path'];?>" />
First concate your file name with path which causing the issue.
base_url() will also help you to fetch/show your image

Using a $_GET variable as img src

I am trying to link an image using the same name as a $_GET variable, example bellow:
The $_GET
$venue = $_GET['venue'];
Is it possible to use '$venue' as the image src example being something like
<div id="imgbox">
<img src="$venue.jpg" alt="venueimage" height="150" width="250">
</div>
My attempts so far have been unsuccessful, is it possible in a similar way to this or is there an alterantive?
Thankyou
You forgot your PHP tags and echo statement:
<div id="imgbox">
<img src="<?php echo $venue; ?>.jpg" alt="venueimage" height="150" width="250">
</div>
or shorthand:
<div id="imgbox">
<img src="<?= $venue; ?>.jpg" alt="venueimage" height="150" width="250">
</div>
As pointed out by Quentin we should take this a step further and sanitize our output:
<div id="imgbox">
<img src="<?= htmlspecialchars($venue); ?>.jpg" alt="venueimage" height="150" width="250">
</div>
You should be able to use <img src="<?php echo $venue; ?>.jpg" alt="venueimage" height="150" width="250">
I forgot to sanitize the code as well. You would need to add:
<img src="<?php echo htmlspecialchars($venue); ?>.jpg" alt="venuimage" height="150" width="250">

insert image with session

I store a pictures name in the database, and but it to a folder.
Now I want to insert that picture like this:
<?php $image = "/uploads/" . $_SESSION['profile_picture'] ?>
<img src="<?php echo $image?>" width="100" height="100">
It says:
Bad value "" for attribute "scr" on element "img":DOUBLE_WHITESPACE in PATH
Syntax of IRI reference:
Any URL. For example:'/hello/','#carvas', 'http://exaple.org'. Character should be represented in NFC and spaces should be escaped as %20.
I took an extra character. The correct code is the following:
<?php $image = "uploads/" . $_SESSION['profile_picture'] ?>
<img src="<?php echo $image?>" width="100" height="100">

PHP file_get_contents with if/else

On this link I was learning about file_get_contents and I'd like to incorporate it into a simple if/else statement but can't seem to get it correct.
I'd like to check if the file exists and if not, display a no_image.jpg graphic.
My struggle is that the tag already has a PHP echo so it is causing the snippet to stop working. How do I format the if/else while still haveing the use the dynamic value from the echo if file_get_contents is true?
Currently I have a simple tag like this:
<img width="200" height="150" src="images/<?php echo(rawurlencode($row['MLS_NUMBER'])); ?>_1.jpg" alt="" align="left" vspace="3" hspace="3" />
Basically:
<?php
if (file_exists(...)) {
$path = ...;
} else {
$path = 'no_image.jpg';
}
?>
<img src="images/<?php echo $path; ?>" />
//add a semicolon after echo $path otherwise it won't work
Assuming you are looking for a jpg file based on $row['MLS_NUMBER']
<img width="200" height="150" src="images/
<?php
if (file_exists($row['MLS_NUMBER']) {
echo(rawurlencode($row['MLS_NUMBER'])).'_1.jpg';
} else {
echo "no_image.jpg";
}
?>" alt="" align="left" vspace="3" hspace="3" />

How to pass a string from PHP into an html tag?

I have a string, an img url, in a php block called $str. And I want to set that string as the img src in an img src but its not working.
<img src="<?php $str ?>" height="50px" width="50px">
how else can i set the src to the $str string?
<img src="<?php echo $str;?>" height="50px" width="50px">
Well, I found correct answer for this problem. Nothing works well from above answers, that codes only print out source string to html page on site.
This works for me (I have my function that return source string of picture):
require_once("../../my_coded_php_functions.php");
<?php echo '<img src="' . getSourcePathOfImage() . '" />' ?>
This site(article) helped me to find and understand solution:
http://php.net/manual/en/faq.html.php
<?php echo '<img src="'.$str.'" height="50px" width="50px">' ?>
Other ways, although is not recommended. You may try :
<img src="<?=$str?>" height="50px" width="50px">
this will work, only if (in php.ini)
short_open_tag = On

Categories