Photo Gallery

I want to try to make a photo gallery with family pictures. What I am thinking I want to have happen is something like this. I have thousands of photos I want to put all the photos in the database once but be able to search for a specific pic. I want to have different "category’s’ for example person_one, person_two, person_three, birthday_party, family_reunion, ext. I have a lot of photos that could fall inn a lot of different category’s but I only want that photo to be in the database once. So I was thinking I could tag each photo like youtube does. When you upload a video on youtube for the tags you can put as many as you want separated by a comma. What is my best beat going to be?

Thanks.

You might check out gallery3 or coppermine rather than re-inventing a photo gallery php/mysql script.

Three tables. One for photos, tags and a relational table Something like:

photo

  • id (auto increment)
  • path (physical location of disk)
  • caption

tag

  • id (string)

photo_tag

  • photo_id
  • tag_id

I do not like Coppermine or Gallery 3 as they are relativly large installs and have a load more options than I will ever use.

I use a similar method to the one suggested by oddz on my current gallery.

Gallery

  • ID
  • Gallery name

Photo

  • ID
  • Path to photo
  • Tags( in my case EXIF data )

Index

  • Gallery ID
  • Photo ID

You could see what else is listed on Hotscripts as they have quite a few gallery scripts.

Thanks for the help all, i agree coppermine and Gallery 3 is a bit more then what i need.
I have all the photos organized just using folders on my pc. If i moved the main folder to my server would there be a script I can use to make it go through and populate the database then go back and tag each photo with the appropriate tags and gallery?

You could do something like this to save the photos to the database but would need to weed out the file . and … you could change the glob command to only select jpg if that is what all the images are.


//Connect to database here

// Directory to read images from
$dir = "photos/";

// Read the directory and sellect EVERYTHING
$filenames = glob( "$dir*" );

// Start the loop to read the images
foreach ( $filenames as $filename ) {

// Insert photo into the database
mysql_query("INSERT INTO photo VALUES '$filename");
	}

I would probably edit the photos in phpmyadmin or you would need to write some code to populate a table/form to add the tags.

Rubble would that code above make galleries as wall out of all the sub-folders in the main dir? The main folder has like 30 different folders/categories.

That is just some rough untested code to show a possible method.

If as you say you have thousands of photos it would probably be better to do them in batches anyway to save php timing out. In your case I would modify the “Directory to read images from” for each directory and process one directory at a time. Do the different directories contain random images or are they sorted into groups; if grouped you could save that detail into the database at the same time.

You need to decide on what you want from what you have before doing anything to make sure it is correct the first time around.

A couple of things to think about:
How do you want to group them
Are there some standard tags for each directory - or possibly date taken as that could be found out from the EXIF data.

Yes the folders are all categorized for the most part. If I can make a script that will go through one at a time and save the entire main category first I can make a form to add all the other tags later. I’m not sure how to go about this. I tried to make the code above work but have not had any luck. Do you mind showing me a more complete script or even a complete script from another site, I have not found any. Any additional help is appreciated.

Try this and remember this is just to get the data into the database:


<?php
/*
In this case the database has a table called photo with three columns: ID ( auto index ), path and catagory
You can use this as a trial as you need to decide on your final database design and work around that - you will probaby have more tables interlinked.
This will read the image from the folder and put it into the database with the catogary name
You need to specify the folder to read and the catagory to use
If all the images are jpg you could remove that from the name to save database space and the filename will be in the format: car_photos/07GAJJQE4Y.jpg and there are ways to remove the photos/ and put it back when the photo is displayed again saving database space.
You will need to decide on your folder layout as well to get the paths correct when displaying the images later.
*/

//Connect to database
$username = '';
$password = '';
$database = '';

$conn = @mysql_connect( "localhost", "$username", "$password" );

if (!$conn) die ("Could not connect MySQL");

mysql_select_db( $database, $conn ) or die ( "Could not open database" );

// Directory to read images from - change for each folder
$dir = "car_photos/";

// Catagory tag - change for each folder
$catagory = "cars";

// Read the directory and sellect EVERYTHING 
$filenames = glob( "$dir*" ); 

// Start the loop to read the images 
foreach ( $filenames as $filename ) { 

// Insert photo into the database
mysql_query("INSERT INTO photo ( 'path', 'catagory' ) VALUES ('$filename', '$catagory') "); 
    }  

?>

Just noticed this line should be:

mysql_query("INSERT INTO photo ( path, catagory ) VALUES ('$filename', '$catagory') ");

@dxm31: Can you provide an example of the directory structure as it currently exists.

