你可以试试image_editor_save_pre
过滤器:
add_filter( \'image_editor_save_pre\', \'custom_image_editor_save_pre\', 10 , 2 );
function custom_image_editor_save_pre( $image, $post_id){
// your stuff here
return $image;
}
裁剪/旋转图像并按下保存按钮时,似乎会调用此过滤器(这是一个预过滤器)
这个$image
是类型的对象WP_Image_Editor_GD
下面是一个示例,说明它在过滤器中的外观:
WP_Image_Editor_GD Object
(
[image:protected] => Resource id #172
[file:protected] => /absolute/path/to/wordpress/install/wp-content/uploads/2013/07/car.jpg
[size:protected] => Array
(
[width] => 220
[height] => 330
)
[mime_type:protected] => image/jpeg
[default_mime_type:protected] => image/jpeg
[quality:protected] => 90
)
以及
$post_id
是附件id。
也许还有其他更好的钩子可以找,但我在
/wp-admin/includes/image-edit.php
在此功能中:
/**
* Saves Image to File
*
* @param string $filename
* @param WP_Image_Editor $image
* @param string $mime_type
* @param int $post_id
* @return boolean
*/
function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
if ( $image instanceof WP_Image_Editor ) {
$image = apply_filters(\'image_editor_save_pre\', $image, $post_id);
$saved = apply_filters(\'wp_save_image_editor_file\', null, $filename, $image, $mime_type, $post_id);