如何在PHP中显示来自mysql blob的图像(joomla)

时间:2011-02-08 10:24:01

标签: php mysql joomla

我在joomla网站上创建了一个组件。该组件显示一些照片(不大,只有8KB)。照片存储在mysql blob中。我可以将照片上传到joomla数据库,但我无法在网站上显示。无论我做什么,它只显示一些编码字符或空白。我试图创建一个单独的页面,但结果是一样的。这就是我所做的: mycomp是我的joomla组件。

admin.mycomp.php

<?php
function showDetail($option)
{
    $db = &JFactory::getDBO();
    $id = mysql_real_escape_string(JRequest::getVar('id'));
    $query = "select id,myphoto from jos_myphotos where id = ".$id;
    $db->setQuery($query);
    $rows = $db->loadObjectList();
    HTML_myphoto::showPhoto($rows,$option);
}     
?>

admin.mycomp.html.php

<?php
class HTML_myphoto
{
   ...
   function showPhoto($row,$option)
   {
      ...
      header("Content-type: image/jpeg");
      echo $row->myphoto;  //this will show some encoding character
      echo base64_decode($row->myphoto);  //this will show blank page
      //change echo with print get the same result.
      ...
   }
   ...
}

我试图像这样创建一个单独的页面: admin.mycomp.html.php

<?php
class HTML_myphoto
{
...
function showPhoto($row,$option)
{
    ...
?>
    <img src="show_image.php?myphoto=<?php echo $row->myphoto;?>" width=200 height=300>
<?php   
    ...
}
...
}

show_image.php

<?php
     $myphoto = (isset($_GET['myphoto'])) $_GET['myphoto'] : false;
     if($myphoto)
     {
         header("Content-type: image/jpeg");
         echo $myphoto;  //this will show some encoding character
         echo base64_decode($myphoto);  //this will show blank page
         //change echo with print get the same result.
     }
?>

结果相同。

2 个答案:

答案 0 :(得分:1)

我认为你有两个选择:

  1. 要么在PHP文件中创建一个带有源的图像标记,只接收一个ID参数,并在数据库中检索照片的字符串并回显它。
  2. 或者您直接在标签中回显照片的字符串:

    <img src="<?php echo base64_decode($myphoto); ?>" />
    
  3. 修改

    我刚刚查看了一个旧的应用程序,我将这些favicons存储在数据库中。在内联显示图像时,您不需要base64_decode(我的选项2) 所以仅供参考,这张图片有效:

    <img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAQAAAAEABcxq3DAAACkElEQVQ4y42TTWyUZRSFn/e+n6W00E4l9ScSCZGODXFLIIZENyZu1ITEGBdE3ejGVQksunDlwoUrE0ICCQuomZAYF4YNNvEPlFpjiJRgKq10HDKdmVJxEOh0vu+9h0UrMSyAk9zVOffczXPRI8hdciVNTZ/X6MjLOnrkK3XTLbk64lEK5FIhaf87HyqznYKy5qsLyuUPL/D1OXzkC8U4IhhViC9o7OAnSkkKksQDVG81GR//iAvTbS7OXIQQ1p0Oc3O/YlCAAAcSQIHIEc43Z8/z6r5xfrsiBp9+ArK4FgprNye//oEMDIJDMBDU6jc4d/YXzpz5kcuzC5SGBvBkoMT257ah5FTn/4Qgpqd+InM3LEDr+j8cO/45pyrnmLk0w0uv7KV/cIDCI4GIhUAgY3h4C9W5Kma9LF//l2xpqc33301xcuJLTp+ehLiJbSNlcpxoEanAolF4okB0lRAiuVOt1cje/+AAly8tcHWhDnEDeAfXKo/FTbg7ZLdwCrDI0PAAK7fvQATkYIbN/l4jWh8pAb4BrJ/abI32301MXULKoIhE9bKxt8RKJ4D1gXpIitj+d19j6PGN7CiXQavgHYir3G5fY+tT/Wx/cgulnh6su0JYvUPodiHvAGJz32YoJC222qo1lvTH1b906NBngq06cPBjFZKkXIuNmhZbN3RlvqUTlUnt2fumsGc0NvapkJLcpeRrzF+r31R5dLd2PP+i6s3if//gcndJUqPRUKVSUbPZFCnlkpJcSWmd+dffeE/BntWJk9/eW7q/RJJSSjIzQxKBhIUcgN17dhFCxsREheXlZSStZUIghEBRFACYGRlwz3Q5FuDtt/Yx/fMFSqVB8jwnrPP/Xy7LMtwdM+Mu+2gfA7SP0igAAAAASUVORK5CYII=" style="margin-right: 5px; vertical-align: middle;" class="bbns_itemDragger">
    

    它存储在我的数据库中(base64编码):

    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABIAAAASABGyWs+AAAACXZwQWcAAAAQAAAAEABcxq3DAAACkElEQVQ4y42TTWyUZRSFn/e+n6W00E4l9ScSCZGODXFLIIZENyZu1ITEGBdE3ejGVQksunDlwoUrE0ICCQuomZAYF4YNNvEPlFpjiJRgKq10HDKdmVJxEOh0vu+9h0UrMSyAk9zVOffczXPRI8hdciVNTZ/X6MjLOnrkK3XTLbk64lEK5FIhaf87HyqznYKy5qsLyuUPL/D1OXzkC8U4IhhViC9o7OAnSkkKksQDVG81GR//iAvTbS7OXIQQ1p0Oc3O/YlCAAAcSQIHIEc43Z8/z6r5xfrsiBp9+ArK4FgprNye//oEMDIJDMBDU6jc4d/YXzpz5kcuzC5SGBvBkoMT257ah5FTn/4Qgpqd+InM3LEDr+j8cO/45pyrnmLk0w0uv7KV/cIDCI4GIhUAgY3h4C9W5Kma9LF//l2xpqc33301xcuJLTp+ehLiJbSNlcpxoEanAolF4okB0lRAiuVOt1cje/+AAly8tcHWhDnEDeAfXKo/FTbg7ZLdwCrDI0PAAK7fvQATkYIbN/l4jWh8pAb4BrJ/abI32301MXULKoIhE9bKxt8RKJ4D1gXpIitj+d19j6PGN7CiXQavgHYir3G5fY+tT/Wx/cgulnh6su0JYvUPodiHvAGJz32YoJC222qo1lvTH1b906NBngq06cPBjFZKkXIuNmhZbN3RlvqUTlUnt2fumsGc0NvapkJLcpeRrzF+r31R5dLd2PP+i6s3if//gcndJUqPRUKVSUbPZFCnlkpJcSWmd+dffeE/BntWJk9/eW7q/RJJSSjIzQxKBhIUcgN17dhFCxsREheXlZSStZUIghEBRFACYGRlwz3Q5FuDtt/Yx/fMFSqVB8jwnrPP/Xy7LMtwdM+Mu+2gfA7SP0igAAAAASUVORK5CYII=
    

答案 1 :(得分:1)

对不起,你是否从show_image.php跳过一些行?! 原因$myphoto只是照片的ID。你不能base64_decode一个ID。