-1

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.

Nick
  • 138,499
  • 22
  • 57
  • 95
Abdulsalam Elsharif
  • 4,773
  • 7
  • 32
  • 66

4 Answers4

2

Try with PHP tags like below:-

<img id="img1" width="150" height="150" src="<?php echo $src;?>"/>
Akshay Paghdar
  • 3,599
  • 2
  • 22
  • 41
2

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; ?>"/>
Nick
  • 138,499
  • 22
  • 57
  • 95
2

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.

sassy_rog
  • 1,077
  • 12
  • 30
2

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 ?>"/>
Hemant Sharma
  • 259
  • 5
  • 14