PHP显示图像为BLOB mysqli [英] PHP show image as BLOB mysqli

查看:250
本文介绍了PHP显示图像为BLOB mysqli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在StackOverflow上阅读所有可能的问题,我问所有我知道的人,没有人可以帮助我。
我在数据库中有表:

  CREATE TABLE如果不存在`zdjecia`(
`id` int(11)NOT NULL,
`imie_wlasciciela` varchar(50)NOT NULL,
`zdjecie` blob NOT NULL,
`nazwa` varchar(50)NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 16

很抱歉没有英语变量,希望保持所有源代码不变。
imie_wlasciciela nazwa 在我的问题中不重要。
所以 zdjecie 是图像的二进制数据
id 是id。 >

我有一个应该显示图片的文件。



// single_image.php

 <?php 

if(isset($ _ GET ['id']))
{
include ('../connect.php'); // connect to database
$ id = $ _GET ['id'];
$ ret = $ conn-> query(SELECT * FROM zdjecia WHERE id = $ id);
$ row = $ ret-> fetch_assoc();
header(Content-type:image / png);
echo $ row ['zdjecie'];
// echo $ row ['nazwa']; //更正结果。
}
?>

在我的浏览器网址中,我粘贴... / single_image.php?id = 8



我的问题是不显示图像,我在数据库中有几张图片。我可以通过/ phpmyadmin在XAMPP下载它,它是正确的图像,但我的页面不能正确显示
每次我看到一个破碎的图像图标。



文件single_image.php设置为no-bomb和Code-Page1250(中欧)。



当我通过 $ row ['nazwa'] ,并注释头(Content-type ...)行显示正确 varchar(50)从数据库。



我使用它像上面几行或在html,< img src =single_image.php?id = 8/>

解决方案

尝试以下

  echo'< img src =data:image / png; base64,'。base64_encode($ row [' zdjecie'])。'/>'; 



为了更好的维护,你可以添加 data:image / png; base64 作为你的头,然后只有 echo base64_encode($ row ['zdjecie'])这将适用于 single_image.php?id = $ id


I read all possible questions on StackOverflow, I asked all people who I know and no one can help me. I have table in database:

CREATE TABLE IF NOT EXISTS `zdjecia` (
`id` int(11) NOT NULL,
  `imie_wlasciciela` varchar(50) NOT NULL,
  `zdjecie` blob NOT NULL,
  `nazwa` varchar(50) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16

I'm sorry for no English variables, but I want to keep all source without changes. imie_wlasciciela and nazwa aren't important in my problem. So zdjecie is a binary data of image And id is id.

I have a file which should show image.

// single_image.php

<?php

if (isset($_GET['id']))
{
    include('../connect.php'); // connect to database
    $id = $_GET['id'];
    $ret = $conn->query("SELECT * FROM zdjecia WHERE id=$id");
    $row = $ret->fetch_assoc();
    header("Content-type: image/png");
    echo $row['zdjecie'];
    // echo $row['nazwa']; // correct the result.
}
?>

And in my browser URL I paste.../single_image.php?id=8

My problem is a not displaying images, I have in database few images. I can download it by /phpmyadmin in XAMPP and it is correct image, but my page does not displaying it correctly, Every time I see a broken image icon.

File single_image.php is set to no-bomb and Code-Page 1250 (Central European).

When I check the result by $row['nazwa'], and commented header("Content-type...") line it's showing correct varchar(50) from database.

I'm using it like written a few lines above, or in html, <img src="single_image.php?id=8"/>

解决方案

Try below

echo '<img src="data:image/png;base64,'.base64_encode( $row['zdjecie'] ).'"/>';

For better maintenance you can add data:image/png;base64 as your header then only echo base64_encode( $row['zdjecie'] ) this will work for single_image.php?id=$id

这篇关于PHP显示图像为BLOB mysqli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