SaranacLake Posted November 30, 2019 Share Posted November 30, 2019 Like many projects, this started small, and keeps growing! I am building a photo gallery and would now like to add labels/captions below each photo, HOWEVER, I really don't want to have to build a database and do all the related coding. (I know how, but its a PITA.) To display photos in the gallery, I read in files from my "images/" directory, store them in a simple array, and then iterate through the array to display the thumbnails in a gallery. I am wondering if there would be an easy way that I could merge photo metadata (e.g. a brief caption) with my array or something like that? If I could type up the file names and a brief description/caption in a Text File or Spreadsheet and then somehow slurp that into my code and merge it with my array or something like that, then I would be willing to type up captions. (This is for like 600 photos which is why I don't want to do a database as data entry in phpMyAdmin is a PITA!) Any suggestions how I could do this and add a "cherry on the top" of this mini project I am doing for my co-workers? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/ Share on other sites More sharing options...
Barand Posted November 30, 2019 Share Posted November 30, 2019 39 minutes ago, SaranacLake said: as data entry in phpMyAdmin is a PITA! Anything with phpMyAdmin is a PITA. There are alternatives MySQL Workbench Write a php script for your data entry. (You can create the initial db table by scanning the directory then you just need to add the descriptions. You can then easily give the users a facility to search the image descriptions for keywords) Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/#findComment-1572011 Share on other sites More sharing options...
ginerjm Posted November 30, 2019 Share Posted November 30, 2019 I build small folders of trip pics and add caption files (.txt) to the same folder. Then as I loop thru the image files to display them I also check for files with the same basenames but a .txt extension and display that along with the pic. Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/#findComment-1572016 Share on other sites More sharing options...
maxxd Posted November 30, 2019 Share Posted November 30, 2019 If it's set up in the image you could use exif_read_data() to grab comment metadata and output that as the caption. Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/#findComment-1572017 Share on other sites More sharing options...
Barand Posted November 30, 2019 Share Posted November 30, 2019 (edited) You could've had a database set up in less time than you spent moaning about what a PITA it is. It takes about half a dozen statements to create an image table from your image directory. JFDI. const IMGDIR = 'images/'; $db->exec("CREATE TABLE IF NOT EXISTS `image_lib` ( `img_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, `path` varchar(255) DEFAULT NULL, `width` int(11) DEFAULT NULL, `height` int(11) DEFAULT NULL, `mime_type` varchar(20) DEFAULT NULL, `description` varchar(255) DEFAULT NULL, `img_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`img_id`), FULLTEXT KEY `idx_image_lib_description` (`description`) ) "); $ins = $db->prepare("INSERT INTO image_lib (name, path, width, height, mime_type) VALUES (?,?,?,?,?)") ; $imgs = glob(IMGDIR.'{*.png,*.jpg}', GLOB_BRACE); foreach ($imgs as $i) { $s = getimagesize($i); $ins->execute( [ basename($i), IMGDIR, $s[0], $s[1], $s['mime'] ]); } Job done! Edited November 30, 2019 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/#findComment-1572018 Share on other sites More sharing options...
SaranacLake Posted November 30, 2019 Author Share Posted November 30, 2019 3 hours ago, ginerjm said: I build small folders of trip pics and add caption files (.txt) to the same folder. Then as I loop thru the image files to display them I also check for files with the same basenames but a .txt extension and display that along with the pic. That's one option. Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/#findComment-1572022 Share on other sites More sharing options...
SaranacLake Posted November 30, 2019 Author Share Posted November 30, 2019 2 hours ago, maxxd said: If it's set up in the image you could use exif_read_data() to grab comment metadata and output that as the caption. No, unfortunately what I need isn't in the metadata, but that is a good idea for future projects! Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/#findComment-1572023 Share on other sites More sharing options...
SaranacLake Posted November 30, 2019 Author Share Posted November 30, 2019 1 hour ago, Barand said: You could've had a database set up in less time than you spent moaning about what a PITA it is. It takes about half a dozen statements to create an image table from your image directory. JFDI. const IMGDIR = 'images/'; $db->exec("CREATE TABLE IF NOT EXISTS `image_lib` ( `img_id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) DEFAULT NULL, `path` varchar(255) DEFAULT NULL, `width` int(11) DEFAULT NULL, `height` int(11) DEFAULT NULL, `mime_type` varchar(20) DEFAULT NULL, `description` varchar(255) DEFAULT NULL, `img_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`img_id`), FULLTEXT KEY `idx_image_lib_description` (`description`) ) "); $ins = $db->prepare("INSERT INTO image_lib (name, path, width, height, mime_type) VALUES (?,?,?,?,?)") ; $imgs = glob(IMGDIR.'{*.png,*.jpg}', GLOB_BRACE); foreach ($imgs as $i) { $s = getimagesize($i); $ins->execute( [ basename($i), IMGDIR, $s[0], $s[1], $s['mime'] ]); } Job done! Thanks for the code - might be handy if I go with a database solution. Um, @Barand, nothing is "easy" when you have been away from coding and database for 4-5 years (and are an old man) like me!! While you sample is useful - thanks - id does NOT cover all of the other things I need to do like creating a database in my DEV environment, writing code to read the DB and populate my PHP gallery, creating a PROD database on my server, migrating the DEV data to PROD, and don't forget data entry. If it was "easy", I wouldn't be griping here. Guess it will come down to how much time I have left - if any - at the end of this 4-day weekend. (Can't believe I spent my whole holiday building something for work for free?! Such is the addiction of a programmer with a problem to solve?!) Quote Link to comment https://forums.phpfreaks.com/topic/309600-adding-photo-labels-without-database/#findComment-1572024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.