简体   繁体   English

使用PHP获取图像文件路径并以HTML格式显示

[英]Get image filepath using PHP and display in HTML

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. 我已经关注了几个帖子并取得了进展,但我仍然无法使用位于www / SnapShots下的webroot文件夹中的html img src标签显示jpeg图像。

I would like to use PHP to search a local mysql db and return the newest image filepath. 我想使用PHP搜索本地mysql数据库并返回最新的图像文件路径。 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. 我已经尝试使用单独的getImage.php和showimage.html以及包含php和html的单个文件,我将在下面分享。 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. 目前我能够回显正确的文件路径,当手动复制和粘贴到img src时,它会起作用,但是它不能通过引用变量或.php url来工作。

webcam.php: 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: . 我已尝试过多种插入$ imgpath变量的方法,或者从单独的html页面引用它,即:。

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. 截至目前,webcam.php输出以下内容,因此解释html标签似乎也存在问题。

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

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. PHP不替换单引号字符串中的变量。

Update 更新

If I understand well your question, you want to read the contents of a image and use it in a <img> tag. 如果我理解你的问题,你想要阅读图像的内容并在<img>标签中使用它。

You have to split the code in two files (I'll illustrate with a simplification): 你必须将代码分成两个文件(我将简化说明):

index.html 的index.html

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

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 一个名为image.jpg的示例图像

A HTML file called index.html with the following code: 一个名为index.html的HTML文件,代码如下:

<img src="view_image.php">

And a file called view_image.php with this content: 还有一个名为view_image.php的文件, 其中包含以下内容:

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

Note that php has to be enabled in your web server. 请注意,必须在Web服务器中启用php。

您也可以像这样使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM