Get image size from ZIP compressed images

Hello,

I would like to get the image size of zip compressed images.

Getting the actual image size is easy:


$resource = NewMagickWand();
MagickReadImage( $resource, $fotofile );
$foto_width = MagickGetImageWidth( $resource ); 
$foto_height = MagickGetImageHeight( $resource );

but how could I temporarly open a zip file and pass it’s content to the above function?

To make it easy, let assume, all ZIP files only contain the one image file. What I will not know, is the filetype. It could be a .psd, a .tiff or a .jpeg.

Thanks for suggestions.

Regards
Flözen


$zip = new ZipArchive();
if (true !== $zip->open($file))
{
    throw new Exception('Could not open ZIP archive');
}

// Search for the image file.
for($i = 0; $i < $zip->numFiles; $i++)
{
    $entry = $zip->statIndex($i);
    $ext = substr($entry['name'], -3);
    if (in_array($ext, array('jpg', 'png'))
    {
        $filename = $entry['name'];
    }
}

if (isset($filename) && ($image = $zip->getFromName($filename)))
{
    list($width, $height) = getimagesize($image);
}
else
{
    throw new Exception('No image found');
}

unzip it

@AlienDev
thanks allot, that looks good.

But one more question: How could I make sure, that it is an image?
For Example MacOS ZIP files also contain a file lie “.__name.jpg” which is not an image, but contains the Mac resource (as far as I know)?

Change
if (in_array($ext, array(‘jpg’, ‘png’))
to
if (in_array($ext, array(‘jpg’, ‘png’) && ($entry[‘name’][0] != ‘.’))

thanks!

Not a big problem, but

$entry[‘name’][0]

won’t count a “.” …