Re: Serving graphics from a database

From: Margaret MacDonald (scratch65536_at_att.not.invalid)
Date: 08/15/04


Date: Sun, 15 Aug 2004 14:32:11 GMT

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

-- 
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)


Relevant Pages

  • Re: Serving graphics from a database
    ... >them from there rather than from the filesystem. ... General opinion on this group is to not store images in a database and instead ...
    (comp.lang.php)
  • Re: Serving graphics from a database
    ... > Andy Hassall wrote: ... >>General opinion on this group is to not store images in a database and instead ...
    (comp.lang.php)
  • storing images in filesystem
    ... I am convinced that I want to store images in filesystem as to ... database and store path into database Can someone point out an ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: reiser4 plugins
    ... > an entire new storage array and this wasn't/isn't cheap. ... > following versions of an article; reduced the database by 85%). ... So a filesystem with intelligent ... Any "unnecessary double work" would squarely be within ReiserFS... ...
    (Linux-Kernel)
  • Re: [RFC] Linux Kernel Subversion Howto
    ... - you only have a filesystem that supports 8.3 file names, ... This was the clever trick I alluded to. ... > why aren't you telling Oracle to disclose how Oracle works inside? ... stored in the database for itself, ...
    (Linux-Kernel)