Re: Serving graphics from a database

From: Steve (ThisOne_at_Aint.valid)
Date: 08/18/04


Date: Wed, 18 Aug 2004 22:47:13 +1200

Margaret MacDonald wrote:
> Andy Hassall wrote:
>
>
>>On Sat, 14 Aug 2004 13:28:17 GMT, Margaret MacDonald
>><scratch65536@att.not.invalid> wrote:
>>
>>
>>>I want to store frequently-used graphic elements in a db and serve
>>>them from there rather than from the filesystem.
>>>
>>>Is there a better/faster way to do that than this? (error checking
>>>etc not shown)
>>>
>>>The relevant piece of the page:
>>><?php
>>> echo
>>> '<table><tr><td>
>>> <img src="ImageFromDB.php?Image=ImageIdentifer">
>>> </td></tr></table>' ;
>>>?>
>>>
>>>ImageFromDB.php:
>>><?php
>>> $imgname = $_GET['Image'] ;
>>> $dset = mysql_query( 'SELECT imagebits FROM graphics_table
>>> WHERE name="' . $imgname . '"' ) ;
>>> $rec = mysql_fetch_row($dset) ;
>>> $browser = fopen( 'php://output', 'w') ;
>>> header( 'Content-Type: image/png' ) ;
>>> fwrite( $browser, $rec[0] ) ;
>>>?>
>>>
>>>I fingered through the docs and did some googling, but didn't find
>>>anything obviously helpful. Many thanks for any insights.
>>
>>Looks OK to me. A plain 'print' would likely do just as well as opening the
>>output stream as you've done, but there's nothing wrong with that. You might
>>want to change the mode to 'wb' though to indicate binary mode.
>>
>>If these images are frequently accessed then it might be worth considering
>>using a filesystem-based cache if the database queries take noticable amounts
>>of resources.
>>
>>General opinion on this group is to not store images in a database and instead
>>just store a filesystem path. However, I believe that if the images are
>>
>>(a) user data rather than static source code of the site
>>(b) have relationships with data in the database
>>(c) have any sort of value with regards to the end-user
>>
>>... then it's worth considering storing them in full in the database so you
>>can include them consistently in the database backups, and have the same
>>transaction guarantees as all the other data on them.
>
>
> Thanks, Andy. I didn't even experiment with print() since I presumed
> it would do gratuitous interpretation. And thanks for catching the
> 'b'--I seem to have a habit of missing that out.
>
> Your articulation of when/why it can be good to store images in the db
> fits for me.
>
> Margaret

The only real downside on storing images s a database is if you're using
a system-based backup procidure. Then, every time oyu change an image,
then the whole data file needs to be saved again, and images tend not to
be small.

So why not apply the same logic above to the file name, and then store
the images somewhere else. That way they can be backed up individually
when they change, not en masse.

$0.02

Steve



Relevant Pages

  • Re: [PHP] Re: how to display images stored in DB*
    ... Your claim is that in ALL cases using a file system to store images ... bitmaps in a database without a damn good reason, a bad practice, yes. ...
    (php.general)
  • Re: [PHP] Re: how to display images stored in DB**
    ... for storing images is "bad" practice. ... "Bad" practice or "good" practice depends upon the needs and one's perspective of the situation. ... If you were to talk to the average computer user; or to the average investor who invests in technologies; or to the businessman who is paying the bills for programmers to build the product while trying to make money at the same time; they all might have different opinions as to the quality and state of software. ... If you assume that you have one database supply all the images to all ...
    (php.general)
  • Re: bound object frame: picture
    ... Please let me respond to your critique and hopefully help Bob along the way. ... generates can be very large when they contain images. ... Print event NOT THE FORMAT EVENT. ... your entire database will stop working when you get about 100 pictures ...
    (microsoft.public.access.forms)
  • Re: ListView - Caching Thumbnails?
    ... The program just will yield some odd results if they do (which I figure they can deal with if they are monkeying with the database;). ... Access databases fill the middle ground I guess but still, you can edit XML by hand. ... The underlying goal here is to create an interactive anatomical atlas program using high resolution images. ... Then I go back to school ). ...
    (microsoft.public.dotnet.languages.vb)
  • Re: bound object frame: picture
    ... Keeping the Images in a folder instead of the MDB is certainly the ... If you follow the above steps and your Report still fails then here are ... The error message is "Database has reached maximum size". ... > I have tried splitting the database, linking tables from additional databases, and finally not linking the photos but only storing the text description of the path to the photo file in the database. ...
    (microsoft.public.access.forms)