Bulding an archive of images dynamicly
Recently I had a need to gather a group of images, add a watermark, and zip them up so a user could download them. Instead of create all of these zip files each time anything is changed, I instead wrote a class to do this all for me.
This class requires Cana_Zip and Cana_Thumb
class myZip extends Cana_Zip { public function _construct($params = array()) { parent::_construct($params); } public function create($params = array()) { // here i queried our database to get a list of files. // you could also loop through a directory $files = array();
// create the thumb object $thumb = new Cana_Thumb(array( 'path' => 'images/', 'cache' => 'imagecache/', 'watermarkSrc' => 'images/watermark.png', 'watermark' => true, 'format' => 'jpg' ));
$zipedFiles = array(); foreach ($files as $file) { // write the thumb $out = $thumb->writeThumb($file->image); // add the cached image path to the zip $zipedFiles[$file->image] = $out['file']; }
// write out the zip file $out = $this->create($zipedFiles, array( 'name' => 'myzip.zip', 'destination' => 'zip/' )); } }
Once our class is set up, we now need to use it.
// create the zip $zip = new myZip(array( 'destination' => './zip' )); // give it to the user header('Location: /zip/myzip.zip');