PHP - 我如何读取图片值并使其可以通过浏览器查看?

PHP - how do i read the picture value and make it viewable by browser?

我有 JPEG/PNG 个小文件,存储在 mysql 数据库字段图片中,如下所示:

  $fdata = '';
  if (isset($_FILES['picture']) && $_FILES['picture']['size'] > 0) {
    $tmpName  = $_FILES['picture']['tmp_name'];  
    $fp      = fopen($tmpName, 'r');
    $fdata = fread($fp, filesize($tmpName));
    $fdata = addslashes($fdata);
    fclose($fp);
    //$sl = "INSERT INTO image (image)VALUES ( '$data')", $connection);
    print "Thank you, your file has been uploaded.";        
  } else {
    print "No image selected/uploaded";        
  }

  // Update the table 
  $this->db->update('users', 
                    array('password' => $_POST['password'], 
                          'picture' => $fdata), 
                    array('username=?' => $ouser )); 

但现在的问题是我如何将数据库中的 "picture field" 值输出到网络浏览器的真实图片中?

编辑 1:

只是echo 不会在浏览器中渲染图片

编辑 2:

  public function pictureshowAction() {  
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    $this->_response->setHeader('Access-Control-Allow-Origin', '*');
    $this->db = Application_Model_Db::db_load();        
    $ouser = $_GET['ousername'];

    $sql = "select *From users where username='{$ouser}' limit 1";
    $result = $this->db->fetchAll($sql);
    if (count($result) > 0 ) {
      $picture  = $result[0]['picture'];     
      //$content = $picture; 
      $content = stripslashes($picture);
    } else {
      $content = '';
    }

    //echo $content;    

    $this->getResponse()
            ->setHeader('Content-Type', 'image/jpg')
            ->setBody($content)
            ->sendResponse();    
    exit;
  }   

很简单,假设您的内容类型是 png 您首先需要向浏览器发送正确的 header

header('Content-type: image/png');

然后简单输出你的图片内容

echo $pictureContentFromDatabase;

您的屏幕截图显示的内容是由于您没有发送正确的内容类型 header 告诉浏览器您的服务器发送的是图像,一旦您修复它就可以了。而且您不必 addslashes 输出,那会使图像无用。所以回显没有 addslashes

的内容