webp格式的图片有点我就不说了,不明白的可以看看下面的描述:
WebP,是一种同时提供了有损压缩与无损压缩的图片文件格式,派生自视频编码格式 VP8。WebP 最初在2010年发布,目标是减少文件大小,但达到 和 JEPG 格式相同的图片质量,希望能够减少图片档在网络上的发送时间。2011年11月8日,Google 开始让 WebP 支持无损压缩和透明色的功能。
现在很多站点已经开始使用这个格式的图片作为主力,但是我个人还是比较推荐保留两种格式,毕竟不是所有人都喜欢webp格式的文件,同时存在源文件格式和webp格式文件才是最优解,毕竟现在的硬盘空间已经不值钱了。
看了上面的所有说明,你没有顾虑,可以看看下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
<?php /** * Plugin Name: GD WebP Converter * Plugin URI: https://stackoverflow.com/a/67234000 * Description: After uploading an image it will be converted to WebP format using the GD image engine. <a target="_blank" href="https://developer.wordpress.org/reference/classes/wp_image_editor_gd/" rel="noopener">WP GD Image Engine</a> If the file is deleted form the Media Library the created WebP conversions will also be deleted. * Version: 1.0.0 * Requires at least: 5.5 * Requires PHP: 7.2 * Author: lowtechsun * Author URI: https://stackoverflow.com/users/1010918/lowtechsun * License: GPL v2 or later * License URI: https://www.gnu.org/licenses/gpl-2.0.html */ //================================================= // Security: Abort if this file is called directly //================================================= if ( ! defined( 'ABSPATH' ) ) { die; } function debug( $info ) { $message = null; if ( is_string( $info ) || is_int( $info ) || is_float( $info ) ) { $message = $info; } else { $message = var_export( $info, true ); } if ( $fh = fopen( ABSPATH . '/gdwebpconvert.log', 'a' ) ) { fputs( $fh, date( 'Y-m-d H:i:s' ) . " $message\n" ); fclose( $fh ); } } add_filter( 'wp_generate_attachment_metadata', 'gd_webp_converter', 10, 2 ); function gd_webp_converter( $metadata, $attachment_id ) { $gd_webp_converter = new GDWebPConverter( $attachment_id ); $gd_webp_converter->check_file_exists( $attachment_id ); $gd_webp_converter->check_mime_type(); $gd_webp_converter->create_array_of_sizes_to_be_converted( $metadata ); $gd_webp_converter->convert_array_of_sizes(); return $metadata; } class GDWebPConverter { private $file_path; private $file_dirname; private $file_ext; private $file_name_no_ext; private $array_of_sizes_to_be_converted = array(); private $array_of_sizes_to_be_deleted = array(); public function __construct( $attachment_id ) { $this->file_path = get_attached_file( $attachment_id ); debug( $this->file_path ); // https://stackoverflow.com/questions/2183486/php-get-file-name-without-file-extension/19040276 $this->file_dirname = pathinfo( $this->file_path, PATHINFO_DIRNAME ); debug( $this->file_dirname ); $this->file_ext = strtolower( pathinfo( $this->file_path, PATHINFO_EXTENSION ) ); debug( $this->file_ext ); $this->file_name_no_ext = pathinfo( $this->file_path, PATHINFO_FILENAME ); debug( $this->file_name_no_ext ); } public function check_file_exists( $attachment_id ) { $file = get_attached_file( $attachment_id ); if ( ! file_exists( $file ) ) { $message = 'The uploaded file does not exist on the server. Encoding not possible.'; debug( $message ); throw new Exception( 'The uploaded file does exist on the server. Encoding not possible.', 1 ); } } public function check_mime_type() { // https://www.php.net/manual/en/function.finfo-file.php $finfo = finfo_open( FILEINFO_MIME_TYPE ); $this->file_mime_type = finfo_file( $finfo, $this->file_path ); finfo_close( $finfo ); // debug( $this->file_mime_type ); // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types $this->allowed_mime_type = array( 'image/jpeg', 'image/png' ); if ( ! in_array( $this->file_mime_type, $this->allowed_mime_type, true ) ) { $message = 'MIME type of file not supported'; // debug( $message ); throw new Exception( 'MIME type of file not supported', 1 ); } } public function create_array_of_sizes_to_be_converted( $metadata ) { // push original file to the array array_push( $this->array_of_sizes_to_be_converted, $this->file_path ); // debug( $this->array_of_sizes_to_be_converted ); // push all created sizes of the file to the array foreach ( $metadata['sizes'] as $value ) { // debug( $value['file'] ); array_push( $this->array_of_sizes_to_be_converted, $this->file_dirname . '/' . $value['file'] ); } // // debug( $this->array_of_sizes_to_be_converted ); } public function convert_array_of_sizes() { debug( $this->array_of_sizes_to_be_converted ); switch ( $this->file_ext ) { case 'jpeg': case 'jpg': foreach ( $this->array_of_sizes_to_be_converted as $key => $value ) { $image = imagecreatefromjpeg( $value ); if ( 0 === $key ) { imagewebp( $image, $this->file_dirname . '/' . $this->file_name_no_ext . '.webp', 80 ); } else { $current_size = getimagesize( $value ); // debug( $current_size ); imagewebp( $image, $this->file_dirname . '/' . $this->file_name_no_ext . '-' . $current_size[0] . 'x' . $current_size[1] . '.webp', 80 ); } imagedestroy( $image ); } break; case 'png': foreach ( $this->array_of_sizes_to_be_converted as $key => $value ) { $image = imagecreatefrompng( $value ); imagepalettetotruecolor( $image ); imagealphablending( $image, true ); imagesavealpha( $image, true ); if ( 0 === $key ) { imagewebp( $image, $this->file_dirname . '/' . $this->file_name_no_ext . '.webp', 80 ); } else { $current_size = getimagesize( $value ); // debug( $current_size ); imagewebp( $image, $this->file_dirname . '/' . $this->file_name_no_ext . '-' . $current_size[0] . 'x' . $current_size[1] . '.webp', 80 ); } imagedestroy( $image ); } break; // animated GIF to WebP not supported by GD - imagecreatefromgif // case 'gif': // foreach ( $this->array_of_sizes_to_be_converted as $key => $value ) { // $image = imagecreatefromgif( $value ); // if ( 0 === $key ) { // imagewebp( $image, $this->file_dirname . '/' . $this->file_name_no_ext . '.webp', 80 ); // } else { // $current_size = getimagesize( $value ); // // debug( $current_size ); // imagewebp( $image, $this->file_dirname . '/' . $this->file_name_no_ext . '-' . $current_size[0] . 'x' . $current_size[1] . '.webp', 80 ); // } // imagedestroy( $image ); // } // break; default: return false; } } public function create_array_of_sizes_to_be_deleted( $attachment_id ) { // debug( $attachment_id ); $this->attachment_metadata_of_file_to_be_deleted = wp_get_attachment_metadata( $attachment_id ); // debug( $this->attachment_metadata_of_file_to_be_deleted ); // push original file to the array array_push( $this->array_of_sizes_to_be_deleted, $this->file_dirname . '/' . $this->file_name_no_ext . '.webp' ); // debug( $this->array_of_sizes_to_be_converted ); // push all created sizes of the file to the array foreach ( $this->attachment_metadata_of_file_to_be_deleted['sizes'] as $value ) { // debug( $value ); $this->value_file_name_no_ext = pathinfo( $value['file'], PATHINFO_FILENAME ); // debug( $this->value_file_name_no_ext ); array_push( $this->array_of_sizes_to_be_deleted, $this->file_dirname . '/' . $this->value_file_name_no_ext . '.webp' ); } // debug( $this->array_of_sizes_to_be_deleted ); } public function delete_array_of_sizes() { debug( $this->array_of_sizes_to_be_deleted ); foreach ( $this->array_of_sizes_to_be_deleted as $key => $value ) { // debug( $value ); unlink( $value ); } } } add_action( 'delete_attachment', 'delete_webp_conversions', 10 ); function delete_webp_conversions( $attachment_id ) { $delete_webp_conversions = new GDWebPConverter( $attachment_id ); $delete_webp_conversions->create_array_of_sizes_to_be_deleted( $attachment_id ); $delete_webp_conversions->delete_array_of_sizes(); } |
经过测试之后,上传图片确实是可以在后台生成对应的webp图片,效果截图如下:
同样的,你可以尝试一下,上方的图片修改后缀为webp同样可以看到图片。
上面的代码直接保存为一个php文件之后,在function文件中通过include导入即可完成该功能。
[hong unique_number=”hong_c7763c9c91264932b1f2339b58d88053″]
经过尝试,这里需要注意的就是PHP方面需要开启fileinfo扩展,否则会报错,报错内容大概为:Call to undefined function finfo_open()
[/hong]