简体   繁体   English

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

[英]PHP display image from Database(medium blob)

I currently have a database where i can store images in.. But the PHP page i created to display them... doesnt work. 我目前有一个数据库,我可以存储图像..但我创建的PHP页面显示它们...不起作用。 But when i use the link as scr for a < img> it shows a empty image frame. 但当我使用链接作为scr的<img>它显示一个空的图像框架。 if i echo the image directly, i get a lot of strange signs 如果我直接回应图像,我会得到许多奇怪的迹象

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犯了错误(没有选择/永远不会做一个int的mysql_real_escape_string)

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! 谢谢你的时间!

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? 您是否尝试过保存通过image.php获取的图像,并将二进制文件与已知良好的图像文件进行比较? Perhaps once you do that, the answer will be apparent (eg wrong length, corrupted header, etc.) 也许一旦你这样做,答案就会很明显(例如错误的长度,错误的标题等)

You may want to consider a different database record. 您可能需要考虑不同的数据库记录。 Most db's support storage of binary blob data, which you could more simply return. 大多数db支持存储二进制blob数据,您可以更简单地返回。 This would be simpler (and take less space in your db!) than using the php functions to stringify and imagecreatefromstring. 这比使用php函数stringify和imagecreatefromstring更简单(并且在db中占用更少的空间!)。

Also, I believe that you're attempting to use imagegif, imagejpeg, imaging to perform image format conversion for you. 此外,我相信您正在尝试使用imagegif,imagejpeg,images来为您执行图像格式转换。 This isn't how I understand them to work, based on my reading at: http://php.net/manual/en/function.imagecreatefromstring.php . 根据我在http://php.net/manual/en/function.imagecreatefromstring.php上的阅读,这不是我理解它们的工作方式。 Try storing and retrieving the same format to tease out whether this is your problem. 尝试存储和检索相同的格式,以梳理出这是否是您的问题。

Try looking at Content-type. 尝试查看内容类型。 I think it's case sensitive. 我认为这是区分大小写的。 Try Content-Type. 尝试Content-Type。 http://en.wikipedia.org/wiki/List_of_HTTP_header_fields http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

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. 不确定您是否与file_get_contents类似。 How I output is similar to how you are doing it, only I use a ORM and a framework. 我的输出方式与您的输出类似,只是我使用ORM和框架。

$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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM