要了解您在过滤器或操作中使用哪些数据,并查看哪些变量名称包含哪些值,可以使用如下所示的辅助函数。
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 );
}
}
这将在您的 WordPress 安装的根目录中创建一个 gdwebpconvert.log 文件,并且您放入 debug($value_xyz); 的任何字符串、整数、浮点数或数组都将记录到该文件中,并带有日期和时间。在 Linux 上,您只需转到保存文件的目录并执行 tail -f gdwebpconvert.log 即可,该文件的最新条目将显示在终端中。
作为替代方案,您可以通过将这些行添加到 wp-config.php 来使用 WordPress 自己的调试功能。
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
这将导致信息输出到debug.log 文件,同样位于您的 WordPress 安装的根目录中,尽管它也会一直在其中添加大量数据。所以我更喜欢上面的小辅助函数来获取我当前想要查看的值,而无需搜索 debug.log 文件以查找我要查找的内容。
至于转换,当我看到我正在处理的数据时,我编写了一个类,将上传的图像及其在上传完成后创建的所有大小转换为 WebP 格式。继续取消注释 debug() 声明以跟进。鉴于此https://github.com/Imagick/imagick/issues/358 以及自 WordPress 5.2 以来我使用 Imagick 描述的问题,这提供了一个简单的解决方案,只需在服务器上创建文件的 WebP 版本,然后您可以以任何您喜欢的方式使用,而无需自动添加 .htaccess 或其他功能.
随意做你想要和需要的。 ;)
<?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/">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 版本。如果您删除帖子,这不会根据 WordPress 的默认行为发生,因为您永远不知道您是否可能在另一篇文章中需要该文件。
如果您想充分利用这个类,请确保您默认为 GD 图像编辑器。 => https://support.pagely.com/hc/en-us/articles/115000052451
<?php
/**
* Plugin Name: Use GD For Image Processing
* Plugin URI: https://support.pagely.com/hc/en-us/articles/115000052451
* Description: Sets GD to the default image processor.
* Version: 1.0.0
* Requires at least: 5.5
* Requires PHP: 7.2
* Author: JeffMatson, Pagely
* Author URI: https://pagely.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
add_filter( 'wp_image_editors', 'pagely_default_to_gd' );
function pagely_default_to_gd() {
return array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
}
谢谢,简!