Wrapped in gaffa

Programming, Art, Music, Philosophy, Politics

Downloading galleries with Zenphoto

with 5 comments

Last weekend I had a Christmas Party or what you call it in english – it is basically a danish tradition where you drink a lot of schnapps, with marinated herring and anything made from pigs on rye bread. A great one also has warm food, but mine didn’t.. I think it all evolves around the schnapps and beer, the rest is just fittings.

Anyway I took a lot of pictures with my quite expensive digital camera before I got too drunk.. and decided to hide it from myself. Digital pictures is no fun unless you can share peoples embarrassing moments online, so I installed Zenphoto on my Dreamhost account. Worked like a charm, but soon people started requesting an archived download of the entire gallery. I did a little search and found out that Zenphoto already had that function printAlbumZip and I just had to add it to the Template which was as easy (even for a PHP virgin like me) as adding <?php if (function_exists(‘printAlbumZip’)) { printAlbumZip(); } ?> somewhere appropiate in the template and turned on persistent archive in Zenphoto, so that the gallery would not be archived more than once. Storage space was not a big issue, but apparently RAM was – I kept getting errors because memory ran out. I took a look at createAlbumZip and found that it was sending the file by using readfile which puts the entire ~500MB into RAM before sending it. I did a small patch to cache and send chunks of 16MB at a time in createAlbumZip, it’s not a pretty patch, but I guess simply using readfile isn’t either.

I replaced this code in zp-core/functions.php:
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . urlencode($album) . '.zip"');
echo file_get_contents($dest);

With this:
$buf_size = 1024*16;
$file = fopen($dest, "r");
 
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . urlencode($album) . '.zip"');
header('Content-Length: ' . filesize($dest));
flush();
 
while(!feof($file))
{
print fread($file, $buf_size);
flush();
}
fclose($file);

Link to gallery

Written by gaffa

2008-12-07 at 17:12

Posted in Programming, Software

Tagged with

5 Responses

Subscribe to comments with RSS.

  1. Excellent code thanks for that fixed up downloading albums on my dreamhost zenphoto installation

    Rob

    2009-12-31 at 05:16

  2. Can you explain exactly how you get the link to the zip file to show up on the album page? I cannot get this working in my environment.

    Jester

    2010-02-05 at 18:54

  3. In zenphoto/themes/your_theme/album.php (replace your_theme with the folder of your theme) you insert this line of PHP where you want the link to be displayed:

    <?php if (function_exists(‘printAlbumZip’)) { printAlbumZip(); } ?>

    If this doesn’t display a link, printAlbumZip might not be supported in your Zenphoto installation.

    gaffa

    2010-02-06 at 18:31

  4. Thanks, I got this working. I was copying what you had published above and it pasted the incorrect character ‘ in the php file. Once I corrected all the ‘ it worked.

    It also looks like they have resolved the memory issue in v1.2.8 with the following code:

    /**
    * Processes a file for createAlbumZip
    *
    * @param string $dest the source [sic] to process
    */
    function printLargeFileContents($dest) {
    $total = filesize($dest);
    $blocksize = (2 << 20); //2M chunks
    $sent = 0;
    $handle = fopen($dest, "r");
    while ($sent < $total) {
    echo fread($handle, $blocksize);
    $sent += $blocksize;
    }
    }

    Jester

    2010-02-09 at 19:19

    • Thank you for updating me on ZenPhoto, Jester.

      gaffa

      2010-04-24 at 11:12


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.