PHP 显示数据库中的图像(中等 blob)
我目前有一个数据库,可以在其中存储图像。 但是我创建的用于显示它们的 PHP 页面...不起作用。 但是当我使用链接作为
我想要显示图像的方式是: < img src="image.php?image="'.$imageID.'/>
我用于显示图像(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'];
}
}
?>
我已经尝试了很多,但它不起作用:( 我将代码更新到最新版本。 使用sql时犯了错误(没有选择任何内容/从不执行int的mysql_real_escape_string)
现在它显示一条直线字符,而不是图像,这至少是一个改进...
有人可以帮助我吗?
感谢您的宝贵时间!
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
the way i want to display the images is:
< img src="image.php?image="'.$imageID.'/>
the code i use for displaying the image(image.php) is:
<?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)
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
老实说,我知道这可能很棘手。我认为这与你如何存储它有关,而不是你如何输出它。我可以展示最近一个项目中的一个存储示例。
不确定您是否对 file_get_contents 执行类似操作。我的输出方式与你的输出方式类似,只是我使用了 ORM 和框架。
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.
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.
我怀疑你的问题在于你的转换 - 也许是在字符串和图像之间,或者可能是在不同的图像格式之间。
您是否尝试过保存通过 image.php 获取的图像,并将二进制文件与已知良好的图像文件进行比较?也许一旦您这样做了,答案就会显而易见(例如,长度错误、标头损坏等)。
您可能需要考虑不同的数据库记录。大多数数据库支持存储二进制 blob 数据,您可以更简单地返回这些数据。这比使用 php 函数进行 stringify 和 imagecreatefromstring 更简单(并且在数据库中占用更少的空间!)。
另外,我相信您正在尝试使用 imagegif、imagejpeg、image 来为您执行图像格式转换。根据我的阅读,这不是我理解它们的工作方式: http:// php.net/manual/en/function.imagecreatefromstring.php。尝试存储和检索相同的格式,以弄清楚这是否是您的问题。
尝试查看内容类型。我认为它区分大小写。尝试内容类型。
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
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.)
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.
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.
Try looking at Content-type. I think it's case sensitive. Try Content-Type.
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields