我已经关注了几篇文章并取得了进展,但是仍然无法使用位于www / SnapShots下我webroot文件夹中的html img src标签显示jpeg图像。
我想使用PHP搜索本地mysql数据库并返回最新的图像文件路径。然后,我想使用该文件路径来显示图像。
我已经尝试使用单独的getImage.php和showimage.html以及包含php和html的单个文件,我将在下面共享它们。目前,我能够回显正确的文件路径,当将其手动复制并粘贴到img src时可以正常工作,但是通过引用变量或.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>
我尝试了多种方法来插入$ imgpath变量,或从单独的html页面(即:)引用它。
朝正确方向的观点将不胜感激。
截至目前,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>
请使用双引号代替:
// code
echo "<img src='{$imgpath}' alt='Most recent WebCam Shot' width='800' height='600'><br />";
PHP不会替换单引号字符串内的变量。
更新
如果我对您的问题很了解,则希望阅读图像的内容并在<img>
标签中使用它。
您必须将代码分成两个文件(我将简化说明):
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);
这是您可以采用的方法。
我建议您从一个简单的示例开始:
创建一个包含三个文件的文件夹:
名为image.jpg的示例图像
一个带有以下代码的名为index.html的HTML文件:
<img src="view_image.php">
还有一个名为view_image.php的文件,其内容如下:
<?php
header("content-type: image/jpeg");
readfile("image.jpg");
请注意,必须在您的Web服务器中启用php。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句