簡體   English   中英

使用 <img> 標簽以調用PHP腳本以顯示從數據庫檢索到的編碼圖像

[英]using <img> tag to call PHP script for displaying encoded image retrieved from database

我的PHP腳本,用於將編碼的圖像數據作為“ blob”類型存儲在數據庫表中:

include './libraries/LIB_mysql.php'; # library for using the 'insert()' function at the end
$imageFilePath = "./pythonProgrammingProgram.PNG";
$imageFileRead = file_get_contents($imageFilePath); # == �PNG  IHDR�A ... uY�IEND�B`� (... and so on)
$imageEncode = base64_encode($imageFileRead); # iVBORw0KGgoAAAA (... and so on)
$data_array['image_id'] = 3;
$data_array['image_file'] = $imageEncode;
$table = 'images';
insert(DATABASE, $table, $data_array);

圖像數據似乎已正確存儲在數據庫中:

mysql> select * from images;
| image_id | image_file
|        3 | iVBORw0KGgoAAAA # (... and so on)

然后,我想使用'img'標簽在HTML頁面中顯示圖像,該標簽調用PHP腳本: <img src="./imageDisplay.php?pictureID=3"> PHP腳本imageDisplay.php從數據庫檢索圖像數據並回顯解碼的圖像:

header("Content-type: image/png");
include('./libraries/LIB_mysql.php'); # for using exe_sql() function
$image_id = $_GET['pictureID']; # == 3 -> corresponds to the value in the database !!
$sql = "SELECT image_file FROM images WHERE image_id = '" . $image_id . "'"; # $image_id";
list($img) = exe_sql(DATABASE, $sql); # $img contains the decoded data: == �PNG  IHDR�A ... uY�IEND�B`� (... and so on)
echo base64_decode($img);
exit;

在本地主機上加載HTML文件時,我看到了損壞的圖像。 在Firefox的“網絡”->“響應”下,我看到:

Name: imageDisplay.php
Dimensions: 0 × 0
MIME Type: image/png

