contao/image

Improve zlib stream test

discordier opened this issue · 3 comments

The current test is not enough and may lead to

PHP message: PHP Warning:  XMLReader::open(): 
Unable to open source data in /vendor/contao/image/src/Image.php on line 201

The following diff solves the issue.

         if (null === $zlibSupport) {
-            $zlibSupport = \in_array('compress.zlib', stream_get_wrappers(), true);
+            $zlibSupport = \in_array('compress.zlib', stream_get_wrappers(), true)
+                && ($reader = new XMLReader)
+                && @$reader->open('compress.zlib://data:text/xml,<x/>') === true
+                && @$reader->read() === true
+                && @$reader->close() === true;
         }

It is not necessary to test if the object can be instantiated etc., is it?

if (null === $zlibSupport) {
    $zlibSupport = \in_array('compress.zlib', stream_get_wrappers(), true);
}

if (false === $zlibSupport) {
    $reader = new XMLReader();

    if (true ===  @$reader->open('compress.zlib://data:text/xml,<x/>')) {
        $zlibSupport = true;
    }

    @$reader->close();
}
ausi commented

The whole result should be saved in the static $zlibSupport variable so that it only has to be checked once.

ausi commented

See #41