This should be simple. Trying to echo an image if it exists in a directory. Else guest.png should echo. So far, the guest.png is echoing fine. My problem is that I can never the .jpg image when it does exist. I have double-checked the source. The image just shows up as an empty image box.
<?php
$ID=$row_RecordsetLast['ID'];
$image = '../../pics/'.$ID.'.jpg';
if (file_exists($image)) {
echo '<img src=$image alt="" width="110" height="161" />';
} else {
echo '<img src="guest.png" alt="" width="110" height="161" />';
}
?>
You're not wraping the image address with apostrophes or quotes.
Change the line to this:
echo '<img src="$image" alt="" width="110" height="161" />';
Should work, considering the given address will be reached by the HTML page.
Have in mind that ../../pics/ may not work depending on your project's folder structure.
After trial and error, the following code worked for me. I guess variables did not work well within img source.
<?php
$ID=$row_RecordsetLast['ID'];
$image = '../../pics/'.$ID.'.jpg';
if (file_exists($image)) {
echo '<img src="../../pics/' . $ID . '.jpg" alt="" width="110" height="161" />';
} else {
echo '<img src="guest.png" alt="" width="110" height="161" />';
}
?>
Related
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 ?>"/>
I'm trying to get an image using the following
$html ='<img alt="" src="'.bloginfo('stylesheet_directory').'/images/my-image.png">';
When I return $html the path from bloginfo('stylesheet_directory') shows in the top portion of the page instead in the src attribute of the image.
You should use use get_stylesheet_directory_uri() instead. And you are missing a quote (as CyberJunkie pointed out). Try this:
$img_path = get_stylesheet_directory_uri() . '/images/my-image.png';
$html = '<img alt="" src="' . $img_path . '" />';
There is also a sample in the wordpress codex:
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />
bloginfo will automatically echo out the directory you are requesting, and has no return value. The output happens when you are building your string, not whenever you are echoing the $html variable.
I think the function you are looking for is get_stylesheet_directory_uri
Example usage of bloginfo (notice no echo):
<?php bloginfo('name'); ?>
Example usage of get_stylesheet_directory_uri:
<?php echo get_stylesheet_directory_uri(); ?>
Part of the code that's supposed to display an image:
$userav = getUserAvatar($_SESSION['login']);
$image1 = '<img src="$userav" alt="avatar.gif" width="80" height="80"/>';
echo '<span class="devpanellog">userav: '.$userav.'</span>';
?>
<div style="position:absolute; top:3px; left:3px; font-size: 12">
<?php
echo $image1;
?>
</div>
With $userav being userav: /wamp/www/graphics/avatars/defaultavatar.gif, the files exists there, there's nothing wrong with the image itself.
And the image that's being echo'ed:
http://prntscr.com/3ffb0j
I ran out of ideas, for now at least.
I have tried using /www/graphics/avatars and /graphics/avatars as the path to the dir but it didn't work either.
The script that's supposed to display the image is located in a different subfolder, /www/scripts/somescript.php while images are in /www/graphics/
After you noticed the pathetic fails I haven't noticed I fixed it to (also tried the other anwsers)
$image1 = '<img src="'.$userav.'" alt="avatar.gif" width="80" height="80"';
and it still doesn't display the image.
Fixed. The solution was wrong quotes, missing />(edit - it works even without /> lol) and wrong path.
Some random function($pathtoavatars = "/graphics/avatars/")
$userav = getUserAvatar($_SESSION['login']);
$image1 = '<img src='.$userav.' alt="avatar.gif" width="80" height="80"';
echo '<span class="devpanellog">userav: '.$userav.'</span>';
?>
<div style="position:absolute; top:3px; left:3px; font-size: 12">
<?php
echo $image1;
?>
</div>
your image variable is wrong try:
$image1 = '<img src="' . $userav . '" alt="avatar.gif" width="80" height="80" />';
Also your $userav variable is wrong - you'll just need the path from your webroot so assuming your web root is www you just need the /graphics/avatars/defaultavatar.gif part of it
Or you could try
$image1 = '<img src="' . str_replace($_SERVER['DOCUMENT_ROOT'], '', $userav) . '" alt="avatar.gif" width="80" height="80" />';
This will never work:
$image1 = '<img src="$userav" alt="avatar.gif" width="80" height="80"';
Do this instead:
$image1 = '<img src=" /graphics/avatars/'. $userav .'" alt="avatar.gif" width="80" height="80">';
Not sure but I think you should change the quotation marks from ' to " and vice versa. You didn't close the image tags and also, I think you don't input the image right.
Try this:
$userav = getUserAvatar($_SESSION['login']);
$image1 = "<img src='" . $userav . "' alt='avatar.gif' width='80' height='80'>";
echo "<span class='devpanellog'>userav: " . $userav . "</span>";
?>
<div style="position:absolute; top:3px; left:3px; font-size: 12">
<?php
echo $image1;
?>
</div>
You are you single quotes, Single quotes mean literal display (the $var instead of its value).
To remedy this, you do this:
$image1 = '<img src="'.$userav.'" alt="avatar.gif" width="80" height="80" />';
---------------^^ ---^^
You can see the syntax highlighting change. Also, you forgot to close the image-tag :)
A topic with a more detailed explanation:
What is the difference between single-quoted and double-quoted strings in PHP?
Some examples:
$var = "Hello";
echo 'Say $var World'; // screen will say [Say $var World]
echo "Say $var World"; // screen will say [Say Hello World]
echo 'Say '.$var.' World'; // screen will say [Say Hello World]
echo "Say ".$var." World"; // screen will say [Say Hello World]
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" />
I am trying to set size of an image in PHP but it's not working..
echo "<img src=".$media."width=200 height=200";
$media is the src link. Without the width and height attributes, it works perfectly well. I think that 200 must be enclosed in double quotations but I am unable to do that. Any ideas on how to solve this problem?
width is currently concatenated with your file name. Use:
echo '<img src="'.$media.'" width="200" height="200" />';
The /> closing tag is necessary to correctly render your image element.
The quotes around tag names are recommended. Omitting these quotes will only cause issues when:
The attribute value contain spaces, or
You forgot to add a space after the attribute value
Yet another alternative, just for kicks:
echo <<<EOL
<img src="{$media}" width="200" height="200" />
EOL;
aka a HEREDOC.
That is because that is not proper HTML.
In your code you'd get something like:
<img src=image.jpgwidth=200 height=200
And what you need is:
<img src="image.jpg" width="200" height="200" />
So do:
echo '<img src="' . $media . '" width="200" height="200" />';
echo '<img src="' . $media . '" width="200" height="200" />';
You should be able to make it work with the following code
echo "<img src=\"".$media."\" width=200 height=200>";