Jump to content

Adding Photo Labels without Database


SaranacLake

Recommended Posts

Like many projects, this started small, and keeps growing!  :facepalm:

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!

 

 

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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 by Barand
  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?!)  :happy-04:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.