insert image with session - php

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

Related

Assign Image SRC in 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 ?>"/>

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.

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

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

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