Skip to content

Supporting webp format#21

Open
znagy wants to merge 1 commit into
yurkinx:masterfrom
znagy:webp-support
Open

Supporting webp format#21
znagy wants to merge 1 commit into
yurkinx:masterfrom
znagy:webp-support

Conversation

@znagy

@znagy znagy commented Mar 25, 2022

Copy link
Copy Markdown

Allowing webp format for GD

$type = IMAGETYPE_WEBP;

// Quality is different in webp than PNG
$quality = 85;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. It's better not to set $quality value here (webp handling should be similar to the one for jpeg, not gif or png).

With the PR-suggested change, end-users won't have control over the resulting webp images quality, even passing $quality to save/render functions. Note that $quality is passed by reference to _save_function. So we get the value of 85 to be "hard-coded", instead of the expected behavior.


// Quality is different in webp than PNG
$quality = 85;
break;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check this line: my editor shows trailing spaces after the break;.

$quality = 85;
break;
default:
throw new ErrorException(sprintf('Installed GD does not support %s images',$extension));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note, not related to the PR directly: I really think this error message could be improved. Not handling webp prior this PR, for instance, doesn't mean Installed GD does not support it. Hence the error message is misleading, and could be improved too, whilst we're looking at this part of code.

@yugaego

yugaego commented Apr 19, 2023

Copy link
Copy Markdown

Well, I've wrapped up the above said into yugaego@ad4b181 and for now load it in my project with

composer.json
{
    "require": {
        "yurkinx/yii2-image": "dev-webp"
    },
    "repositories": [
        {
            "type": "vcs",
            "url":  "git@github.com:yugaego/yii2-image.git"
        }
    ]
}

The linked branch also adds WebP support to Imagick driver, since compiling imagick with WebP support seems to be a common option nowadays.

For reference, to quick check whether WebP support is enabled on the server, do:

  • $ php -r "print_r(gd_info());"
  • $ php -r "print_r(Imagick::queryFormats());"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants