2011-01-10 73 views
0

我有我的數據庫中的斑點數據,他們是我想要顯示爲基本畫廊的圖像。顯示斑點圖像 - symfony

我有我寫了顯示圖像的方法,但我得到一個錯誤,說這是一個字符串,而不是返回BLOB數據:

public function getFilenamePath() 
{ 
    $file_src = false; 
    if (null !== $fp = $this->getFilename()) 
    { 
     $file = stream_get_contents($fp); 
     $file_src = '/uploads/gallery/'.$this->getId().'.jpg'; 
    } 
    return $file_src; 
} 

其中getFilename()是我的blob列。

行動:

public function executeSingle(sfWebRequest $request) 
{ 
    $application_id = $this->getRequestParameter('id'); 
    $c = new Criteria(); 
    $c->addJoin(GalleryPeer::APPLICATION_ID, ApplicationPeer::ID); 
    $c->addJoin(GalleryImagePeer::GALLERY_ID, GalleryPeer::ID); 
    $c->add(GalleryPeer::APPLICATION_ID, $application_id); 

    $this->galleries = GalleryImagePeer::doSelect ($c); 
} 

模板:

  foreach($galleries as $gallery) 
      { 

      $path = $gallery->getFilenamePath(); 
      if($path) 
      { 
       echo '<img src="'.$path.'" />'; 
      } 

      } 

我得到的錯誤是stream_get_contents似乎返回一個字符串。

無論如何,我可以得到blob數據,或者不使用模型方法,使用動作來返回附加到應用程序的所有圖像?

感謝

+0

爲什麼你的圖像作爲二進制大對象數據存儲在數據庫中,如果你有他們在本地以及下`/上傳/ gallery`?你在哪裏使用`$ file`,從你的例子無處? – mhitza 2011-01-10 17:09:38

+0

同意...什麼?你爲什麼要將任何東西流入$文件? – dqhendricks 2011-01-10 17:29:28

+0

看來我使用的代碼here正在使用$ file上的stream_get_contents。所以我想我不小心把它丟下了。 – terrid25 2011-01-10 17:40:21

回答

4

如果存儲圖像的數據庫中,你有(基本上)兩個選項,以顯示它們的客戶端上:

解決方案一:獲取文件的內容,並用base64編碼編碼。您可以在這裏找到工作的例子:

http://www.php.net/manual/en/function.base64-encode.php#99842

這種方法是不是最好的,如果你那樣做,客戶端將無法緩存這些圖像,這意味着更多的流量,更多處理時間,更多數據庫連接,更慢頁面加載等。

第二種解決方案:您在Symfony中創建圖像加載操作。路由是這樣的:

mapimage: 
    url: /myimage/:image_id.png 
    param: { module: myimagemodul, action: myimageaction } 

你必須創建一個控制器動作myimageaction在那裏你可以得到圖像ID喜歡

$請求 - >的getParameter( 'image_id');

並從數據庫中獲取blob數據並將其作爲具有特定http標頭的二進制返回。你可以找到工作的例子簡單Googleing,一個例子:

$this->image = ImagePeer::retrieveByPk ($request->getParameter('image_id')); 

$response = $this->getResponse(); 
$response->clearHttpHeaders(); 
$response->setContentType ($this->image->getMimeType()); 
$response->setHttpHeader ('Content-Disposition', 'inline;filename='.$filename); 
$content = $this->image->getData(); 
$response->setContent (stream_get_contents ($content)); 

$this->setLayout (false); 
return sfView::NONE; 

所以在模板中,你可以這樣做:

<img src='<?= url_for ('route_to_action_above', array ('image_id' => $image->getId()) ?>'/> 

我發現這一個在

http://forum.symfony-project.org/viewtopic.php?f=22&t=31207#p109705

0

此代碼沒有任何意義。

爲什麼你要從->getFilename()stream_get_contents()只能在resource數據類型上運行二進制(讀取字符串)數據?當然會抱怨。

Outputing BLOB的瀏覽器一樣簡單:

$this->getResponse->setContentType('image/jpeg'); 
echo $data->getFilename(); // assuming filename column is your blob column 
0

嘗試切換您的getFilenamePath功能這一點。

public function getFilenamePath() { 
    $file_src = '/uploads/gallery/'.$this->getId().'.jpg'; 
    if (file_exists($file_src) { 
     return $file_src; 
    } else { 
     return false; 
    } 
} 
0
$profile_picture = base64_encode(stream_get_contents($image->getContent())); 
echo '<img src="data:image/jpeg;base64,'.$profile_picture.'" />';