通常可以通过intermediate_image_sizes_advanced
钩唯一的问题是$sizes
数组可用,因此这对于一般级别来说已经足够了,但如果您想有条件地执行此操作,则不能这样做。有条件地执行此操作所需的信息可以通过debug_backtrace
- debug_backtrace
但性能方面的成本有点高。无论如何,我想这可以像下面所示:
add_action(
\'intermediate_image_sizes_advanced\',
\'wpse162413_unset_sizes_if_gif\'
);
function wpse162413_unset_sizes_if_gif( $sizes ) {
// we\'re only having the sizes available
// we\'re using debug_backtrace to get additional information
$dbg_back = debug_backtrace();
// scan $dbg_back array for function and get $args
foreach ( $dbg_back as $sub ) {
if ( $sub[ \'function\'] == \'wp_generate_attachment_metadata\' ) {
$args = $sub[ \'args\' ];
}
}
// attachment id
$att_id = $args[0];
// attachment path
$att_path = $args[1];
// split up file information
$pathinfo = pathinfo( $att_path );
// if extension is gif unset sizes
if ( $pathinfo[ \'extension\' ] == \'gif\' ) {
// get all intermediate sizes
$intermediate_image_sizes = get_intermediate_image_sizes();
// loop trough the intermediate sizes array
foreach ( $intermediate_image_sizes as $size ) {
// unset the size in the sizes array
unset( $sizes[ $size ] );
}
}
// return sizes
return $sizes;
}