简体   繁体   English

Wordpress webp 图像预览

[英]Wordpress webp image previews

I have updated wordpress using the following code to allow for webp uploads,我使用以下代码更新了 wordpress 以允许 webp 上传,

function webp_upload_mimes( $existing_mimes ) {
    $existing_mimes['webp'] = 'image/webp';
    return $existing_mimes;
}
add_filter( 'mime_types', 'webp_upload_mimes' );

Which works great, but the webp images do not show a preview in the media selector, as shown效果很好,但是 webp 图像没有在媒体选择器中显示预览,如图所示

在此处输入图片说明

Is there anyway I can force wordpress to render webp previews?无论如何我可以强制wordpress渲染webp预览吗? By the time my site is done, it could potentially have hundreds of webp images, not being able to see them when selecting could be a huge pain!当我的网站完成时,它可能有数百个 webp 图像,在选择时无法看到它们可能是一个巨大的痛苦!

Update July 2021 2021 年 7 月更新

From WordPress version 5.8 forward, you can upload and use WebP images in WordPress like you would a JPEG or PNG image today (as long as your hosting service supports WebP).从 WordPress 5.8 版开始,您可以像今天使用 JPEG 或 PNG 图像一样在 WordPress 中上传和使用 WebP 图像(只要您的托管服务支持 WebP)。 Switching to the WebP format for your images will improve your site's performance and your site visitor's experience.将图像切换为 WebP 格式将提高您网站的性能和网站访问者的体验。
https://make.wordpress.org/core/2021/06/07/wordpress-5-8-adds-webp-support/ https://make.wordpress.org/core/2021/06/07/wordpress-5-8-adds-webp-support/


I found a solution to show the thumbnails on the media manager.我找到了在媒体管理器上显示缩略图的解决方案。 You have to add the following code to the functions.php of your active theme:您必须将以下代码添加到活动主题的functions.php中:

//enable upload for webp image files.
function webp_upload_mimes($existing_mimes) {
    $existing_mimes['webp'] = 'image/webp';
    return $existing_mimes;
}
add_filter('mime_types', 'webp_upload_mimes');

//enable preview / thumbnail for webp image files.
function webp_is_displayable($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', 'webp_is_displayable', 10, 2);

The webp_is_displayable function is using the file_is_displayable_image hook and checks if the file (on $path ) is a webp image file. webp_is_displayable函数使用file_is_displayable_image挂钩并检查文件(在$path )是否为webp图像文件。 To check for the webp image file the function is using the constant IMAGETYPE_WEBP .为了检查webp图像文件,该函数使用常量IMAGETYPE_WEBP

The solution proposed by Sebastian Brosch is still working. Sebastian Brosch 提出的解决方案仍然有效。 But as S_R commented, the old webp images won't have a working preview.但正如 S_R 评论的那样,旧的 webp 图像不会有工作预览。 For that, you can use the wordpress plugin "Force Regenerate Thumbnails" to reload old images and create previews.为此,您可以使用 wordpress 插件“强制重新生成缩略图”来重新加载旧图像并创建预览。

One more thing, if you want WordPress to allow you to upload webp to the media/library you can use this code:还有一件事,如果您希望 WordPress 允许您将 webp 上传到媒体/库,您可以使用以下代码:

//** Enable upload for webp image files.
function webp_upload_mimes($existing_mimes) {
    $existing_mimes['webp'] = 'image/webp';
    return $existing_mimes;
}
add_filter('mime_types', 'webp_upload_mimes');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM