如何实现WordPress支持WebP格式图片上传
source link: https://www.huhexian.com/13489.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
如何实现WordPress支持WebP格式图片上传
WordPress默认是不支持WebP格式图片上传,我们在后台设置里面将WebP放进允许上传格式也没有反应,那么我们应该如何实现WordPress支持WebP格式图片上传呢,下面我们就来看看吧!
将下面代码添加到当前主题函数模板functions.php中。
- function webp_filter_mime_types( $array ) {
- $array['webp'] = 'image/webp';
- return $array;
- add_filter( 'mime_types', 'webp_filter_mime_types', 10, 1 );
- function webp_upload_mimes($existing_mimes) {
- $existing_mimes['webp'] = 'image/webp';
- return $existing_mimes;
- add_filter('mime_types', 'webp_upload_mimes');
虽然已经可以上传WebP格式的图片了,但在媒体列表中看不到缩略图,这是因为WordPress在用 wp_generate_attachment_metadata()函数生成图片数据时,使用了file_is_displayable_image()函数判断文件是否为图片,判断WebP图片的结果为否,因此中断了保存图片数据的操作。
该函数位于:wp-admin/includes/image.php
- function file_is_displayable_image( $path ) {
- $displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO );
- $info = @getimagesize( $path );
- if ( empty( $info ) ) {
- $result = false;
- } elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) {
- $result = false;
- } else {
- $result = true;
- * Filters whether the current image is displayable in the browser.
- * @since 2.5.0
- * @param bool $result Whether the image can be displayed. Default true.
- * @param string $path Path to the image.
- return apply_filters( 'file_is_displayable_image', $result, $path );
解决办法是在主题的functions.php里添加以下代码:
- function webp_file_is_displayable_image($result, $path) {
- $info = @getimagesize( $path );
- if($info['mime'] == 'image/webp') {
- $result = true;
- return $result;
- add_filter( 'file_is_displayable_image', 'webp_file_is_displayable_image', 10, 2 );
- 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图片,虽然目前七牛、又拍云、阿里云oss、腾讯云cos等都支持WebP,不过发现苹果设备并不支持webp图片,包括IOS版的微信,这也可能是WordPress一直不支持webp图片的原因吧。
如果嫌改代码麻烦可以安装插件:Allow Webp image
Recommend
-
3
I'm 李闯Lucas 10余年互联网技术老鸟,全栈工程师。不会...
-
3
upload上传组件的使用方法 上传图片后自动上传(也可以手动上传),图片上传成功后由后端返回特定图片地址,在表单提交后将表单数据同图片地址一并返回即可完成图片上传功能。 组件HTML
-
5
WordPress上传图片出现“图像后期处理失败,请将其缩小到2500像素并重新上传”如果您发现本...
-
4
wordpress学习 移除WordPress上传图片的宽度和高度参数 我们都知道在wordpress后台上传的图片添加到日志内会附带宽高属性。虽然在 HTML...
-
3
iview实现文件上传 限制上传格式、大小 精选 原创 问题描述:下面以CRMEB Pro版产品为例1.当上传的文件...
-
5
ionic2中选取相册图片并支持多附件上传 最近在研究的ionic2,准备公司项目用该框架来写。近期在做多附件上传时遇到问题了,先简单的说下自己所遇到的具体问题和解决方案。另外强调下,由于web这块之前是一窍不通,经过一段时间的学习从最简单的html、c...
-
2
跨境全洞察!eBay支持listing上传24张图片;2022年搜索量最高的100种产品 ...
-
2
1219 - iPic 支持上传图片或文件至 Backblaze B2 云存储 ...
-
1
0319 - 新版 iPic 支持上传图片、文件至 Cloudflare R2 云存储 ...
-
71
项目中需要上传图片可谓是经常遇到的需求,本文将介绍 3 种不同的图片上传方式,在这总结分享一下,有什么建议或者意见,请大家踊跃提出来。 没有业务场景的功能都是耍流氓,那么我们先来模拟一个需要实现的业务场景。假设我们要做一个后台系统添加商品的页面,有...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK