Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neater way to find and display image file from ZIP archive with PHP

Tags:

php

image

zip

function showimage($zip_file, $file_name) {
    if (file_exists($zip_file)) {
        $zip = zip_open($zip_file);
        while ($zip_entry = zip_read($zip)) {
            if (zip_entry_open($zip, $zip_entry, "r")) {
                if (zip_entry_name($zip_entry) == $file_name) {
                    $theimg = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                    $theimg = imagecreatefromstring($theimg);
                    if ($theimg !== false) {
                        header('Content-Type: image/jpeg');
                        imagejpeg($theimg);
                        imagedestroy($theimg);
                    }
                    else { echo "Could not create image."; }
                    zip_entry_close($zip_entry);
                }
            }
            else { echo "Could not open."; }
        }
        zip_close($zip);
    }
    else { echo "File not found."; }
}

I'm running this function to open a specified zip file and then looping through the contents to find the specified file name, then creating an image from that file without the need to extract. I was just kind of curious how system intensive this process was, and if there was a neater/more direct manner of finding a file within a zip archive without the need to loop through and see if the name matches the given file name. Is there anyway to call a file directly from the zip file with the given name, assuming it exists?

The above code works... I guess I'm just trying to see how to do it better. If that makes sense.

like image 832
Skryn Avatar asked Feb 20 '23 19:02

Skryn


1 Answers

ZipArchive has a method for getting the file without actually searching for it.

function showimage($zip_file, $file_name) {
    $z = new ZipArchive();
    if ($z->open($zip_file) !== true) {
        echo "File not found.";
        return false;
    }

    $stat = $z->statName($file_name);
    $fp   = $z->getStream($file_name);
    if(!$fp) {
        echo "Could not load image.";
        return false;
    }

    header('Content-Type: image/jpeg');
    header('Content-Length: ' . $stat['size']);
    fpassthru($fp);
    return true;
}
like image 50
rrehbein Avatar answered Apr 26 '23 00:04

rrehbein