0

I have blob data in my database and they are images that I want to display as a basic gallery.

I have a method that I have written to display the images, but I'm getting an error, saying that it is a string, rather than blob data being returned:

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;
 }

where getFilename() is my blob column.

action:

 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 ); 
 }

template:

           foreach($galleries as $gallery)
           {

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

           }

The error I get is that stream_get_contents seems to be returning a string.

Is there anyway, I can get the blob data, or rather than use a model method, use an action to return all the images attached to the application?

Thanks

4
  • Why do you store the images as blob data in the database if you have them locally as well under /uploads/gallery ? And where do you use $file, from your example nowhere ? Commented Jan 10, 2011 at 17:09
  • agree... what the heck? why are you streaming anything into $file? Commented Jan 10, 2011 at 17:29
  • It seems the code I was using <a href="forum.symfony-project.org/…> was using the stream_get_contents on $file. So I guess I left it in by accident. Commented Jan 10, 2011 at 17:40
  • I used the $file_src path of /uploads/gallery so in the html source I had a path to the image. I wasn't sure if i could do it any other way Commented Jan 10, 2011 at 17:41

4 Answers 4

4

If you store images in the database, you have (basically) two options to show them on the client:

First solution: Get file content and encode it with base64 encoding. You can find a working example here:

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

This method is not the best as if you do it like that, the client won't be able to cache these images, that means more traffic, more processing time, more database connection, slower page loading etc.

Second solution: You create an image loading action in Symfony. The routing is like:

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

You have to create a controller action myimageaction and there you can get the image ID like

$request->getParameter('image_id');

And get the blob data from the database and return it as binary with specific http headers. You can find working examples with simple Googleing, one example:

$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;

So in the template you can do like:

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

I have found this one at

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

Sign up to request clarification or add additional context in comments.

3 Comments

But this is for multiple images. I have a table named application (id), gallery(id, application_id) and image_gallery(id, gallery_id). So i need all the images in the gallery, associated to the application, thus my gallery. The code above will only display one image, surely?
Whenever you want to display a gallery, you get all the images from the database, and generate an HTML in your template, where for all the <img> tags source URL you can use the controller described above. I'm updating the answer with the template code.
But the code you provided above, will display an image by default. I want a html template, with a list of images.
0

This code doesn't make any sense.

Why do you feed binary(read string) data from ->getFilename() to stream_get_contents() which operates ONLY on resource data type? Of course it will complain.

Outputing blob to a browser is as simple as:

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

1 Comment

I was follwing code from: forum.symfony-project.org/… - this seemed to use stream_get_contents. I need to display the images in my template. Wont setting the contenttype, just display an image? I need images on the template as a list of images.
0

try switching your getFilenamePath function to this.

public function getFilenamePath() {
    $file_src = '/uploads/gallery/'.$this->getId().'.jpg';
    if (file_exists($file_src ) {
        return $file_src;
    } else {
        return false;
    }
}

1 Comment

But, my images are not stored on the file system, so $file_src will be null
0
$profile_picture = base64_encode(stream_get_contents($image->getContent()));
echo '<img src="data:image/jpeg;base64,'.$profile_picture.'" />';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.