Extract images from a zip archive

For my second example of using my image thumbnailer I’ve thrown together a little script which accepts a zip archive upload containing images and generates and returns to the browser a new zip archive containing a thumbnail for each image in the original.

Form to upload a zip file containing images

Form to upload a zip file containing images

After processing a new zip is sent to the browser containing thumbnails

After processing a new zip is sent to the browser containing thumbnails


Source code download at the end of this page.

zip2thumb.php

 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
	UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
	UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
	UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
	UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
	UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
	UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.'
);

// get url to link to thumbs
$pathinfo = pathinfo($_SERVER['SCRIPT_NAME']);
$download_url = 'http://'.$_SERVER['HTTP_HOST'] . '/' . $pathinfo['dirname'] . '/thumbs.zip';

// has the form been submitted?
if( isset($_POST['submit']) ){

	// was a file submitted?
	if( isset($_FILES['zip']) ){
		// any errors?
		if( !$_FILES['zip']['error'] ){
			// security check
			if( is_uploaded_file($_FILES['zip']['tmp_name']) ){
				// is it a zip file?
				if( preg_match('#\.zip$#i', $_FILES['zip']['name']) ){

					// get the thumbnail options from the form
					$thumb_width = (int)$_POST['thumb_width'];
					$thumb_height = (int)$_POST['thumb_height'];
					$thumb_method = $_POST['thumb_method'];

					// create a zip archive object
					$zip = new ZipArchive();
					if( $zip->open($_FILES['zip']['tmp_name']) ){
						$output_name = 'thumbs.zip';
						// create output archive
						$output = new ZipArchive();
						if( $output->open($output_name, ZipArchive::OVERWRITE ) ){

							// loop through all the files in the archive
							for( $i = 0; $i < $zip->numFiles; $i++){
								$entry = $zip->statIndex($i);
								// is it an image?
								if( $entry['size'] > 0 && preg_match('#\.(jpg|gif|png)$#i', $entry['name'] ) ){
									$file = $zip->getFromIndex($i);
									if( $file ){
										$image = imagecreatefromstring($file);
										if( $image ){
											$thumb = paGdThumbnail($image, $thumb_width, $thumb_height, $thumb_method, $thumb_bgColour);
											imagedestroy($image);
											if( $thumb ){
												ob_start();
												imagejpeg($thumb,null,$thumb_quality);
												imagedestroy($thumb);
												$thumb_data = ob_get_clean();
												$output->addFromString(preg_replace('#\.(jpg|gif|png)$#i', '_t.jpg', $entry['name']), $thumb_data);
											}
										}
									}
								}
							}
							$output->close();
						}
						$zip->close();
						header("Location: $download_url");
						exit();
					}
				}
				else{

				}
			}
		}
		// display an error message
		else{
			$error = $errors[$_FILES['zip']['error']];
		}

	}

}

?>






Upload a zip archive containing images in jpeg, png or gif format.

You will receive a zip archive containing a thumbnail for each image in the original archive. Select the thumbnail width, height and method using the form below.

Width: Height:

[drain file 4 show]

Scripts.com