Wrapped in gaffa

Programming, Art, Music, Philosophy, Politics

Archive for December 2008

Downloading galleries with Zenphoto

with 16 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