SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Thread: Photo Gallery

  1. #1
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2009
    Posts
    356
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You might check out gallery3 or coppermine rather than re-inventing a photo gallery php/mysql script.
    Doug G
    =====
    "If you ain't the lead dog, the view is always the same - Anon

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,048
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    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
    The only code I hate more than my own is everyone else's.

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    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.

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    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.
    PHP Code:
    //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.

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    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.

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    Try this and remember this is just to get the data into the database:
    PHP Code:
    <?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') "); 
        }  

    ?>

  11. #11
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    Just noticed this line should be:
    PHP Code:
    mysql_query("INSERT INTO photo ( path, catagory ) VALUES ('$filename', '$catagory') "); 

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,048
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    @dxm31: Can you provide an example of the directory structure as it currently exists.
    The only code I hate more than my own is everyone else's.

  13. #13
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    @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.

  14. #14
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @ Rubble I have been messing around with the code you gave
    PHP Code:
    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

  15. #15
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    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

    PHP Code:
    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);  
      
        } 

  16. #16
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Rubble View Post
    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

    PHP Code:
    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 "$dir<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.

  17. #17
    Non-Member
    Join Date
    Jan 2012
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>";

  18. #18
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply Meaghandrina, I can get it to add the category fine. I'm having trouble getting
    PHP Code:
    $filenames glob("$dir*"); 
    to go through
    PHP Code:
    $dir 'photos/Jim_12'
    and move the path of every photo to the database.

  19. #19
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    Somewhere along the line a / has been lost and I wonder if that is the problem?
    PHP Code:
    $dir 'photos/Birthday/Jim_12/'

  20. #20
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Code:
    $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?

  21. #21
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,195
    Mentioned
    71 Post(s)
    Tagged
    3 Thread(s)
    My mind has gone blank with tables as I use css.
    You will need to add a count and after every so many images echo a </tr><tr> but it gets more complicated as you need a starting <tr> on its own and a finishing </tr> on its own.

    I use a container div and then put the images in the div; this means the images automaticaly wrap to fit the div.

    PHP Code:
    #thumbnail_div {float: left;
        width: 378px ;
        margin-left: 0 ;
        text-align: left;
        padding: 10px 0 10px 0;
    }
    .thumbnail
    {
        float: left;
        height: 110px;
        margin: 0 0 2px 0;
        text-align: center;
    }


        <div id="thumbnail_div">        

    <?php

    $result 
    mysql_query("SELECT * FROM photo WHERE catagory = '$cat' ORDER BY path");
                while(
    $row mysql_fetch_array($result))
    {
    // Just fix the height or else if the widths and heights vary the photo will be distorted
    echo "<div class=\"thumbnail\">\n<a href='viewphoto.php?photo=".$row['path']."'><img src='".$row['path']."'></a></div>";
    }
    ?>
    <div style="clear:both;"></div>
    </div>
    On my Latest gallery I resize the images up upload and put them on "standard" size canvases which makes for a nice neat layout: My gallery main page


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •