为WordPress开启webp和svg支持

webp是Google出的一种图片格式,是一种同时提供了有损压缩与无损压缩(可逆压缩)的图片文件格式,派生自影像编码格式VP8,被认为是WebM多媒体格式的姊妹项目,是由Google在购买On2 Technologies后发展出来,以BSD授权条款发布。

让WordPress支持webp,能对文件体积大幅度减小。

在主题或者插件添加如下代码来支持webp


  1. function webp_filter_mime_types($array)
  2. {
  3. $array['webp'] = 'image/webp';
  4. return $array;
  5. }
  6. add_filter('mime_types', 'webp_filter_mime_types');

支持上传了,但是不支持预览,需要手动处理,处理后能在媒体中心直接预览


  1. function webp_file_display($result, $path) {
  2. $info = @getimagesize( $path );
  3. if($info['mime'] == 'image/webp') {
  4. $result = true;
  5. }
  6. return $result;
  7. }
  8. add_filter( 'file_is_displayable_image', 'webp_file_display');

支持SVG

SVG是一种图像文件格式,它的英文全称为Scalable Vector Graphics,意思为可缩放的矢量图形。可以无限制放大图片大小不失真。

在主题或者插件添加如下代码来支持svg

下面的函数是让WordPress支持上传svg文件,同时,这个函数修改修改还能支持上传其他的文件,只需要添加到数组即可


  1. function upload_support($mimes = array())
  2. {
  3. $mimes['svg'] = 'image/svg+xml';
  4. return $mimes;
  5. }
  6. add_filter('upload_mimes', 'upload_support');

你可能感兴趣的:(wordpress,php,前端)