I want to save an uploaded image to database. Now my question is:
What should be the data type for SQL Server 2000 for image?
a. image
b. varbinary
what is the code for saving image in database?
How to retrieve the image from database and show?
Please refer me any tutorial or guidline. Or if you can please share with me.
Thanks in advance.
The data type should be text because the best way to save an image to a database is to save its path. Let your OS do the job of storing the actual files.
Typically on SQL Server, you would use a BLOB, Binary Large OBject, to store images. We used it for Word documents on a previous project, and it worked just fine. See this article on Database Journal for more info, although a quick Google for the BLOB type will throw up lots more examples.
I wrote this article a while back on this subject. It should help you with #2 and #3. It uses MySQL, but the basics are the same, you just need to replace the MySQL calls with the MSSQL calls.
As to #1, I would go with the obvious choice: "image".
I am not 100% sure of the differences between the two, however. It just seems obvious :)
Edit, according to this, it appears that the image datatype is deprecated. It will be removed in future versions. Not sure how much this affects you, seeing as you are using a 10 year old version, but it is worth keeping in mind.
In SQLServer IMAGE or VARBINARY are allmost the same. IMAGE is 2GB but VARBINARY() needs a length argument.
It's not good idea to store images in a database, the size increases a lot, with each Backup you need to save all images, with increasing size also increases the time to perform a Backup and Restore. You also need to change the network packet size (at the server properties, advanced, network, network packet size) In recents SQLServer versions the most adecuate datatype is varbinary(MAX)
Related
What I know, in Database Context, BLOB or Binary Large OBject is nothing but actually a stored binary code for a given data. Can Reserves spaces in GBs and can be used to store virtually any data type. But What's actually a use of it?
My major is Computer Vision and I'm fairly novice at databases and web development. Currently, I'm working on a sentiment analysis project and want to collect a large dataset for this purpose i.e. huge number of images and also want to keep record of whether a image has been used for the analysis purpose or not. I thought storing images in database with separate column for access record is the best thing I can do to have an organized and systematic approach. But Everyone I talked with recommends not to store image as a blob in database but just have its URL or name there and should have images in a dedicated folder.
Moreover, since BLOB is just binary encoding of a file how would we decode it into an image file? I found codes like following to convert a BLOB value into an image:
echo '<img src="data:image/png;base64,' . base64_encode($image->getimageblob()) . '" />';
echo '<img src="data:image/jpg;base64,' . base64_encode($image->getimageblob()) . '" />';
But these codes are specific to the extension (And personally I haven't been successful with any such codes). As all extensions for sure have some different schemes and thus a code cannot be used for image of all those extensions. My dataset targets visuals of an image and not on extension thus contains images of various extensions so how can one deal with them using a BLOB?
So the approach of storing just names in database and and images in a dedicated folder sounds good but then what is the use of database itself? Can not we have some renaming mechanism for images via PHP and store them directly into that folder. Why use database when we can rename images like img_1_accesses_5.png and split image name to get the ID and number of times it accessed?
If BLOB can store virtually every type of data, why the use of BLOB is such horrible and everyone recommends not to use it? And what is the problem if we directly inject images into database as BLOB? And finally If its suitable for images then how to deal with it?
So my question is How to effectively use BLOB and for which purposes it is suitable?
So my question is How to effectively use BLOB and for which purposes it is suitable?
Quick and dirty answer
The simple answer is: BLOBs smaller than
256KB are more efficiently handled by a database,
while a filesystem is more efficient for those greater
than 1MB. Of course, this will vary between
different databases and filesystems
There is a microsoft technical report here : Compare blob and ntfs filesystem . The report is quite old (2006) but i think there isn't any much change from there.
Imaging when you want to read file which stored in blob. you have send request to your database software, then the software controller will read blob data which is stored in filesystem. Instead of directly read from file-system, you have to go through 2 steps processes. So when the size of your file become bigger, blob will slow down your database a lot. And we all know that speed is the main key for database.
Hope that help
recently i have tried to store images in Mysql database (use BLOB data type), using php and web to upload and store it. It works fine, except when loading large enough image , it is going to load very slowly. Is there any way to load this image faster ?
note : my friend suggests me to use force caching to this image ( he says something about change the content-header of image ), but i don't know how to do it. and i doubt it will bring significantly better performance.
Thanks in advance
The images should definitely be cached. I think this can be done mostly just by making sure the image url you make is always the same for the same picture. What I think your problem is though is you need to change max_allowed_packet. If this is too small it won't be able to send much data over the network at one time. Also if the pictures are truly that big I'd also consider changing the quality of the picture to maybe 70%? All the resize image functions have a way to change it. ie: http://php.net/manual/en/function.imagejpeg.php. Hope that helps. I'd also look into YSlow. It'll help point out what exactly is wrong with your images that is making it load slowly. Whether it is quality, cache, compression or w/e it may be.
Caching the images could be used when images stored on filesystem. If they are dynamically popped and printed from the database they will be fetched each time the PHP code ask for them.
It could be that images are fetched in a dozens of ms, but a 3MB image data could be downloaded to clients browser for 5 seconds to 1 minute (depending on the connection speed). There is not much to do with it (even less on common shared hostings).
I would suggest storing the images on a filesystem so they could be cached by the browser, or You could even set a memcache on Apache server so until the expire they would be served from the cache.
I guess that caching can be good or not. Instead of I suggest you to upload images or other files to a folder and save on DB just the information about file: name, type, size, folder, etc...
If you don't have any requisite that requires you to store an image in the database, images are better inside a folder and what you should store in the database is the path or the name of each of them.
This would make them load normally. Just depending on the size of the image, of course.
That's what you will find in almost all web applications.
Hey,
I need some advice so I'll explain what I'm trying to accomplish, how I think I want to do it and I hope someone can tell me if this a good idea or if I'm over-thinking it.
What I want to do it take a bitmap image from an as3/Flash IDE project and save it on a database server. This is a permanent installation that has 4 flash apps running and they all link to a MySQL database.
I found this tutorial that shows me how to serialize bitmapdata and then store it on the local computer but what I'd instead like to do is store that compressed bitmap data in the database in a blob field. Is this a good idea?
If not can I send it to php to store the bitmapdata into a folder on the server and store a reference to it in the database? I can convert it to jpeg to email from the server at the moment but I need a copy that is still in bitmap format to be picked up by a 5th app and printed out.
If I do store it as a file should I then use PHP to grab the file and stream the bytes back to Flash? Is there a better way of doing it?
Any tutorials or code snippets would be greatly appreciated. If not just a push towards the right subject to read up on.
Thanks guys.
Ben
There has been great debate over the years about whether or not to store images in the DB itself. I think most of that is over and the consensus is rapidly heading to 'store it on the filesystem with a link'... you can scale that up easily, but scaling up a DB is much much harder. All of that's fine by me, because I always thought the DB guys were nuts.
For transfer over the wire, I'd suggest PNGing the image, depending on what kind of image it is (PNG is both compressed and lossless).
How to store the file will depend on how your app is structured. You can probably get by with large folders full of MD5 generated filenames to prevent collision. You probably don't want to store anything serially, as that makes it easy for somebody to come in and just take the entire set. You can also store autogenerated filenames in user id'd folders... that's up to you.
Unless high security is needed, you can just keep them all on a Web server and shoot back a URL, which makes the files readily available. If you need to keep them private, then you can stream them back with PHP. Make sure you use Etags or some other caching mechanism.
Added:
Btw, the PHP/MySql part is actually pretty easy. All in all, a seasoned pro could do it in a couple of days -- week tops.
And here's the AS3 PNG encoder, which is part of AS3CoreLib
i answered a similar question about saving bitmap into jpg file using as3 and php
Save image from Flash, send it to PHP and return a URL string to Flash
The best way to store images into MySQL is by storing the image location as a character string.
If you need to manipulate the image, then, the best way is to copy the image as a binary.
How one can store images into binary form and how we can retrieve them back? I don’t know anything about this technique. Please tell me how we can do this.
Don't store images in the database. Store them in the filesystem, then store their relative paths in the database.
I've written some blogs on this (and have some data from SQL Server)
http://www.atalasoft.com/cs/blogs/loufranco/archive/2007/12/03/images-in-databases-part-i-what-to-store.aspx
http://www.atalasoft.com/cs/blogs/loufranco/archive/2007/12/04/images-in-databases-part-ii-web-images-are-random-access.aspx
http://www.atalasoft.com/cs/blogs/loufranco/archive/2009/10/26/more-on-images-in-databases.aspx
Basically,
Small images are ok to put in a blob
Large images are much better to put on the filesystem
Images in a blob are much easier to manage (transactions, backup, simpler code, access control)
Images on the filesystem will perform much better
Think about pulling some meta-data out of the image and storing in separate columns for filtering and sorting purposes.
Almost every professional enterprise system that needs to deal with a lot of large blobs has some way of putting them on the filesystem. The latest SQL Server even has a field type that will do it automatically (and then it's as easy to program and manage as a blob)
You can use the BLOB data type. Although I agree with #Ignacio Vazquez-Abrams, there are times where storing the image in the DB is best. I have done so in past with great results. As long as the files are not large then this is a good solution.
I happen to have a database with pictures stored as blob fields. Can't help it, it was the previous developer's choice.
Now I need that data in a new site and the provider won't let me copy the data the easy way (file has become 11Mb big - won't upload that and I don't have shell access).
So I thought I'd write a script that opens a connection in db1, selects all the records, and then copies each into a table in the new db2.
Works all fine if I exclude the blobs. If I want to copy them too, it won't insert.
Anyone had something similar before?
Should I treat the blobs differently when it comes to inserting?
Thanks for any ideas or help.
11MB isn't a huge file, I'm surprised your host has such a low max upload size.
Have you thought about exporting as SQL, splitting the file in two (in Notepad++ or something) then uploading it in smaller sections? Wouldn't take long.
Perhaps check to see if you can increase the max_allowed_packet setting on your mysql DB. I'm not sure if it affects inserts, but I remember having to adjust this setting when I worked on a web-app that allowed users to download 3-5MB binaries from blob fields in the DB.
This link may be helpful, from a quick google search: http://www.astahost.com/info.php/max_allowed_packet-mysql_t2725.html