store the image names in a database, and after you isnert the image rename it into a hashed format (using md5 or sha1) to prevent the image names from special characters. If you insert a new item into the database, you get an ID number, you can rename the uploaded files into that (using the thumbnails). So you have more then one method to store the files. The previous is better, becouse then you can't owerwrite the existing filenames.
If you rename the images, you prevent your server from the unwanted uploaded scripts. If i upload a .php file, i can easily run it in your server (if you don't check the correct file-type. are you checking it?... it is called security hole)
Then using a comments table could solve your problem. The comments connected with this images table (foreign key is the image_ID )
After you uploaded the images, create a php file which can send the image to the browser using the readfile function, you can make the images visible with this php file.
<img src="show.php?image_id=1&size=thumb">
You can get ideas in this forum how to go in a right direction, but nobody like to solve others homeworks 🙂