Allow Webp image(高级视图):WordPress插件WP插件

电脑软件评论1,07710阅读模式
摘要WebP 是一种可以同时提供有损压缩和透明度的图片文件格式

WebP 是 Google 的一种可以同时提供有损压缩(像 JPEG 一样)和透明度(像 PNG 一样)的图片文件格式,不过与 JPEG 或 PNG 相比,这种格式可以提供更好的压缩。Android 4.0(API 级别 14)及更高版本支持有损 WebP 图片,Android 4.3(API 级别 18)及更高版本支持无损且透明的 WebP 图片。简单的说,WebP就是减少文件大小,达到Png和JPEG相同的图片质量,是网站优化的必要手段。

Allow Webp image(高级视图):WordPress插件文章源自甲骨虫-https://www.icqone.com/15890.html

WordPress插件Allow Webp image(高级视图)专门解决WordPress网站程序不能上传Webp格式图片问题。有兴趣的朋友可以在本页面底部找到下载链接。新版Safari和微信已支持webp图片。文章源自甲骨虫-https://www.icqone.com/15890.html

动手能力强的也可以采取知更鸟提供的方法:文章源自甲骨虫-https://www.icqone.com/15890.html

将下面代码添加到当前主题函数模板functions.php中,即可解决上传问题。文章源自甲骨虫-https://www.icqone.com/15890.html

    function webp_filter_mime_types( $array ) {
    	$array['webp'] = 'image/webp';
    	return $array;
    }
    add_filter( 'mime_types', 'webp_filter_mime_types', 10, 1 );

展开收缩
文章源自甲骨虫-https://www.icqone.com/15890.html
    function webp_upload_mimes($existing_mimes) {
        $existing_mimes['webp'] = 'image/webp';
        return $existing_mimes;
    }
    add_filter('mime_types', 'webp_upload_mimes');

文章源自甲骨虫-https://www.icqone.com/15890.html

虽然已经可以上传WebP格式的图片了,但在媒体列表中看不到缩略图,这是因为WordPress在用 wp_generate_attachment_metadata()函数生成图片数据时,使用了file_is_displayable_image()函数判断文件是否为图片,判断WebP图片的结果为否,因此中断了保存图片数据的操作。文章源自甲骨虫-https://www.icqone.com/15890.html

该函数位于:wp-admin/includes/image.php文章源自甲骨虫-https://www.icqone.com/15890.html

解决办法是在主题的functions.php里添加以下代码:文章源自甲骨虫-https://www.icqone.com/15890.html

    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 );

展开收缩
文章源自甲骨虫-https://www.icqone.com/15890.html
    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图片,以目前的网络速度使用webp图片并不完全为了加载快,而是当图片很多时webp图片优势就明显了,可以减少三分一以上的空间占用。虽然目前七牛、又拍云、阿里云oss、腾讯云cos等都支持WebP,可惜之前苹果设备不支持webp图片,这也可能是WordPress一直不支持webp图片的原因吧。

不想改代码的,可以安装插件:Allow Webp image

另:如何将JPG格式的图片转换为WEBP格式?

答:格式工厂

下载信息 资源名称:Allow Webp image 资源大小:21k
最近更新2024-1-14
下载地址
 最后更新:2024-1-14
  • 版权声明:本站作为资源聚合网站,我们不提供任何资源的上传、下载、存储,版权归属原著作权人,本站仅提供兴趣及技术研究,访问和下载与否,均与本站无关。
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证