Its set up like this now
Birthdays(Main)
---->Jim’s 12th(1st Sub-Dir)
--------->Dinner(2nd Sub-Dir)
--------->Six Flags(2ndSub-Dir)
---->Jim’s 13th
--------->And so on
Family Reunion
---->2001
--------> Breakfast
--------> Pikes Peak
---->2002

The main thing I want is to be able to tag who is in every photo, then that person becomes a main directory so we can view all photos with Jim or Susan.

@ Rubble I have been messing around with the code you gave

session_start();
include'include/dbconfig.php';
// Directory to read images from - change for each folder
$dir = 'photos/Birthday/Jim 12';

// Catagory tag - change for each folder
$catagory = "Birthday";

// Read the directory and sellect EVERYTHING 
$filenames = glob( "$dir*" ); 

// Start the loop to read the images 
foreach ( $filenames as $filename ) { 

// Insert photo into the database
$insert = "INSERT INTO photo ( path, catagory ) 
				VALUES ('".$filename."', '".$catagory."') ";   
    } 
	$add_member = mysql_query($insert);

When I run the page and check the database it adds 3 rows but there all look like this: Path is set to photos/Birthday/Jim 12 and Category is Birthday

It does not add the path to the photo just the dir the photos are in shouldn’t in be like this: photos/birthday/Jim 12/1.jpg

You have put the line to add to the database outside the loop and I do not know why you have added the ". to each varible.

Also I would change the white space in the directory name to a _ as your URL will display Jim 12 as Jim%2012


session_start();
include'include/dbconfig.php';
// Directory to read images from - change for each folder
$dir = 'photos/Birthday/Jim 12';

// Catagory tag - change for each folder
$catagory = "Birthday";

// Read the directory and sellect EVERYTHING 
$filenames = glob( "$dir*" ); 

// Start the loop to read the images 
foreach ( $filenames as $filename ) { 

// You could uncomment this line to check the filenames and comment out the line $add_member = mysql_query($insert);
// to see what is happening without saving to the database.
// echo "<br>$filename";

// Insert photo into the database
$insert = "INSERT INTO photo ( path, catagory ) 
                VALUES ('$filename', '$catagory') ";

$add_member = mysql_query($insert);  
  
    } 
    

I have coped and pasted the code you gave me and still can not get it working. When I echo $filename it displays the same as $dir when I try inserting to the database it add’s exactly what $filename and $dir are set too but it dose it three times.

try this one.

echo “<div id=‘form’ class=‘form’>
<form name=‘myform’ method=‘post’ action=‘cms-edit-photo-gallery-add-category.php’>
<table width=‘700’ border=‘0’ cellpadding=‘2’ cellspacing=‘2’ align=‘center’>
<tr>
<td width=‘60’>New Category</td><br>
<td><input name=‘category’ type=‘text’ size=‘29’></input></td>
</tr>
<tr>
<td> </td><td><input name=‘save’ type=‘submit’ value=‘Save Category to DB’>
<input name=‘reset’ type=‘reset’ value=‘Reset’></td></tr>
</table>
</form>
</div>”;

Thanks for the reply Meaghandrina, I can get it to add the category fine. I’m having trouble getting

$filenames = glob("$dir*");

to go through

$dir = 'photos/Jim_12';

and move the path of every photo to the database.

Somewhere along the line a / has been lost and I wonder if that is the problem?


$dir = 'photos/Birthday/Jim_12/';

Thanks Rubble that was what was missing. Just one more thing. To display the results of a practice query I’m am trying to use a <table> this is what I have now

$result = mysql_query("SELECT * FROM photo WHERE catagory = '$cat' ORDER BY path");
			while($row = mysql_fetch_array($result))
			{ echo "
			<table>
				<tr>
					<td>
					<p><a href='viewphoto.php?photo=".$row['path']."'><img src='".$row['path']."' width='150' height='100'></a></p>
					</td>
					<td>
					<p><a href='viewphoto.php?photo=".$row['path']."'><img src='".$row['path']."' width='150' height='100'></a></p>
					</td>
					<td>
					<p><a href='viewphoto.php?photo=".$row['path']."'><img src='".$row['path']."' width='150' height='100'></a></p>
					</td>
					<td>
					<p><a href='viewphoto.php?photo=".$row['path']."'><img src='".$row['path']."' width='150' height='100'></a></p>
					</td>
				</tr>

I only have two pictures in the db as of now just to try all my code. When I load this page it puts the pics in to the table except the top row is all the same pic and the second row is four of the second pic. What is the proper way to display the results in a table?