Please move this to the correct forum.
I am in the process of creating a large database of images (over 50,000), and was looking for a way to tag each image quickly and efficiently. Since the photos in my gallery are from a digital camera, generally groups of images with sequential filenames get the same keyword. Therefore, I set out to write a script that would allow me to add keywords to a range of images at once.
Please note--the code below is VERY SPECIFIC to my application. Also, I am not a very good programmer, and I'm still learning. I am sure it is not the most efficient design, however, it works. You will not be able to cut and paste my code for your own purposes--it will take some modification before it can be applied generally. But, it is a good starting point--if you are an inexperienced programmer faced with a similar problem, take a look at what I've done. If you are an experienced programmer who would like to turn this into a plugin, please feel free.. you don't even have to credit me (you can if you want). Also note--this is not a plugin--it is a script that stands alone and modifies the Coppermine database. Anyway, I hope someone finds this useful:
<!-- I start with HTML only.. PHP comes later -->
<html>
<head>
<title>Tagging Tool</title>
</head>
<body>
<?php
// check all form entries and assign them to variables
if (isset($_POST['do'])) {
$do = $_POST['do'];
}
/*
I originally planned to use this, then removed it. It is still in the HTML as a hidden input, so I left it in the PHP as well.. you can remove if you want..
*/
else {
$do = '';
}
if (isset($_POST['start'])) { // where the range of images to modify begins
$start = $_POST['start'];
}
else {
$start = '';
}
if (isset($_POST['stop'])) { // where the range ends
$stop = $_POST['stop'];
}
else {
$stop = '';
}
if (isset($_POST['tags'])) { // the keywords I am going to add
$tags = $_POST['tags'];
}
else {
$tags = '';
}
/*
This is something specific to my application--it lets me define a city (an album in Coppermine) to run the database update on
*/
if (isset($_POST['city'])) {
$city = $_POST['city'];
}
else {
$city = '';
}
/*
This is just for safety--my image names all end in a 4 digit number, so here I ensure that I didn't accidentally have an extra digit
I was afraid this would modify the whole DB instead of the part I wanted.
*/
if ((strlen($start) > 4) || (strlen($stop)> 4)) {
die('too many selects');
}
else {
// I didn't end up using $do, but I check for it anyway
if (!$do) { // Here is all the HTML for the form input
?>
Enter the range of images you would like to tag:<br>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Start: <input type="text" name="start"><br>
Stop: <input type="text" name="stop"><br><br>
Enter the tag(s) you would like to add to these images:<br>
<textarea rows="7" cols="30" name="tags">Separate each tag with a comma.. no spaces!</textarea><br><br>
Select a city to apply these changes to: <select name="city"><option value="sf06">SF</option><option value="at06">AT</option></select><br><br>
<input type="submit" value="Submit" name="submit">
<input type="hidden" name="do" value="update">
</form>
<?php
}
// Make sure all necessary fields are entered, and then...
if ($do && $start && $stop && $tags) {
$tag = explode(",",$tags); // tags are entered in the form separated by a comma, so split them into an array
$db = mysql_connect("localhost","username","password"); // connect to the db
mysql_selectdb("gallery",$db); //select the right db
$res = mysql_query("SELECT * FROM cpg147_pictures",$db); //select all rows in the pictures table
while ($rows = mysql_fetch_array($res)) { // cycle through the rows and...
$num = substr($rows['filename'],-8,4); //store the last 4 chars of the image filename in $num
//this is because of the way my images are named.. you may need to change this
if (substr($rows['filename'],0,1) == "L") {
$city1 = "sf";
/*
Currently I only have 2 cities (albums) in my gallery.
"SF" image filenames all begin with "L" and "AT" filenames
begin with "I" so these two conditional check
the first char of the filename and set the $city1 variable
*/
}
elseif (substr($rows['filename'],0,1) == "I") {
$city1 = "at";
}
$pid = $rows['pid']; //sets $pid to the pid of the current row
if (($num >= $start) && ($num <= $stop) && $city == $city1) { //if the current row image is in the range...
$cur_tags = $rows['keywords']; // get any tags already assigned
foreach ($tag as $j) { //for every new tag to be added
$cur_tags = $cur_tags . " " . $j; //append it to any original tags
}
$res1 = mysql_query("UPDATE cpg147_pictures SET keywords = '$cur_tags' WHERE pid = '$pid' LIMIT 1",$db);
/*
Update the DB with the new keywords
There is a small bug here I can't figure out.. for some reason
the first tag added back into the db contains a space in
front of it. If anyone can figure out why I'd be very grateful.
(This does not seem to effect the operation of Coppermine)
*/
print('I am currently adding: ' . $cur_tags . ' to ' . $rows['filename'] . '<br>');
// Print some stuff so we know what is happening/happened.
}
}
}
}
?>
That's it.. let me know if you have any questions, comments, or criticisms.
Thanks!
Jon
edit (by Paver): Renamed subject from "New mod (sort of)" to "Add-on Script: Batch-add keywords to sequential files"