在WordPress媒体库中上传WebP格式的图片

huangapple go评论70阅读模式
英文:

Uploading WebP images in WordPress Media Library

问题

在运行的 WordPress 6.1.1 和 PHP 7.0.33,托管在 Plesk Obsidian v18 和 Linux 7.9 上时,当我尝试通过WP的媒体库上传一个webp图像时,会收到以下警告:

"无法由Web服务器处理此图像。在上传之前将其转换为JPEG或PNG。"

我去检查了WP的站点健康 > 媒体处理,发现不支持webp。

有人有解决方法吗?

在WordPress媒体库中上传WebP格式的图片

英文:

Running WordPress 6.1.1 and PHP 7.0.33 hosted on Plesk Obsidian v18 and Linux 7.9

When I try to upload a webp image through WP's media library, I get this warning:

This image cannot be processed by the web server. Convert it to JPEG or PNG before uploading.

I went and checked WP's site health > Media Handling and see that webp is not supported.

Any one has a solution for this?

在WordPress媒体库中上传WebP格式的图片

答案1

得分: 2

我找到了这个对我有帮助的内容,我修改了我的XAMPP Apache配置。
取消注释此行:

extension=gd

资源链接:https://alvand.dev/blog/enable-webp-xampp-wordpress/

英文:

I found this that helped me, i change my XAMPP Apache config
Uncomment this line:

extension=gd

Resource: https://alvand.dev/blog/enable-webp-xampp-wordpress/

答案2

得分: 0

WordPress 从版本 5.8 开始增加了对 WebP 格式的支持,并且在您的新版本 6.1.1 中也支持。警告截图显示,您的 Imagick 库不支持 WebP 格式在支持的格式列表中。

您可以尝试更新 PHP 版本,或者请您的托管提供商确保在 Imagick 模块中启用了 libwebp 以支持 WebP。

或者,如果您具有 SSH 访问权限,您可以参考此答案来自行安装/配置它。

英文:

WordPress added support for WebP from version 5.8 onwards and is hence supported in your newer version 6.1.1 . The warning screenshot shows that your imagick library does not support WebP format in the list of formats suppoerted.

You can try updating PHP version or ask your hosting provider to make sure the libwebp for Webp support is enabled in the Imagick module.

OR

If you have SSH access you can refer to this answer on how to install/configure it on your own.

答案3

得分: 0

从WordPress版本5.8开始,默认支持WebP格式。无论如何,请将以下代码片段添加到您的活动主题的functions.php文件中。

function add_custom_upload_mimes( $mimes_types ) {
    $mimes_types['webp'] = 'image/webp'; // 支持WebP文件
    return $mimes_types;
}
add_filter( 'upload_mimes', 'add_custom_upload_mimes' );

function add_allow_upload_extension_exception( $types, $file, $filename, $mimes ) {
    // 进行基本的扩展名验证和MIME映射
    $wp_filetype = wp_check_filetype( $filename, $mimes );
    $ext = $wp_filetype['ext'];
    $type = $wp_filetype['type'];
    if( in_array( $ext, array( 'webp' ) ) ) { // 如果是WebP文件
        $types['ext'] = $ext;
        $types['type'] = $type;
    }
    return $types;
}
add_filter( 'wp_check_filetype_and_ext', 'add_allow_upload_extension_exception', 99, 4 );

function displayable_image_webp( $result, $path ) {
    if ($result === false) {
        $displayable_image_types = array( IMAGETYPE_WEBP );
        $info = @getimagesize( $path );

        if (empty($info)) {
            $result = false;
        } elseif (!in_array($info[2], $displayable_image_types)) {
            $result = false;
        } else {
            $result = true;
        }
    }

    return $result;
}
add_filter( 'file_is_displayable_image', 'displayable_image_webp', 10, 2 );

以上是代码部分的翻译。

英文:

From wordpress version 5.8, webp is supported by default. Anyways add the below code snippet in your active theme's functions.php file.

function add_custom_upload_mimes( $mimes_types ) {
    $mimes_types['webp'] = 'image/webp'; // webp files
    return $mimes_types;
}
add_filter( 'upload_mimes', 'add_custom_upload_mimes' );

function add_allow_upload_extension_exception( $types, $file, $filename, $mimes ) {
    // Do basic extension validation and MIME mapping
      $wp_filetype = wp_check_filetype( $filename, $mimes );
      $ext         = $wp_filetype['ext'];
      $type        = $wp_filetype['type'];
    if( in_array( $ext, array( 'webp' ) ) ) { // if follows webp files have
      $types['ext'] = $ext;
      $types['type'] = $type;
    }
    return $types;
}
add_filter( 'wp_check_filetype_and_ext', 'add_allow_upload_extension_exception', 99, 4 );
  


function displayable_image_webp( $result, $path ) {
    if ($result === false) {
        $displayable_image_types = array( IMAGETYPE_WEBP );
        $info = @getimagesize( $path );

        if (empty($info)) {
            $result = false;
        } elseif (!in_array($info[2], $displayable_image_types)) {
            $result = false;
        } else {
            $result = true;
        }
    }

    return $result;
}
add_filter( 'file_is_displayable_image', 'displayable_image_webp', 10, 2 );

huangapple
  • 本文由 发表于 2023年3月1日 14:41:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定