Jump to content

Generate a .zip file from images


vknt
 Share

Recommended Posts

Hey everyone,

Processwire is such f** great tool to structure content that I use it in every possible situation. So now I'm using it to generate a datamodel for an iPad application (probably a 1000+ line datamodel)

It's a kind of keynote like application. So I setup a tree with all al the slides (images) I need and various other pages to define variables for those slides.

To generate the text datamodel, I just use php. No problem, but I'm stuck with the assets:

What I want to be able to do is generate various sizes of the slides and then package those as a zip and force download that to the browser.

Any guides on how to accomplish this?

Thanks in advance!

Valentijn

Link to comment
Share on other sites

$zip = $page->images->path . 'dosfile.zip';
$fileos = array();
foreach($page->images as $image) $fileos[] = $image->filename;
$fileos = implode(' ', $fileos); 
exec("zip $dosfile $fileos"); 
echo "<p><a href='$dosfile'>clicka.mato dawnlode</a></p>";
  • Like 1
Link to comment
Share on other sites

Hi thanks for the responses. I tried the exec approach, but that didn't really work for me so I ended up using the ZipArchive class. http://www.php.net/m....ziparchive.php

This is the basic approach of my code.

<?php
// ************************************************
// Zip files
// ************************************************
$zip_filename = "assets.zip";
$zip_path = $config->paths->root."site/assets/files/".$page->id."/".$zip_filename;
$zip_url = $config->urls->root."site/assets/files/".$page->id."/".$zip_filename;
$zip = new ZipArchive;
$res = $zip->open($zip_path, ZipArchive::CREATE);
$log_errors = "";
if ($res === TRUE) {
foreach ($pages->get("/slides/")->children() as $slide) {
 if ($slide->screen_image_nl->filename) {
		// add image - size iPad
  $slide_nl = $slide->screen_image_nl->size(1024,768)->filename;
  $zip->addFile($slide_nl, basename($slide_nl));
		// add image - thumbnail
  $slide_thumb_nl = $slide->screen_image_nl->size(128,96)->filename;
  $zip->addFile($slide_thumb_nl, basename($slide_thumb_nl));
 } else {
  $log_errors .= "slide (id:".$slide->id.") - ".$slide->title." <strong>does not exist!</strong><br/>";
 }
}
$zip->close();
$zip_html = "<a href='$zip_url'>Download $zip_filename</a>";
} else {
$zip_html = 'Oops something went wrong. I cannot give you the .zip file.';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Download assets</title>
</style>
</head>

<body>
<h4>Download</h4>
<p><?php echo $zip_html; ?></p>
<h5>ERRORS:</h5>
<p><?php echo $log_errors == "" ? "No errors" : $log_errors; ?></p>
</body>
</html>

  • Like 1
Link to comment
Share on other sites

I just remembered I've used the ZipArchive class recently too. I've got an inteanet system where zip files are uploaded and I have to check for a readme.csv file and it's perfect for that as there's very little overhead (as in it doesn't unzip the whole file but can search for a single file and pull it out very efficiently).

That's not what we're doing here obviously, but it is cool!

Link to comment
Share on other sites

  • 3 years later...

I just coded this functionality with updated methods of PW. I use it to give all the images of the news page as a package for press release purposes.

This is triggered by a button on the page like below, so the link above does not change, page is stable but download starts.

<button onclick="window.location.href='<?= $page->httpUrl; ?>?download=images'">
if ($input->get->download == 'images') {

        if ($page->images->count() > 0) {

            //collect images
            $imagesArray = [];
            foreach ($page->images as $img) {
                $imagesArray[] = $img->filename;
            }

            //zip images
            //TODO: in a function
            $zipPath = $config->paths->files . $page->id . DIRECTORY_SEPARATOR . $page->name . '.zip';
            wireZipFile($zipPath, $imagesArray, [
                'overwrite' => true
            ]);

            $options = array(
                // boolean: halt program execution after file send
                'exit'             => true,
                // boolean|null: whether file should force download (null=let content-type header decide)
                'forceDownload'    => true,
                // string: filename you want the download to show on the user's computer, or blank to use existing.
                'downloadFilename' => $page->name . '.zip'
            );

            wireSendFile($zipPath, $options);
            exit;
        }

    }
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...