BUG PNG to WEBP conversion

Vred
Member | 9
+
0
-

Hello,
I have a small problem when converting PNG to WEBP, when an empty file is created.

My code is

$image = Image::fromString( $fileContents, $type );

$image->resize(800, null);

dump($image->toString($image->getWidth()); //Prints the value 800
dump($image->toString(Image::WEBP, 85)); // Prints the value ''

Uploading PNG image with size 800×600, 196 kB

But if I change $image->resize(801, null); or $image->resize(799, null); then everything works as it should. Yes, I can fix it this way using IF which I will obviously have to do but it seems like a BUG to me.

Infanticide0
Member | 106
+
0
-

Hello @Vred,

Image::toString does not expect int 800 as parameter.

Image::toString(int $type = ImageType::JPEG, ?int $quality = null): string

I tried this and it works as expected.

    $image = Image::fromFile(__DIR__ . "/img.PNG", $type); // $type = 3 = IMAGETYPE_PNG
    //$image->getWidth(); = 232
    $image->resize(800, null);
    //$image->getWidth(); = 800

    $this->sendResponse(new CallbackResponse(function() use($image) {
        $this->getHttpResponse()->addHeader("Content-type", "image/webp");
        echo $image->toString(Image::WEBP, 85);
    }));

Different image works?

“nette/utils”: “^4.0.5”,

Last edited by Infanticide0 (2024-08-22 19:06)

Vred
Member | 9
+
0
-

Sorry, dump is of course dump($image->getWidth());

“nette/utils”: “^3.2”,

Vred
Member | 9
+
0
-

Everything worked on PHP8.1, I upgraded to PHP8.3 and it works.