Echo not displaying image correctly - php

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]

Related

How to add "../" into a PHP echo img src

In my code I show some images in this way:
...
<?php echo '<img src="' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="' .$imagetwo. '" alt="" /> '?></a></div>
...
where $imageone, $imagetwo have path from Mysql database
e.g.
$imagesone = "images/exposition/02-05-2017-11-28-00-foto 2_b.JPG";
This code is working in the same place where I have my images folder, but now I need to put the same code in a subfolder page and I'd like to use the same image.
So, I need to put dinamically "../" before my varible, something like this:
...
<?php echo '<img src="../' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="../' .$imagetwo. '" alt="" /> '?></a></div>
...
but it's not working.
Any suggestion?
EDIT This is my solution:
if ($images1 != ""){
$imageone = "../".$images1;
}
if ($images2 != ""){
$imagetwo = "../".$images2;
}
in this way I fixed my code and it's working!
Since you're using double quotation marks after src, you can just write down <img src="../$imageone" alt="" />
There's no need to use simple quotation marks to write the variable because the double quotation marks gets the variable number.

Image Not Showing up in if statement

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" />';
}
?>

PHP variable with string

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(); ?>

Link to be displayed as imgsrc

I have a code here that outputs a image link like http://img.domain.com/2515.jpg
<?php echo IMG_URL . $code . ".jpg" ?>
But i want to make it print this entire thing <img src="http://img.domain.com/2515.jpg" alt="" title="Created by domain.com" />
How can i format that php string <?php echo IMG_URL . $code . ".jpg" ?>
to include that entire img src link?
I am trying to fit it in this html code below
<li><a class="linkInsert" data-value="<?php echo IMG_URL . $code . ".jpg" ?>">Direct Link (email & IM)</a></li>
Update:
i figured it out below with just using '
<li><a class="linkInsert" data-value='<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">'>HTML Image (websites / blogs)</a></li>
Try this,
<?php echo '<img src="'.IMG_URL.$code.'.jpg" alt="" title="Created by domain.com" />' ;?>
You just have to use single quotes and double quotes alternatively.
PHP can be embedded inside HTML:
<img src="<?php echo IMG_URL . $code ?>.jpg" alt="">

Setting size of an image in PHP

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>";

Categories