We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

best way to add thousands of photos to gallery

Development and Programming

Dave Rau's avatar
Dave Rau
85 posts
17 years ago
Dave Rau's avatar Dave Rau

I’m trying to use the photo gallery module to store a few thousand photos for a photo sharing web site with about 800 members (it won’t get much bigger, it’s an internal site for my job).

So I’ve got a few thousand photos, spread out over a handful of folders (which would be categories). What’s the best way to import and process (make thumbnails, medium images, etc) these photos for EE? Is the batch option the way to go here?

Also, is there some documentation somewhere about creating custom front-ends for data entry, if I wanted more than 6 custom fields in the gallery module for example, or if I wanted to tack on some extra processing, how would I get started with that? I’m just looking for a starting point here; not a hand out solution.

Thanks!

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell
So I’ve got a few thousand photos, spread out over a handful of folders (which would be categories). What’s the best way to import and process (make thumbnails, medium images, etc) these photos for EE? Is the batch option the way to go here?

I would be tempted to write some kind of import script to directly load the photos into the system and DB. There are only a few tables to deal with so it shouldn’t be that bad if all the photos will be in the same category.

       
Dave Rau's avatar
Dave Rau
85 posts
17 years ago
Dave Rau's avatar Dave Rau

Thanks Erin.

Can anyone point me to some info about creating custom front end scripts that interface with EE?

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

I wouldn’t do it and have it interface with EE, I would just write a shell script or Perl script that took the images, moved into the appropriate directory and made the appropriate entries in the DB.

Let me hunt around a bit and see what I can create.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

OK, assuming your host has mysqli installed in its PHP installation, put the following in a template, turn PHP on, replace the appropriate variables with your information then run the template.

Please be warned that there is little error checking and handling and this only works with one author/gallery/category.

But, it will get all the images in there.

PLEASE, PLEASE, PLEASE back up your DB before running this.

I take no responsibility for the results of this code!

<?php
$dir    = '</path/to/images/';
$gal = <gallery_ID>;
$cat = <category id>;
$author = <author id>;

// read the files in the directory
$files = scandir($dir);

$mysqli = new mysqli("<DB HOST>", "<DB user>", "<db pwd>, "<EE DB>");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $mysqli->prepare("INSERT INTO exp_gallery_entries (gallery_id, cat_id, author_id, filename, extension, title, width, height, entry_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");

$stmt->bind_param('iiisssiii', $gal, $cat, $author, $name, $ext, $title, $width, $height, $ts);

foreach ($files as $filename)
{
    list($name, $ext) = explode(".", $filename);
    
    if ($ext == "JPG")
    {
        echo "Uploading $name.$ext<br>";
            
            $title = "$name.$ext";
            
            list($width, $height) = getimagesize("/home/edalzell/thedalzells.org/tmp/$title");
            $ts = time();

            /* execute query */
            $stmt->execute();
            
            rename("$dir$title", "/path/to/photos/in/ee/including/cat/folder/$title");


    }
}
/* close statement */
$stmt->close();

/* close connection */
$mysqli->close();

?>
       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

This would make a neat addition to the gallery module I think…I will see what I can do.

The only settings needed would be to choose the gallery, category and author as all the other info is in the gallery module settings (photo upload path and category image path).

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

I haven’t tested this one, but this is much cleaner and only requires you to change 3 things, gallery_id, cat_id and author_id.

<?php
    global $DB;

    $gallery_id = <gallery_ID>;
    $cat_id = <category id>;
    $author_id = <author id>;

    require PATH_CORE.'core.image_lib'.EXT;
    $IM = new Image_lib();

    //get the gallery batch upload folder
    $results = $DB->query("SELECT * FROM exp_galleries WHERE gallery_id = $gallery_id");
    $gallery_batch_path = $results->row['gallery_batch_path'];
    $gallery_path = $results->row['gallery_upload_path'];

    // get the category path (if the setting exists)
    $results = $DB->query("SELECT * FROM exp_gallery_categories WHERE cat_id = $cat_id AND gallery_id = $gallery_id");
    $cat_folder = $results->row['cat_folder'];


    // read the files in the directory
    $files = scandir($gallery_batch_path);


    foreach ($files as $filename)
    {
        list($name, $ext) = explode(".", $filename);
        
        if (strtolower($ext) == "jpg")
        {
            echo "Uploading $filename<br>";
                
                $uploaded_image_path = $FNS->remove_double_slashes($gallery_batch_path.'/'.$filename);
                
                $IM->get_image_properties($uploaded_image_path);

                $data = array(    'gallery_id' => $gallery_id,
                                'cat_id' => $cat_id,
                                'author_id' => $author_id,
                                'filename' => $name,
                                'extension' => $ext,
                                'title' => $filename,
                                'width' => $width,
                                'height' => $height,
                                'entry_date' => time() );

                $sql = $DB->insert_string('exp_gallery_entries', $data);

                $DB->query($sql);
                
                // now move the file
                rename($uploaded_image_path, $FNS->remove_double_slashes($gallery_path.'/'.$cat_folder.'/'.$filename));
        }
    }
?>
       
Dave Rau's avatar
Dave Rau
85 posts
17 years ago
Dave Rau's avatar Dave Rau

Erin, thanks. I’ll be testing this out next week and I’ll come back and post some comments.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

I have a more cleaned up version as well…turns out the $FNS library has the file code that simplifies things as well.

I am going to try to make this a module so that there are some options, but we will see what I get to.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

OK, I have created a module that does this now, so you don’t have to change anything. It is a tad rough around the edges, but it does work.

The only downside is that no thumbnails are created, but I will look into that as well.

Is there any interest in this module? I only made it to learn how to make a module, so if no one wants it, no biggie.

       
Dave Rau's avatar
Dave Rau
85 posts
17 years ago
Dave Rau's avatar Dave Rau

I’d be interested for sure, but I need thumbnails and a few other sizes. There should be a dynamic aspect to number of other sizes, so i could do one for power point, one for print, one for web resolution, etc.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

K, I will see if I can get the thumbs working.

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

OK, thumbs will be to hard for me to do, sorry.

This is sufficient for my needs as I don’t use thumbs and use dynamic resizing instead.

I would be happy to release this for anyone to play with though. PM if you want it.

       
Michael C.'s avatar
Michael C.
29 posts
17 years ago
Michael C.'s avatar Michael C.
I would be happy to release this for anyone to play with though. PM if you want it.

Sent!

       
Erin Dalzell's avatar
Erin Dalzell
790 posts
17 years ago
Erin Dalzell's avatar Erin Dalzell

For the two of who PM’ed me, I promise, I will get to it this weekend. Two small kids + day job = tired in the evening

       
1 2

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.