将png图像加载到mysql表/数据库中 [英] Loading png images into a mysql table/database

查看:137
本文介绍了将png图像加载到mysql表/数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在建立一个照片网站。我决定使用PHP和MySQL。

我可以将jpeg文件加载到表中(中等blob,甚至是longtext)

并获取要显示的图像没问题。我正在使用

chunk_split(数据)以及文件上的base64_encode和base64_decode。


我从数据库中选择,然后回显图像(用

标题(内容类型:image / jpeg)

并且解码后的图像显示正常。是的,我已尝试过标题(内容

类型:image / png),没有

成功。


我的问题是png图像。有什么我需要做的事情

在读取/编码/解码png图像方面有什么不同?我将它的二进制数据放入db表中。

但是当我选择时在png图像上 - 它永远不会显示在

浏览器中。


我没有使用文件/路径名作为参考,但实际上是把

数据库中的图像。


这是我用来将数据插入表中的代码示例,并且

来选择表中的代码。


加载图片:

<?php

wh ile($ file = readdir($ dir_handle))

{

$ filetyp = substr($ file,-3);

if(b) $ filetyp ==''png''或$ $ filetyp ==''jpg'')

{

$ handle = fopen($ path。 " /" 。 $ file,''r'');

$ file_content = fread($ handle,filesize($ path。" /"。$ file));

fclose($ handle);


$ encoded = chunk_split(base64_encode($ file_content));

$ sql =" INSERT INTO images SET sixfourdata =' '$ encoded''" ;;

mysql_query($ sql);

}

}

?> ;


显示图片:


<?php

$ result = @mysql_query(" SELECT * FROM images WHERE id ="。$ img。"");


if(!$ result)

{

echo("执行查询时出错:" .mysql_error()。"");

exit();

}


while($ row = mysql_fetch_array($ result))

{

$ imgid = $ row [" id"];

$ encodeddata = $ row [" sixfourdata"];

}

?>


< ?php

// echo base64_decode($ encodeddata);

echo $ encodeddata;

?>


这个过程似乎总是适用于jpegs。


谢谢

ewholz

Hello Members,

I am setting up a photo website. I have decided to use PHP and MySQL.
I can load jpeg files into the table (medium blob, or even longtext)
and get the image(s) to display without a problem. I am using
chunk_split(data) and the base64_encode and base64_decode on the files.

I do a select from the database, and then echo the image (with
header(Content Type: image/jpeg)
and the decoded image displays fine. Yes, I have tried header(Content
Type: image/png), without
success.

My problem is png images. Is there something that I need to do
different in terms on reading/encodeing/decodeing the png image? I am
putting its "binary" data into the db table.
But when I do a select on a png image - it never displays in the
browser.

I am not using file/path names as references, but actually putting the
image in the db.

Here is a sample of code I use to insert the data into the table, and
to select the code from the table.

load image(s):
<?php
while ($file = readdir($dir_handle))
{
$filetyp = substr($file, -3);
if ($filetyp == ''png'' OR $filetyp == ''jpg'')
{
$handle = fopen($path . "/" . $file,''r'');
$file_content = fread($handle,filesize($path . "/" . $file));
fclose($handle);

$encoded = chunk_split(base64_encode($file_content));
$sql = "INSERT INTO images SET sixfourdata=''$encoded''";
mysql_query($sql);
}
}
?>

display images:

<?php
$result = @mysql_query("SELECT * FROM images WHERE id=" . $img . "");

if (!$result)
{
echo("Error performing query: " . mysql_error() . "");
exit();
}

while ( $row = mysql_fetch_array($result) )
{
$imgid = $row["id"];
$encodeddata = $row["sixfourdata"];
}
?>

<?php
//echo base64_decode($encodeddata);
echo $encodeddata;
?>

This process seems to always work with jpegs.

Thanks

ewholz

推荐答案

file = readdir(
file = readdir(


dir_handle))

{
dir_handle))
{


filetyp = substr(
filetyp = substr(


这篇关于将png图像加载到mysql表/数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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