Hi,
I have a friend/client that I am switching over to nopCommerce. His current shop uses a directory/folder structure for storing product images. Needless to say uploading all those images to the database will be a big job, not to mention the question of whether to store images in the db at all. To be honest I have no idea which way is better so I decided to try to implement both solutions.
It feels a bit hacky/work-aroundish but I am interested to see what anyone has to say about the fundamental pros and cons of my implementation. My goals were simple:
1. Change as little of the existing code as possible to make upgrades easier.
2. Utilize the resizing methods in picture manager.
3. Support binary images AND image folders.
Here is what I did:
1. In the db I added a new column to Nop_Picture called URL and allowed Nulls.
2. Changed PictureBinary column to allow Nulls.
3. Added 2 sprocs called Cstm_InsertPicture and Cstm_UpdatePicture.
4. In SqlPictureProvider.cs added public overrides for InsertPictureURL and UpdatePictureURL that use the additional URL column. Also added a reference to picture.URL in GetPictureFromReader().
5. Added public abstracts to DBPictureProvider.cs matching Insert and UpdateURL.
6. Added get;set; for URL property in Picture.cs
7. PictureManager.cs
-Add mapping to DBMapping
-Add Insert/UpdateURL public static methods.
-Add if (picture.binary != null) just above the 'using (MemoryStream.. in GetPictureURL(pciture,targetsize,getdefaultURL)
***-Add else statement below and copied same code sans removing the memory stream reference. Instead I created an Image object and used Image.FromFile(picture.URL)
8. Just to test the uploading of images I added a checkbox(Picture is in file?) and 2 textboxes(File Location),(File Extension) to /Admin/ProductPictures.ascx.
9. In the code behind I added an Event to toggle the visibility of the FileUpload field and the textbox fields based on whether the checkbox was checked.
10. Finally I added another condition (cbIsInFile.Checked == false) to the 2nd if statement in btnUploadProductPicture_Click. Then added an else that used my InsertPictureURL I defined in the previous steps.
That's it. It's not terribly elegant but it works. My biggest concern is whether resizing images from a file takes more overhead or can cause problems etc. Frankly I have no idea. Also, there is I am sure a much better way to upload the images than statically entering in the file location (\images\logo.jpg) I just haven't messed with that yet.
Needless to say uploading the image url's was much easier than converting the images to binary and I can use both the db and directories to store images.
Comments good or bad appreciated.
joe