Skryn
Skryn

Reputation: 155

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

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.

Upvotes: 4

Views: 4817

Answers (2)

rrehbein
rrehbein

Reputation: 4170

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;
}

Upvotes: 6

Stefan
Stefan

Reputation: 2194

There is no need of using the GD Library neither writing to a file or anything else, as Brad said above. Just echo the result of the zip_entry_read() function and set the Content-Type. You could use something like this:



    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));
                        if ($theimg !== null) {
                            header('Content-Type: image/jpeg');
                            echo $theimg;
                        }
                        else { echo "Could not load image."; }
                        zip_entry_close($zip_entry);
                    }
                }
                else { echo "Could not open."; }
            }
            zip_close($zip);
        }
        else { echo "File not found."; }
    }

I think just not loading the GD library should improve the performance pretty good.

Upvotes: 2

Related Questions