dqkf36241 2010-01-22 07:27
浏览 31

从SQL数据库显示图像

I've recently been working on a static content handler project, and this is hitting me hard. I cannot get the images to show up in the browser the same way I could before. The image is correct, and it's impossible because my code pretty much stayed the same.

ob_start();

function ImageExistsSql($img)
{
 $rows = mssql_num_rows(mssql_query("SELECT id FROM Static_Image WHERE name = N'{$img}'"));
 if($rows > 0)
 {
  return true;
 }
 else
 {
  return false;
 }

}

function ImageExistsFile($img)
{
 if(!file_exists("images/".$img))
 {
  return false;
 }
 else
 {
  return true;
 }
}

function CreateImageFile($img)
{
 if(!ImageExistsFile($img))
 {
  $b =& new hex2bin;
  $data = mssql_fetch_row(mssql_query("SELECT data FROM Static_Image WHERE name = N'{$img}'"));
  $file = fopen("images/".$img, 'a+');
  fwrite($file, $b->convert($data));
  fclose($file);
 }

}

function GetExtension($img)
{
 return mssql_fetch_row(mssql_query("SELECT extension FROM Static_Image WHERE name = N'{$img}'"));
}

function WriteImageDataToScreen($img)
{
 $r = mssql_fetch_row(mssql_query("SELECT data FROM Static_Image WHERE name = N'{$img}'"));
 $d = str_split($r);

 for($i = 0; $i < strlen($r); $i++)
 {
  echo($d[$i]);
 }
}

function ContentHeader($img)
{
 $ext = GetExtension($img);
 switch($ext[0])
 {
  case "png":
  case ".png":
   header("Content-type: image/png");
   break;

  case "gif":
  case ".gif":
   header("Content-type: image/gif");
   break;

  case "jpg":
  case "jpeg":
  case ".jpg":
  case ".jpeg":
   header("Content-type: image/jpeg");
   break;

  default:
   die("[STATIC HANDLER] Extension: {$ext} not found");
   break;

 }
}

if(isset($_GET['i']))
{
 $imgf = $_GET['i'];
 $ext = GetExtension($imgf);

 if(ImageExistsSql($imgf))
 {
  if(ImageExistsFile($imgf))
  {
   ContentHeader($imgf);
   //$fn = fopen("images/".$imgf, 'rb');
   //fpassthru($fn);
                    // fpassthru isn't working anymore, used to work.
   WriteImageDataToScreen($imgf);
  }
  else
  {
   CreateImageFile($imgf);
   header("Location: {$_SERVER['REQUEST_URI']}");
  }
 }
 else
 {
  die("[STATIC HANDLER] IMAGE NON EXISTANT");
 }
}
else
{
 die("[STATIC HANDLER] FAILED TO PROCESS FILE");
} 

ob_end_flush();

The table Static_Image is as follows

CREATE TABLE [dbo].[Static_Image](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [name] [varchar](50) NOT NULL,
 [extension] [varchar](6) NULL,
 [dateadded] [date] NULL,
 [data] [image] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

the data column is used, however, wasn't being used until recently as a way to expand this onto multiple servers.

  • 写回答

1条回答 默认 最新

  • duanjie6912 2010-01-22 07:33
    关注

    in

    $data = mssql_fetch_row(mssql_query("SELECT data FROM Static_Image WHERE name = N'{$img}'"));
    $file = fopen("images/".$img, 'a+');
    fwrite($file, $b->convert($data));
    

    should that be $b->convert($data[0]) ?

    note, it's often easier to store the image on disk exclusively, and just keep the pathname in the db...

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?