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>";
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 ?>"/>
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.
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" />';
}
?>
Pretty straight forward simple question, can you open a php code block to call image information in html? I don't think I phrased that right. Here is my code:
<img src="../inventory_images/' . <?php echo $item_number; ?> . '.jpg" width="150" height="150" border="2" />
This code is within the tags
I'm just trying to post a photo using the $item_number variable (which is also the name of the image file i.e. $item_number = T3144 and the image file is name T3144.jpg ). Also if there is a better way to accomplish this suggestions are happily accepted. Sorry to take up bandwidth with such a remedial question but for some reason I can't seem to answer this question in research. Thanks for taking the time everyone.
Your code is wrong, try:
<img src="../inventory_images/<?php echo $item_number;?>.jpg" width="150" height="150" border="2" />
with what you have it looks like the code you had would print
src="../inventory_images/' . whateveritem_numberis . '.jpg"
Yes that is perfectly fine, but make sure that this code is in a file that ends with .php or it will not get parsed by PHP. Also, you need to take out the single quotes and periods:
<img src="../inventory_images/<?php echo $item_number; ?>.jpg" width="150" height="150" border="2" />
Unless the above HTML is in an echo statement, you need to change it to this:
<img src="../inventory_images/<?php echo $item_number; ?>.jpg" width="150" height="150" border="2" />
That will in-turn look like this:
<img src="../inventory_images/T3144.jpg" width="150" height="150" border="2" />
Of course, that is going off of your example where $item_number = 'T3144';.
The single quotes and periods are used for concatenating variables inside of strings.
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