未從數據庫接收圖像

[英]Image not being received from database


All the other information being stored within the database is on the website just not the image which is a BLOB image. Database holds the image as if you press it will show the image being stored. Any appreciated!

存儲在數據庫中的所有其他信息都在網站上,而不是作為BLOB圖像的圖像。數據庫保存圖像,就像按下它一樣,將顯示正在存儲的圖像。任何贊賞!

GetImage.php

GetImage.php

<?php
include "connection.php";

$ID = $_GET["ID"];

$SQLCommand = "SELECT * FROM products WHERE ID = '$ID'";
$Result = mysqli_query($con, $SQLCommand);

$row = mysqli_fetch_assoc($Result);

header("Content-type:" . $row["Name"]);
echo $row["Image"];

?>

shop.php

shop.php

<?php
include "connection.php";

$SQLCommand = "SELECT * FROM products";
$Result = mysqli_query($con, $SQLCommand);

while($row = mysqli_fetch_assoc($Result)){
echo "<p id='p1'>" . $row["Name"] .
     "<br></p><p id='p3'>£" . $row["Price"] .
              "<br><br><img src=GetImage.php?ID=" . $row["ID"] . "         width=10%>" .
         "<br><a href=productdetails.php?ID=" . $row["ID"] . ">Details</a></p>".
         "<p id='p2'><a href=BasketAdd.php?ID=" . $row["ID"] . ">Add to     Basket</a><br><br>";
}
?>

InsertImageform

InsertImageform

    <html>
<body>
<form action="UploadFile.php" method="post"

enctype="multipart/form-data">

ENCTYPE =“多部分/格式數據”>

Name: <input type="text" name="name" id="name"><p>
Description: <input type="text" name="description" id="description"><p>
Price: <input type="text" name="price" id="price"><p>
Protein: <input type="text" name="protein" id="protein"><p>
Type: <input type="text" name="type" id="type"><p>

Filename: <input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

2 个解决方案

#1


0  

Instead you could use,

相反,你可以使用,

<?php
    include "connection.php";

    $SQLCommand = "SELECT * FROM products";
    $Result = mysqli_query($con, $SQLCommand);

    while($row = mysqli_fetch_assoc($Result)){
    echo "<p id='p1'>" . $row["Name"] .
         "<br></p><p id='p3'>£" . $row["Price"] .
                  "<br><br><img src=" . base64_encode($row['image']) . "         width=10%>" .
             "<br><a href=productdetails.php?ID=" . $row["ID"] . ">Details</a></p>".
             "<p id='p2'><a href=BasketAdd.php?ID=" . $row["ID"] . ">Add to     Basket</a><br><br>";
    }
?>

You should use the base64_encode() function to read the BLOB image.

您應該使用base64_encode()函數來讀取BLOB圖像。

#2


0  

The problem with images in a database are generally related to how the image was stored in the database.

數據庫中圖像的問題通常與圖像存儲在數據庫中的方式有​​關。

The image can be stored as a binary, but it is less problematic if stored as base64.

圖像可以存儲為二進制文件,但如果存儲為base64,則問題較少。

What I would need to see is the code that stores the image in the database.
Given a file input with a name of "image"

我需要看到的是將圖像存儲在數據庫中的代碼。給定名為“image”的文件輸入

<input type="file" name="image">

Then

然后

if( is_uploaded_file($_FILES['image']['tmp_name']) || !($_FILES['image']['error'] !== UPLOAD_ERR_OK)){
   $img = base64_encode($_FILES['image']);
   $sql = "INSERT INTO `images`(`image`) VALUES('$img')";
   ...
 }

Or as an update:

或者作為更新:

UPDATE `table` SET `image`='$img' WHERE ...

In the table the field image would have a type of "TEXT"

在表格中,字段圖像的類型為“TEXT”

BUT THAT IS STILL NOT ENOUGH...

但仍然沒有......

You need to know the dimensions and the type of image.

您需要知道圖像的尺寸和類型。

if( is_uploaded_file($_FILES['image1']['tmp_name']) || !($_FILES['image1']['error'] !== UPLOAD_ERR_OK)){
  $save = false;
  switch(strtolower($_FILES['image1']['type'])){
  case 'image/jpeg':
    $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/png':
    $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
    if ($image !== false){$save = true;break;}
  case 'image/gif':
    $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
    if ($image !== false){$save = true;break;}
  default:
    $img = @getimagesize($_FILES['image']['tmp_name']);
    switch(strtolower($img['mime'])){
    case 'image/jpeg':
      $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
      $type = 'jpg';
      if ($image !== false){$save = true;break;}
    case 'image/png':
      $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
      $type = 'png';
      if ($image !== false){$save = true;break;}
    case 'image/gif':
      $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
      $type = 'gif';
      if ($image !== false){$save = true;break;}
    default:
      $filename = $_FILES['image']['name'];
      $ext = substr($filename,-3);
      switch(strtolower($ext)){
      case 'jpg':
        $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'gif':
        $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      case 'png':
        $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      default:
        $image = @imagecreatefromjpeg($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefrompng($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
        $image = @imagecreatefromgif($_FILES['image']['tmp_name']);
        if ($image !== false){$save = true;break;}
      }
    }
  }
}

Get width and height

獲得寬度和高度

$width  = imagesx($image);
$height = imagesy($image);
$base64 = base64_encode($image);

Put it all together for an HTML <img>

把它們放在一起用於HTML

$img = "<img  width=\"$width \" height=\"$height\" src=\"data:image/$type;base64,$base64\" alt =\"image\"/>";

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/05/13/ed4a5b1bf62e638ab13f38bcdb92ddaf.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com