gpt4 book ai didi

php - WordPress webp 图像预览

转载 作者:行者123 更新时间:2023-12-03 02:40:24 25 4
gpt4 key购买 nike

我已经使用以下代码更新了 WordPress 以允许 WebP 上传,

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

效果很好,但 webp 图像不会在媒体选择器中显示预览,如图所示

enter image description here

我是否可以强制 WordPress 渲染 WebP 预览?当我的网站完成时,它可能有数百个 webp 图像,在选择时看不到它们可能是一个巨大的痛苦!

最佳答案

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). Switching to the WebP format for your images will improve your site’s performance and your site visitor’s experience.
https://make.wordpress.org/core/2021/06/07/wordpress-5-8-adds-webp-support/

<小时/>

我找到了在媒体管理器上显示缩略图的解决方案。您必须将以下代码添加到事件主题的 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);
<小时/>

webp_is_displayable 函数正在使用 file_is_displayable_image Hook 并检查文件(在 $path 上)是否是 webp 图像文件。为了检查 webp 图像文件,该函数使用常量 IMAGETYPE_WEBP .

关于php - WordPress webp 图像预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54442929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com