Transparently Unzip Images to Display
Moderator: General Moderators
Transparently Unzip Images to Display
Scenario:
I own/manage a PC gaming resource web site, http://www.lonegamers.com/, which stores files locally on a server (specifically things like movies, screenshots, etc.). Now, while some people prefer to download the screenshots for games as zipped files, others prefer not to. Zipping them saves me space and keeps the site tightly organized.
My Goal:
I hope to find some PHP (Perl is too complicated for this - Perl is what I know best, though) that can unzip the screenshot file from the server, and display the images as thumbnails and then, when selected, display the fullscreen image. I really just need to know how to unzip these files, and how to display them, all the usability code I can do myself.
Thanks for any help you may give me and anyone who helps will recieve credit on the site.
I own/manage a PC gaming resource web site, http://www.lonegamers.com/, which stores files locally on a server (specifically things like movies, screenshots, etc.). Now, while some people prefer to download the screenshots for games as zipped files, others prefer not to. Zipping them saves me space and keeps the site tightly organized.
My Goal:
I hope to find some PHP (Perl is too complicated for this - Perl is what I know best, though) that can unzip the screenshot file from the server, and display the images as thumbnails and then, when selected, display the fullscreen image. I really just need to know how to unzip these files, and how to display them, all the usability code I can do myself.
Thanks for any help you may give me and anyone who helps will recieve credit on the site.
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
I believe that you have pretty much what you need here:
http://www.php.net/manual/en/ref.zip.php
On the other hand you realize that images are already compressed so the space saved isn't that great. Of course having multiple files in the same zip would make it cleaner has you said, though this could also be acheived using specific directories for each zip file...
To show a thumbnail of an image stored in the zip archive you would need to:
Open the archive
Read the files content
Use imagecopyresized http://www.php.net/manual/en/function.i ... esized.php to resize the image to thumbnail size
Output the image content to standard output
I admit this is a simplyfied list but if need be I'll go into more details. It's the first time that I'd be attempting something like this but I don't see any problem with it...
EDIT: Sorry for the double post... I guess I clicked Preview after I actually submitted the post but before the page refreshed...
http://www.php.net/manual/en/ref.zip.php
On the other hand you realize that images are already compressed so the space saved isn't that great. Of course having multiple files in the same zip would make it cleaner has you said, though this could also be acheived using specific directories for each zip file...
To show a thumbnail of an image stored in the zip archive you would need to:
I admit this is a simplyfied list but if need be I'll go into more details. It's the first time that I'd be attempting something like this but I don't see any problem with it...
EDIT: Sorry for the double post... I guess I clicked Preview after I actually submitted the post but before the page refreshed...
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
Hi,
Like I said I've never attempted this before and I just realized that none of the 4 servers I have access to supports the ZZIPlib...
Direct quote from http://www.php.net/manual/en/ref.zip.php
The code given in the above URL will show the content (text) of the various files in a zip file so I believe that it's possible to make the same thing for images with a few modifications.
First you would need to create a PHP Script to open a zip file, retrieve a directory list, retrieve a file from the directory and then retrieve the file content. This script would be called like an image while passing it a few variables.
I think this file would look like this:
OK I obviously couldn't try this on any of my systems but I took extreme care to the coding and I believe that it's sound... If you do not have a maximum height you can delete the related variable and the $Size[1] > $MaxHeight part.
To display the thumbnails you would do the following:
I'm pretty sure there are ways to make this faster, though I think it should do very fine...
Anyone who can test this on a ZIP enabled server I'd very much like to hear about it.
Like I said I've never attempted this before and I just realized that none of the 4 servers I have access to supports the ZZIPlib...
Direct quote from http://www.php.net/manual/en/ref.zip.php
So make sure that's done before trying anything.You will need to use the --with-zip[=DIR] configuration option when compiling PHP to enable zip support.
The code given in the above URL will show the content (text) of the various files in a zip file so I believe that it's possible to make the same thing for images with a few modifications.
First you would need to create a PHP Script to open a zip file, retrieve a directory list, retrieve a file from the directory and then retrieve the file content. This script would be called like an image while passing it a few variables.
I think this file would look like this:
Code: Select all
<?php
Header("Content-type: image/jpeg");
// <?php Header() MUST be the VERY FIRST thing to appear at the top of the file, NO Spaces or line should be present above the <?php
// DisplayZipThumbnail.php
// Create a thumbnail from an image compressed inside a zip file
// Needed variables:
// $ZipFile - Name of the zip file
// $MaxHeight - Maximum Height
// $MaxWidth - Maximum Width
// $ZippedFile - Name of the image file inside the zip file
if (!empty($ZippedFile))
{
$zip = zip_open($ZipFile);
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
if (zip_entry_name($zip_entry) == $ZippedFile)
{
if (zip_entry_open($zip, $zip_entry, "r"))
{
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
fopen ("/tmp/$ZippedFile", "W"); // Writing the image to a tmp directory
}
break;
}
}
if (file_exists("/tmp/$ZippedFile")) // Checking that the tmp file was actually saved...
{
$Size = @getimagesize("/tmp/$ZippedFile");
$im = imagecreatefromjpeg ("/tmp/$ZippedFile");
if ($Size[0] > $MaxWidth): // Resize the width to fit the maximum width
$Width = $MaxWidth;
$Height = round(($Size[1]*$MaxWidth)/$Size[0]); // Keeping the width/height constraint
else:
$Width = $Size[0];
endif;
if ($Size[1] > $MaxHeight): // Resize the Height to fit the maximum height
$Height = $MaxHeight;
$Width = round(($Size[0]*$MaxHeight)/$Size[1]); // Keeping the width/height constraint
else:
$Height = $Size[1];
endif;
if (function_exists ("imagecopyresampled")
{
// Checks to see if you can use the resample function, this does a better job the imagecopyresized
// Note: imagecopyresampled() requires GD 2.0.l or greater.
imagecopyresampled ($im, $im, 0, 0, 0, 0, $Width, $Height, $Size[0], $Size[1]); // Resizing the image.
}
else
{
// Unfortunately imagecopyresample isn't active so we'll use this function instead.
imagecopyresized ($im, $im, 0, 0, 0, 0, $Width, $Height, $Size[0], $Size[1]); // Resizing the image.
}
imagejpeg ($im); // Output the thumbnail
ImageDestroy($im); // Destroy the image from memory we don't need it anymore.
unlink ("/tmp/$ZippedFile"); // Delete the tmp file
}
zip_close($zip);
}
}
?>To display the thumbnails you would do the following:
Code: Select all
<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php
// Let's display all the images (jpg) from the zip file named screenshot_1.zip
$ZipFile = "screenshot_1.zip"; // Of course point to the proper location
$MaxHeight = 100;
$MaxWidth = 75;
$zip = zip_open($ZipFile);
if ($zip) {
$index = 0;
while ($zip_entry = zip_read($zip)) // Let's build an array with all the images filename
{
$ImageName[$index] = zip_entry_name($zip_entry); // Get the filename
$ImageSize[$index] = zip_entry_filesize($zip_entry); // Get the filesize for display purpose
// If it's a jpg
if (strtolower(substr($ImageName[$index], -3)) == "jpg" OR strtolower(substr($ImageName[$index], -4)) == "jpeg")
{
$index ++; // Increase the index
}
}
zip_close($zip);
}
// We now have an array of image file name so now we can display the thumbnails
while (list($key, $ZippedFile) = each $ImageName)
{
$FileSize = intval($ImageSize[$index]/1024); // Convert to Kb
print "
<BR>\n
<IMG SRC="DisplayZipThumbnail.php?ZipFile=".$ZipFile."&MaxHeight=".$MaxHeight."&MaxWidth=".$MaxWidth."&ZippedFile=".$ZippedFile."" ALT="File size $FileSize Kb" BORDER="0"><BR>\n
File size $FileSize Kb<BR><BR>";
}
?>
</body>
</html>Anyone who can test this on a ZIP enabled server I'd very much like to hear about it.
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
Antitrust wrote:I get the following:
Parse error: parse error in /home/virtual/site19/fst/var/www/html/testzip.php on line 36
OOPS sorry about that change :
Code: Select all
<?php
while (list($key, $ZippedFile) = each $ImageName)
?>Code: Select all
<?php
while (list($key, $ZippedFile) = each ($ImageName))
?>- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
Well you download the zip file to your own computer, unzip it and form small thumbnail which you upload back to the server. It'll take up more space since you'll have to store the thumbnails but for now it's pretty much the only way to go, at least until your server admin activate's the zip module...
Expanding on Puckeye's answer (which is pretty damn spiffy)...
If you find yourself having to unzip something from a command line way from PHP, remember to use the backtick operator.
For example:
$blah=`ls -lt`;
would create a variable called $blah with the contents of a *nix directory listing.
`unzip file.zip`;
Would unzip the zip file
$dir=`dir`;
would get a directory listing
print $dir[0]; outputs filename.gif
The above is metacode, as i'm hazy on file-handling functions, but you should get the general idea. If you have an executable on your system, you can run it via PHP.
Oh, by the way... backtick is ` not ' or ". On most american keyboards it's on the same key as the tilde (~).
If you find yourself having to unzip something from a command line way from PHP, remember to use the backtick operator.
For example:
$blah=`ls -lt`;
would create a variable called $blah with the contents of a *nix directory listing.
`unzip file.zip`;
Would unzip the zip file
$dir=`dir`;
would get a directory listing
print $dir[0]; outputs filename.gif
The above is metacode, as i'm hazy on file-handling functions, but you should get the general idea. If you have an executable on your system, you can run it via PHP.
Oh, by the way... backtick is ` not ' or ". On most american keyboards it's on the same key as the tilde (~).