Image uploading - best practices

The system I’m developing at the moment will enable users to upload images. Upon upload, several thumbnail versions of the image are created. I also plan to create a database entry for every image upload.

This is how I plan to do it all:

Member images folder: myapp/images/members/

Within this folder I will create seperate folders for each member: myapp/images/members/$member_id

Images are uploaded to the member folder, the original filename is appended with "_main" , "_thumblarge" or "_thumbsmall" depending on the version of the image. I retain the original file extension by using the "extensionName" property of CUploadedFile.

Now when creating the entry in the DB how should the image be referenced? For example suppose the original image is called "abcxyz.jpg" - this will have 3 versions: "abcxyz_main.jpg", "abcxyz_thumblarge.jpg" and "abcxyz_thumbsmall.jpg".

I don’t think we need 3 seperate columns in the DB, but we do need to know what files to look for. Anybody got any thoughts on this?

Can you just include three constants on your Image Model and then use a function than by using a Image::LARGE or Image:THUMB you return the appropiate name?

I am always retaining the original name (the name you use to save the file of course) in the DB and have a field that has its extension.

In your case you just save the first name like "abcxyz"

the endings are always fixed, so you can add them as needed…

Even if there are different type of images you can remember only the original name like "abcxyz.jpg"… and then as needed split and add the "_thumblarge" or "_thumbsmall"

Yeah I think the slight difficulty would be with the extension - if I just store the filename without the extension, it won’t find the file(s). Similarly if I store the full filename, i.e. abcxyz.jpg then I have to do a split and insert the suffix. Perhaps I can just include an “extension” field in the DB?

Do you really need to store images names? All images have ids, so you can name them “253_s.jpg” for small, “253_b.jpg” for big and etc. Maybe you’ll only need to save original image name, because it’s extension can be different. Here I assume you resize images to jpg format.

Yeh I need to know the extension because the user will be able to upload jpg, gif, or png

That is exactly what I suggest:

[i]

I am always retaining the original name (the name you use to save the file of course) in the DB and have a field that has its extension.[/i]

I did something like that once

if you really need to stay with the same name :

-an approach would be save the filename and add the "_main" or other suffix before you output

-another one is to have 2 fields on the table, 1 for the name, 1 for the path of the file and alter the filename adding "_main" as suffix and then you have the original name, which is better can the user will be able to change the image name without alter the file

one tip:

save the thumbnails sufixed with its size instead of "_big" and "_small", like "image1_128_128.png"

at somet point you might want to create a new type of thumbnail and it wont look good to call it "_smaller" and then "_smallest"

also its better for remembering like "img_64.jpg" you will know that its size is 64px x 64px without think twice

Do you think it would be best to resize all images to jpg? For example if a user uploaded a GIF should it be converted to jpg and would it make any difference to the file? That way I don’t actually have to worry about extensions…

OR should it always be resized in the format of the original file?

It does matter

a gif may contem a serie of images, png allow transparency and so on

It depends on the nature of images. If you deal with photos, then all of them will probably be jpg. But if users can upload animated gifs, then it’s another situation.

could always do a replace.

myimage{type}.jpg

myimage{type}.png

then str_replace(’{type}’, ‘_main’, $imageFile);