簡體   English   中英

使用PHP獲取圖像文件路徑並以HTML格式顯示

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

我已經關注了幾個帖子並取得了進展,但我仍然無法使用位於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。

您也可以像這樣使用它:

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