1

I have a number of images that are sotred as blob data in my database. I am aware this isn't a good idea, but it's what I'm using.

I have following code in my Peer class:

public function getImagesPath()
{
  $file_srcs = false;
  $fp = $this->getPhoto->getBlobData();

  if ($fp !== null)
  {
      $file = stream_get_contents($fp);
      $file_srcs = '/uploads/gallery/'.$this->getId().'.jpg';

  }
return $file_srcs;

}

I then call this in my template, like so:

            $path = $item->getImagesPath();
            if ($path)
            {
                echo '<img src="'.$path.'" alt="Thumbnail for '.$photo->getName().'" width="153" height="153" />';
            }

Now this works well, but, I have some images that are either square in shape, or rectangular. Giving them a size/width in the img src distorts some of them.

Is there anyway, in which I could resize/crop the images before they are displayed?

Thanks

terrid25
  • 1,926
  • 8
  • 46
  • 87

3 Answers3

3

sfThumbnailPlugin is what I've used on a number of projects and it is pretty awesome. There is an older version for Symfony 1.0 if that's what you're using. By default it uses GD, but you can have it use ImageMagick and do some pretty cool things with it.

sjobe
  • 2,817
  • 3
  • 24
  • 32
  • But does this support creating thumbs on the fly? I don't want to save the images as thumbnails on the server – terrid25 Jan 07 '11 at 13:38
  • Hmm.. im 90% sure that it requires an output file for the images, how about writing them to a tmp directory and then getting rid of them afterward ? – sjobe Jan 07 '11 at 13:48
  • I'm trying to not display them anywhere on the file system if possible. I saw the jsThumbPlugin, but this only works with 1.0, I'm using 1.4 (Propel) – terrid25 Jan 07 '11 at 13:53
  • You could always edit the plugin and insert your blob generation code into where they have the save-to-disk code. – sjobe Jan 07 '11 at 14:09
  • Seems a little bit hacky to me. I'd rather not download a plugin and then modify it's classes. Kind of defeats the reason to use the plugin – terrid25 Jan 07 '11 at 15:22
  • Is there any other way I could do this? – terrid25 Jan 10 '11 at 10:10
  • I don't have much experience working with blobs or images that don't involve the file system, so I can't think of anything right now. – sjobe Jan 10 '11 at 16:17
1

How are you adding images to the database?

If it is via an upload form, the best method would be to create a thumbnail of the appropriate size/dimensions using GD or another library and store it in a second blob column.

Otherwise you can specify a single dimension in the html and the picture will retain its dimensions.

Jestep
  • 984
  • 2
  • 8
  • 20
  • Well I have an image that is 250x500 and when I set the width to 150, the image looks squashed and distorted. Thus I'd like to create a thumbnail, or render the image with the correct scale ratio – terrid25 Jan 07 '11 at 16:43
  • @terrid When you say the image looks squashed and distorted, what exactly do you mean? If you've got a 250x500 image that gets displayed in an img element with the width set to 150, and no height, then the image should display at 150x300, i.e. the correct aspect ratio. Are you sure the element isn't inheriting a height from somewhere? – Matt Gibson Jan 20 '11 at 10:46
1

You can probably use imagecreatefromstring and imagecopyresampled. This is code that I use, that I've changed to work with your blob. This also adds a white background if the original size width/height ratio doesn't match the destination image size.

static function CreateThumbnailFromBlob($blobData, $dstWidth = 100.0, $dstHeight = 100.0){
    $oldImg = @imagecreatefromstring($olduri);
    if($oldImg){
        $realOldW = imagesx($oldImg);
        $realOldH = imagesy($oldImg);
        $destX = 0;
        $destY = 0;

        if($realOldH>=$realOldW && $realOldH>0){
            $realY = $dstHeight;
            $realX = round($realY*$realOldW/$realOldH);
            $destX = round($dstWidth/2-$realX/2);
        }else{
            $realX = $dstWidth;
            if($realOldW>0)
                $realY = round($realX*$realOldH/$realOldW);
            else
                $realY = $dstHeight;
            $destY = round($dstHeight/2-$realY/2);
        }
        $newImg  = @imagecreatetruecolor($dstWidth, $dstHeight);
        $white   = imagecolorallocate($newImg, 255,   255,   255);
        imagefill($newImg, 1, 1, $white);
        imagecopyresampled($newImg,$oldImg,$destX,$destY,
                            0,0,$realX,$realY,$realOldW,$realOldH);
        imagedestroy($oldImg);
        return $newImg;
    }
}
Aspelund
  • 416
  • 4
  • 10