我想圖像數據的傳輸沒有作用,但是我真的看不到為什么。 base64_decode($img)的已解碼圖像數據與編碼之前的$imageFileRead圖像數據相同,因此我想數據庫操作不是問題,而是echo base64_decode($img);某個位置echo base64_decode($img); <img src="./imageDisplay.php?pictureID=3"> 找到了這個(特別的問題)[ PHP-從img src和另一個(另外一個) 調用了一個函數 [ 無法顯示圖像,因為它包含錯誤,但仍然看不到如何解決它:-(

我會認真考慮重新將圖像存儲在數據庫中。 最好的方法是保存文件並在數據庫中具有圖像路徑。 如果絕對需要這樣做,請嘗試保存以下代碼:

$name = $_FILES['file']['name'];
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");

// Check extension
if( in_array($imageFileType,$extensions_arr) ){

// Insert record
$query = "insert into images(name) values('".$name."')";
mysqli_query($con,$query);

// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);

並顯示此代碼

$sql = "select name from images where id=1";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);

$image = $row['name'];
$image_src = "upload/".$image;

?>
<img src='<?php echo $image_src;  ?>' >

此代碼從此示例中提取-http: //makitweb.com/upload-and-store-an-image-in-the-database-with-php/#base64

您將需要對其進行修改以使用您的代碼。

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" accept="png/jpeg/jpg/image"><br><br>
<input type="submit" value="submit" name="submit">
</form>
<?php 
 if(isset($_POST['submit'])){`enter code here`
    $name       = $_FILES['file']['name'];  `enter code here`
    $temp_name  = $_FILES['file']['tmp_name'];  
     // $allowed = array("pdf" => "pdf");
     // var_dump($name,$temp_name);
    if(isset($name)){
        if(!empty($name)){      
            $location = './uploads1/';      
            if(move_uploaded_file($temp_name, $location.$name)){
            //var_dump($temp_name,$location.$name);
                echo 'File uploaded successfully';

                $con=mysqli_connect("localhost","root","");
echo "connect";
if(!$con)
{
die("not connect");
}
mysqli_select_db($con,"dstpurse"); 


$query="INSERT INTO `image_upload`(`name`,`path`) VALUES('$name','$location')";

$result=mysqli_query($con,$query);
if($result)
{
echo "inserted";
}
else
{
echo "not inserted";
}

            }
        }       
    }  else {
        echo 'You should select a file to upload !!';
    }
    $query1 = "SELECT * FROM `image_upload`";
$result1 = mysqli_query($con,$query1) or die("query failed:".mysqli_error());
while($row=mysqli_fetch_array($result1))
{
var_dump($name); 

echo "<a href=".$location.$name.">dowenload</a>";

}


}

?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" accept="png/jpeg/jpg/image"><br><br>
<input type="submit" value="submit" name="submit">
</form>
<?php 
 if(isset($_POST['submit'])){`enter code here`
    $name       = $_FILES['file']['name'];  `enter code here`
    $temp_name  = $_FILES['file']['tmp_name'];  
     // $allowed = array("pdf" => "pdf");
     // var_dump($name,$temp_name);
    if(isset($name)){
        if(!empty($name)){      
            $location = './uploads1/';      
            if(move_uploaded_file($temp_name, $location.$name)){
            //var_dump($temp_name,$location.$name);
                echo 'File uploaded successfully';

                $con=mysqli_connect("localhost","root","");
echo "connect";
if(!$con)
{
die("not connect");
}
mysqli_select_db($con,"dstpurse"); 


$query="INSERT INTO `image_upload`(`name`,`path`) VALUES('$name','$location')";

$result=mysqli_query($con,$query);
if($result)
{
echo "inserted";
}
else
{
echo "not inserted";
}

            }
        }       
    }  else {
        echo 'You should select a file to upload !!';
    }
    $query1 = "SELECT * FROM `image_upload`";
$result1 = mysqli_query($con,$query1) or die("query failed:".mysqli_error());
while($row=mysqli_fetch_array($result1))
{
var_dump($name); 

echo "<a href=".$location.$name.">dowenload</a>";

}


}

?>

嘗試顯示來自 PHP 腳本的回顯圖像和 ` <img src="“…”`" tag< div><div id="text_translate"><p> 目標:</p><p> 1)我在 web 根目錄之外有一個圖像文件夾,其中放置了來自 web 應用程序的所有圖像(每個圖像路徑都保存在數據庫表中);</p><p> 2) 我想根據用戶的請求獲取該圖像。</p><p> 很簡單,對吧?</p><p> 我做了:</p><p> a) 創建一個 class 來讀取圖像(具有我記得的所有安全功能和保護措施)。 它(成功地)返回使用相應的 mime 類型編碼(base64)的圖像。</p><p> b) 出於演示目的,我創建了一個 index.php,它具有:</p><p> &lt;img src="agivenscript.php?img=name.jpeg"&gt;</p><p> 我在這里唯一的目標是從我的 web 應用程序中的任何其他地方調用一個腳本,該腳本輸出具有相應 mime 類型的 base64 編碼圖像。</p><p> c) 創建了一個腳本“agivenscript.php”,它通過 GET 接收圖像名稱,實例化我的 class 並在一切順利時獲取 base64 編碼圖像。 然后它與圖像相呼應。</p><p> 怎么了:</p><ul><li> 當我做 output - 使用 echo '&lt;img src="'. $output64encodedwithmimetype. '"&gt;'; - 在 agivenscript.php 中,它就像一個魅力;</li><li> 此外,如果我將 $output64encodedwithmimetype 內容直接放在 index.php 的 src 標記中,它也可以工作。</li><li> 但是,當我嘗試&lt;img src="agivenscript.php?img=name.jpeg"&gt;時它不起作用。</li></ul><p> 由於 base64 編碼的圖像顯然很好(並且具有 mime 類型),我錯過了什么? 任何想法?</p><p> 預先感謝。</p></div>

[英]Trying to display an echoed image from a PHP script in and `<img src=“…”` tag

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 顯示使用PHP從數據庫檢索的圖像 html<img> 標簽未在瀏覽器中使用 php 顯示圖像 使用PHP從數據庫檢索到的數據未以HTML格式顯示 如何使用PHP腳本將圖像文件單獨顯示為從MySQL數據庫檢索的鏈接 嘗試顯示來自 PHP 腳本的回顯圖像和 ` <img src="“…”`" tag< div><div id="text_translate"><p> 目標:</p><p> 1)我在 web 根目錄之外有一個圖像文件夾,其中放置了來自 web 應用程序的所有圖像(每個圖像路徑都保存在數據庫表中);</p><p> 2) 我想根據用戶的請求獲取該圖像。</p><p> 很簡單,對吧?</p><p> 我做了:</p><p> a) 創建一個 class 來讀取圖像(具有我記得的所有安全功能和保護措施)。 它(成功地)返回使用相應的 mime 類型編碼(base64)的圖像。</p><p> b) 出於演示目的,我創建了一個 index.php,它具有:</p><p> &lt;img src="agivenscript.php?img=name.jpeg"&gt;</p><p> 我在這里唯一的目標是從我的 web 應用程序中的任何其他地方調用一個腳本,該腳本輸出具有相應 mime 類型的 base64 編碼圖像。</p><p> c) 創建了一個腳本“agivenscript.php”,它通過 GET 接收圖像名稱,實例化我的 class 並在一切順利時獲取 base64 編碼圖像。 然后它與圖像相呼應。</p><p> 怎么了:</p><ul><li> 當我做 output - 使用 echo '&lt;img src="'. $output64encodedwithmimetype. '"&gt;'; - 在 agivenscript.php 中,它就像一個魅力;</li><li> 此外,如果我將 $output64encodedwithmimetype 內容直接放在 index.php 的 src 標記中,它也可以工作。</li><li> 但是,當我嘗試&lt;img src="agivenscript.php?img=name.jpeg"&gt;時它不起作用。</li></ul><p> 由於 base64 編碼的圖像顯然很好(並且具有 mime 類型),我錯過了什么? 任何想法?</p><p> 預先感謝。</p></div> 使用php無法將圖像從數據庫檢索到html表單中 使用img src從PHP顯示的圖像獲取圖像 img標簽顯示文件夾中的img但背景圖片不顯示? 使用php從html img標簽獲取圖像url 使用PHP從數據庫顯示圖像
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM