来自数据库的PHP显示图像(中blob)

时间:2022-09-25 16:43:38

I currently have a database where i can store images in.. But the PHP page i created to display them... doesnt work. But when i use the link as scr for a < img> it shows a empty image frame. if i echo the image directly, i get a lot of strange signs

我现在有一个可以存储图像的数据库。但是我创建的用于显示它们的PHP页面……不工作。但是当我使用链接作为< img>的scr时,它会显示一个空的图像帧。如果我直接对图像进行回波,就会得到很多奇怪的信号

the way i want to display the images is: < img src="image.php?image="'.$imageID.'/>

我想要显示图像的方式是:< img src="image.php?image=" .$imageID.'/>

the code i use for displaying the image(image.php) is:

我用于显示图像(image.php)的代码是:

 <?php session_start();
 if(!isset($_GET['image']))
 {
     header("HTTP/1.0 404 Not Found");
 }
 else{
     $link = mysql_connect('localhost', '45345345435435345', 'wewfsersdfds');
            if (!$link) {
                echo "error";
                die;
            }

            $db_selected = mysql_select_db(Tha base, $link);
            if (!$db_selected) {
                echo "error";
                die;
            }
 $nummer=(int)trim(stripslashes($_GET['image']));
 $mynr=$nummer;
 $sql="SELECT * FROM Foto WHERE fotonr=$mynr";
 $result=mysql_query($sql);
 if(!$result)
 {
     echo "error".mysql_error();
     header("HTTP/1.0 404 Not Found");
 }
 else
 {

     $foto = mysql_fetch_assoc($result);

     header("Content-type: ".$foto['type']);
     echo $foto['image'];


 }
 }
 ?>

I tried a lot already but it wont work :( i updated the code to the newest version. Made mistakes with the sql(selected nothing/never do mysql_real_escape_string of a int)

我已经尝试了很多,但是没有用:(我把代码更新到最新的版本。在sql中出错(没有选择/从不执行mysql_real_escape_string的int)

Now it shows a straight line of characters, instead of an image, thats at least a improvement...

现在它显示的是一行字符,而不是图像,这至少是一种改进……

Can someone help me?

有人能帮助我吗?

Thanx for your time!

谢谢你的时间!

3 个解决方案

#1


2  

Honestly, I know it can be tricky. I think it has to do with how your storing it, not how your outputting it. I have one example of storage from a recent project I can show.

老实说,我知道这很棘手。我认为这与你如何储存它有关,而不是你如何输出它。我有一个最近项目的存储示例。

$fp = fopen($image_path, 'r');
$image = fread($fp, filesize($image_path));
$image = addslashes($image);
fclose($fp);

// save $image into DB.

Not sure if you do similar with file_get_contents. How I output is similar to how you are doing it, only I use a ORM and a framework.

不确定是否对file_get_contents执行类似操作。我如何输出类似于您所做的,只有我使用ORM和一个框架。

#2


2  

I suspect that your problem is in your conversion - perhaps between strings and images, or perhaps between the different image formats.

我怀疑您的问题在于转换——可能是在字符串和图像之间,也可能是在不同的图像格式之间。

Have you tried saving the image gotten through image.php, and comparing the binary against the known-good image file? Perhaps once you do that, the answer will be apparent (e.g. wrong length, corrupted header, etc.)

你试过保存图片了吗?php,比较二进制文件和已知的图像文件?也许一旦你这样做了,答案就会很明显(例如错误的长度、损坏的标题等等)。

You may want to consider a different database record. Most db's support storage of binary blob data, which you could more simply return. This would be simpler (and take less space in your db!) than using the php functions to stringify and imagecreatefromstring.

您可能需要考虑不同的数据库记录。大多数db支持二进制blob数据的存储,您可以更简单地返回这些数据。这将比使用php函数stringify和imagecreatefromstring更简单(并且在db中占用的空间更少)。

Also, I believe that you're attempting to use imagegif, imagejpeg, imaging to perform image format conversion for you. This isn't how I understand them to work, based on my reading at: http://php.net/manual/en/function.imagecreatefromstring.php. Try storing and retrieving the same format to tease out whether this is your problem.

另外,我相信您正在尝试使用imagegif、imagejpeg、imaging来为您执行图像格式转换。根据我在http://php.net/manual/en/function.imagecreatefromstr.php上的阅读,这不是我理解它们工作的方式。尝试存储和检索相同的格式,以查明这是否是您的问题。

Try looking at Content-type. I think it's case sensitive. Try Content-Type. http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

试着看内容类型。我认为它是区分大小写的。试内容类型。http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

#3


0  

$sql = "SELECT * FROM Foto WHERE fotonr=$mynr";
        $result = $conn -> query($sql);
        while ($row = $result -> fetch_assoc()) {
        $img= '<img src="data:image/jpeg;base64,'.base64_encode(                        $row['image'] ).'"  width=350px0px height=300px/>';
}
echo $img;

#1


2  

Honestly, I know it can be tricky. I think it has to do with how your storing it, not how your outputting it. I have one example of storage from a recent project I can show.

老实说,我知道这很棘手。我认为这与你如何储存它有关,而不是你如何输出它。我有一个最近项目的存储示例。

$fp = fopen($image_path, 'r');
$image = fread($fp, filesize($image_path));
$image = addslashes($image);
fclose($fp);

// save $image into DB.

Not sure if you do similar with file_get_contents. How I output is similar to how you are doing it, only I use a ORM and a framework.

不确定是否对file_get_contents执行类似操作。我如何输出类似于您所做的,只有我使用ORM和一个框架。

#2


2  

I suspect that your problem is in your conversion - perhaps between strings and images, or perhaps between the different image formats.

我怀疑您的问题在于转换——可能是在字符串和图像之间,也可能是在不同的图像格式之间。

Have you tried saving the image gotten through image.php, and comparing the binary against the known-good image file? Perhaps once you do that, the answer will be apparent (e.g. wrong length, corrupted header, etc.)

你试过保存图片了吗?php,比较二进制文件和已知的图像文件?也许一旦你这样做了,答案就会很明显(例如错误的长度、损坏的标题等等)。

You may want to consider a different database record. Most db's support storage of binary blob data, which you could more simply return. This would be simpler (and take less space in your db!) than using the php functions to stringify and imagecreatefromstring.

您可能需要考虑不同的数据库记录。大多数db支持二进制blob数据的存储,您可以更简单地返回这些数据。这将比使用php函数stringify和imagecreatefromstring更简单(并且在db中占用的空间更少)。

Also, I believe that you're attempting to use imagegif, imagejpeg, imaging to perform image format conversion for you. This isn't how I understand them to work, based on my reading at: http://php.net/manual/en/function.imagecreatefromstring.php. Try storing and retrieving the same format to tease out whether this is your problem.

另外,我相信您正在尝试使用imagegif、imagejpeg、imaging来为您执行图像格式转换。根据我在http://php.net/manual/en/function.imagecreatefromstr.php上的阅读,这不是我理解它们工作的方式。尝试存储和检索相同的格式,以查明这是否是您的问题。

Try looking at Content-type. I think it's case sensitive. Try Content-Type. http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

试着看内容类型。我认为它是区分大小写的。试内容类型。http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

#3


0  

$sql = "SELECT * FROM Foto WHERE fotonr=$mynr";
        $result = $conn -> query($sql);
        while ($row = $result -> fetch_assoc()) {
        $img= '<img src="data:image/jpeg;base64,'.base64_encode(                        $row['image'] ).'"  width=350px0px height=300px/>';
}
echo $img;