Manuel Pear

Génération dynamique d'archive pour une gallerie photo

Génération dynamique d'archive pour une gallerie photo -- How to use File_Archive for a photo/video gallery

One possible use case of File_Archive is to dynamically generate archives that contain pictures or videos from a gallery.

The choice of the file format is important if you want an efficient generation. Let's see what are the possibilities:

1. Tar
2. Tgz, Tbz
3. Zip

1. Tar

Pros: Generation very efficient, constant memory usage, no need to cache

Cons: No compression (but anyway images or video can hardly be compressed), not as widely used as Zip

2. Tgz, Tbz

Pros: Very high compression ratio, constant memory usage

Cons: Can't be cached, needs a lot of CPU at each generation

3. Zip

Pros: Intermediate result can be cached, compressed, you can choose the compression level, widely used

Cons: Compression ratio lower than for Tgz/Tbz

We will focus on Tar and Zip generation, Tgz and Tbz are too CPU expensive for an "on the fly" archive generation.

Tar generation

  1. <?php
  2. require_once "File/Archive.php";  
  3.  
  4. // $files is an array of path to the files that must be added to the archive
  5. File_Archive::extract( 
  6.    $files, 
  7.    File_Archive::toArchive( 
  8.       'myGallery.tar', 
  9.       File_Archive::toOutput() 
  10.    )  
  11. );  
  12.  
  13. ?> 

Zip generation

The main advantages of the Zip generation is that it is not very expensive (due to the ability to cache the result), and widely used. I think 2 viable options are to generate uncompressed Zip archives (since you don't reduce a lot the size of picture and video files by compressing them) or to generate compressed Zip archive using a cache system.

Exemple 43-1. On the fly creation of an uncompressed ZIP archive

  1. <?php
  2. require_once "File/Archive.php";  
  3.  
  4. File_Archive::setOption('zipCompressionLevel', 0);  
  5.  
  6. // $files is an array of path to the files that must be added to the archive
  7. File_Archive::extract( 
  8.    $files, 
  9.    File_Archive::toArchive( 
  10.       'myGallery.zip', 
  11.       File_Archive::toOutput() 
  12.    )  
  13. );  
  14.  
  15. ?> 

Exemple 43-2. On the fly creation of a compressed ZIP archive with a cache

  1. <?php
  2. require_once "File/Archive.php";  
  3. require_once "Cache/Lite.php";  
  4.  
  5. // See the documentation of cache lite for the meaning of the $options array
  6. // fileNameProtection must be left to the default true value
  7. // automaticSerialization is not required and should be left to false
  8. $options = array('cacheDir' => 'tmp');  
  9.  
  10. File_Archive::setOption('cache', new Cache_Lite($options));  
  11. File_Archive::setOption('zipCompressionLevel', 9);  
  12.  
  13. // $files is an array of path to the files that must be added to the archive
  14. File_Archive::extract( 
  15.    $files, 
  16.    File_Archive::toArchive( 
  17.       'myGallery.zip', 
  18.       File_Archive::toOutput() 
  19.    )  
  20. );  
  21.  
  22. ?> 

Putting it all together

Since generating a zip or a tar archive is pretty much the same code, you can write a simple code that lets the user choose what format he wants. The following code is taken from a code I really use in my gallery.

Exemple 43-3. Custom archive

  1. <?php
  2. $allowedFormats = array('tar', 'zip');  
  3.  
  4. if (!in_array($_GET['type'], $allowedFormats)) { 
  5.    die('Type ' . htmlspecialchars($_GET['type']) . ' is either unknown or not allowed');  
  6. }  
  7.  
  8. require_once "File/Archive.php";  
  9.  
  10. File_Archive::setOption('zipCompressionLevel', 0);  
  11.  
  12. /**
  13. * I skipped the generation of the $files array since it really
  14. * depends on you gallery and what files the user requires
  15. */  
  16.  
  17. File_Archive::extract( 
  18.    $files, 
  19.    File_Archive::toArchive( 
  20.       'myGallery.' . $_GET['type'], 
  21.       File_Archive::toOutput() 
  22.    )  
  23. );  
  24.  
  25. ?> 

Remonter Remonter
L'éditeur javascript - CSS - Gentoo - Tutoriaux PHP - Tutoriels PHP - Bretagne - php - Moto - Kit graphique