1

I have followed several posts and made progress, but I still am unable to display a jpeg image using the html img src tags which is located in my webroot folder under www/SnapShots.

I would like to use PHP to search a local mysql db and return the newest image filepath. I would then like to use that filepath to display the image.

I have tried using both a separate getImage.php and showimage.html as well as as a single file containing both php and html which I will share below. Currently I am able to echo the correct filepath, which when copied and pasted manually into an img src works, however it does not work by referencing the variable or .php url.

webcam.php:

<?php
echo '<!DOCTYPE html>';
echo '<html>';
echo '<body>';
echo '<h1>Display Recent WebCam Image</h1><br />';

//$id = $_GET['id'];
//Connect to mysql DB
$link = mysql_connect("localhost", "root", "password");
mysql_select_db("temperature_database");
//Select thew newest image filepath
$sql = "SELECT imgFilePath FROM tempLog WHERE datetime=(SELECT MAX(tempLog.datetime) FROM tempLog);";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);

//header("Content-type: image/jpeg");
header("Content-type: text/plain");
$imgpath = $row['imgFilePath'];
//echo $imgpath;


echo '<img src="{$imgpath}", alt="Most recent WebCam Shot", width="800", height="600"><br />';
?>
</body>
</html>

I have tried numerous ways of inserting the $imgpath variable and or referencing it from a separate html page ie: .

A point in the right direction would be much appreciated.

As of now webcam.php outputs the following, so there also appears to be an issue with interpreting the html tags.

<!DOCTYPE html><html><body><h1>Display Recent WebCam Image</h1><br /><img src="{$imgpath}", alt="Most recent WebCam Shot", width="800", height="600"><br /></body>

0

2 Answers 2

3

Use double quotes instead:

// code
echo "<img src='{$imgpath}' alt='Most recent WebCam Shot' width='800' height='600'><br />";

PHP does not replace variables inside single quoted strings.

Update

If I understand well your question, you want to read the contents of a image and use it in a <img> tag.

You have to split the code in two files (I'll illustrate with a simplification):

index.html

<p>Webcam image:</p>
<img src="view_image.php">

view_image.php

<?php
$image_path = function_to_get_image_path();
header("Content-type: image/jpeg");
readfile($image_path);

This is the approach you could take.

I recommend you start with an easy sample:

Create a folder with three files:

A sample image called image.jpg

A HTML file called index.html with the following code:

<img src="view_image.php">

And a file called view_image.php with this content:

<?php 
header("content-type: image/jpeg");
readfile("image.jpg");

Note that php has to be enabled in your web server.

Sign up to request clarification or add additional context in comments.

3 Comments

Won't he have to escape the "s
Thanks for the advice, I did not know that about the single quotes. With that change and while also using backslash to escape the other " for the html tags i now receive the literal html markup and the variable is resolved to the proper text when accessing the page. Is there a reason why the html would not be interpreted?
Figured it out. After removing the header content-type text/plain the page now displays the image and interprets the html. I suppose since I am also using html on this .php file i should not define any header content type?
0

You can also use it like this:

echo '<img src="'.{$imgpath}.'", alt="Most recent WebCam Shot", width="800", height="600"><br />';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.