• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To store img in db (bytes) or not?

 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have 2 strategies to store a photo in a db (in my example of user's image)

1. save the image into a local folder (and make it unique - say by the userID --> 2052.jpg) to access it I just point to the portrai_folder\2052.jpg

or

2. I can convert the image to bytes (do String strDB = Base64.encode(bytes)) and store the string (strDB) in a database as field 'portrait'



Q: which one is better in terms of performance/efficiency (obviously #2 is easy to implement)

mind that images cannot exceed 15k (that is VARCHAR (20,000) )

Thanks for any pointers!
 
Dawn Charangat
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What db server are you using ? The big boys like oracle and sql server do have datatypes like LOB and BLOB which can hold large data [even movie clips]... so you wont be limited just to VARCHARs.
 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using mySql.

my fear is not the size because it can not exceed 15-20k
 
Dawn Charangat
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even in MySql, you can use a datatype called LONGBLOB to store images. However, you will have to take care of the size constraint yourselves in the java code. Just out of curiosity, how do you plan to handle the size issue ? ie, if the image is more than 15K, are you going to discard it ?
 
Peter Primrose
Ranch Hand
Posts: 755
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to resize the image first with

Graphics2D g = resizedImage.createGraphics();

* is it a good practice to store images in db or in a local folders?
 
Dawn Charangat
Ranch Hand
Posts: 249
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter, I dont think I'm authority enough to answer that question....

But, in my humble opinion, if your database server is in a clustered mode, ie, spread across multiple machines, then I suppose storing them in the database would be the better option.
If you are planning to store everything, viz., your db, and the folders in the same box, then storing the images in local folders would save your time for converting the LONGBLOB object to image, and then displaying it [a bit of performance improvement].
 
Paul Clapham
Sheriff
Pie
Posts: 20203
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dawn Charangat wrote:But, in my humble opinion, if your database server is in a clustered mode, ie, spread across multiple machines, then I suppose storing them in the database would be the better option.

Yes, that's true. Although it would be possible to store the images in a drive which is accessed remotely from all the servers in the cluster.

This may or may not be a better option: there are other factors involved beyond performance and efficiency (whatever that means in this context). For example, if you keep the images separate from the database then it's possible for somebody to change or delete them, thus damaging your database integrity. Or it's possible that the drive containing the images becomes inaccessible because of a hardware problem, thus making them inaccessible even while the database is still OK.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic