[英]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>";
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.