Dynamically Generating Zip/TGZ Files with One Line of Code
When you work on media-heavy web projects, you will have to manage large sets of images and, at some point, have to share these images with others. Sharing collections is more efficient when the images are archived because it enables easier transfer between servers and ensures that images can be delivered in a relatively smaller sized package.
You also may need to transform these sets of images before they are archived and shared. This strategy is common because it helps optimize files that are being sent from one machine to another, in scenarios such as:
- Email - Providers, such as Gmail, now offer features that enable users to download all attachments for a given email as archived files.
- Online schools and tutors - Companies such as Udacity and Pluralsight enable course downloads - which contain exercises, images and videos – that can created with a single click, as archived files are sent directly to your computer.
- Automated backups - These backups are done by archiving all the media files on the server.
- Uploading images to the server via the browser
- Transforming images on your server for optimization purposes
- Archiving images using a fairly complex logic
- Downloading an archived file
Generating Images ZIP/TGZ Files with Cloudinary
With Cloudinary, we can address the complex process of archiving and sharing images quickly and with just a line of code. Cloudinary supports generating ZIP/TGZ files using thegenerate_archive
Upload API method. This approach can include any type of file, and enables options for determining which files to include in the archive file. For example, you can choose based on the file's name, all files in the same folder, or some other criteria. Cloudinary also enables you to apply transformations to all the images before including them in the file and set various options for generating the archive file, such as naming the file.
Here's one way that Cloudinary enables you to archive and share images:
https://api.cloudinary.com/v1_1/demo/image/generate_archive?api_key=373364719177799&expires_at=1557919777&mode=download&public_ids%5B%5D=fat_cat&public_ids%5B%5D=kitten&public_ids%5B%5D=hungry_cat&signature=a2f86b73d32d2a778493d6759d59059d0a30076d×tamp=1464179836&transformations=c_scale%2Cw_200
If you click on the link above, you can download an archive of cat images. The signature parameter is required and is generated with your credentials using the Cloudinary SDK. Let's now look at performing these task in our various dev environments, using the SDK.
We have two options for archiving image files:
- Pre-creating an image zip/TGZ using your cloud images based on specific parameters
- Using a dynamic URL to download existing images from your cloud as an archive, which is the approach taken with the cat image example above.
Pre-creating and Uploading Image ZIP/TGZ Files
To pre-create a ZIP/TGZ file, use thecreate_zip
SDK method. With this, you can automatically upload the archive file to the cloud, and then give your users a link for downloading it. This option is best if multiple users will be downloading the resulting archive file.
The pre-create option creates an archive file based on parameters you supply. The response for the archive requests gives us a URL to download the files.
The following line shows how to create an archive that contains images of our cloud-tagged cat
:
// JavaScript
cloudinary.v2.uploader.create_zip(
{ tags: 'cats', resource_type: 'image',
target_public_id: 'cute_cats.zip', transformations: {
width: 50, height: 50, crop: 'fill'}},
function(error,result) {console.log(result) });
// PHP
\Cloudinary\Uploader::create_zip(array(
'tags' => 'cats', 'resource_type' => 'image',
'target_public_id' => 'cute_cats.zip', 'transformations' => array(
'width' => 50, 'height' => 50, 'crop' => 'fill')));
# Ruby
Cloudinary::Uploader.create_zip(:tags => 'cats',
:resource_type => 'image', :target_public_id => 'cute_cats.zip',
:transformations => {:width => 50, :height => 50, :crop => :fill})
We then can gain access to the archived file using the image method:
// JavaScript
cloudinary.image("cute_cats.zip")
// PHP
cl_image_tag("cute_cats.zip")
# Ruby
cl_image_tag("cute_cats.zip")
Dynamic URLs for Downloading Archived Images
Another option is generating a signed URL for creating a ZIP/TGZ file on-the-fly and on-demand. The ZIPTGZ file is created and sent to your user only when the URL is accessed. The resulting ZIP/TGZ file is not cached or stored in your Cloudinary account, so this option is ideal for sharing images with a single user. The SDK method to complete this isdownload_zip_url
, which also takes parameters of the images we need to download and returns a URL to download the archived file:
// JavaScript
cloudinary.v2.utils.download_zip_url(
{ public_ids: ['cat3_tqjcms', 'cat4_atunfc', 'cat5_rp7bpa'], resource_type: 'image', transformations: {
width: 50, height: 50, crop: 'fill'}},
function(error,result) {console.log(result) });
// PHP
\Cloudinary\Utils::download_zip_url(
array(
'public_ids' => array('cat3_tqjcms', 'cat4_atunfc', 'cat5_rp7bpa', 'transformations' => array(
'width' => 50, 'height' => 50, 'crop' => 'fill')),
'resource_type' => 'image'));
# Ruby
Cloudinary::Utils.download_zip_url(
:public_ids => ['cat3_tqjcms', 'cat4_atunfc', 'cat5_rp7bpa'],
:resource_type => 'image',
:transformations => {:width => 50, :height => 50, :crop => :fill})
The code above generates the URL like the shown earlier, which you can use to download the file. It's important to note that the URL contains an expiration date, which defaults to one hour, but can be extended with expires_at
when setting the download option.
The Archive Alternative
We have been using the term zip and archive interchangeably in this guide. This is a very common phenomenon but they are quite different. The examples are for ZIP/TGZ files but Cloudinary also supports archives.create_archive
and download_archive_url
are the archive alternative API methods for pre-creating archives and downloading archives respectively.
The methods receive the same arguments as the ZIP/TGZ alternatives. The difference is just in the name and the output.
Using a single line of code to generate ZIP/TGZ files offers numerous benefits when it comes to organizing, streamlining, normalizing and optimizing the delivery of multiple images. Cloudinary gives you options to best fit your needs, either creating the ZIP file and uploading it to the cloud, or generating a dynamic URL that creates and delivers the ZIP/TGZ file on demand.
This short guide gives you a quick and easy alternative for moving collections of images by using Cloudinary's archiving feature, which also enables you to transform and optimize images before creating an archive.
Want to try it yourself? Go to the Cloudinary website and test drive the solution for free.
Disclaimer: This article is sponsored by Cloudinary.