Answers:
您可以使用wp_generate_attachment_metadata
过滤器:
function wpse_183835_to_jpeg( $meta, $post_id ) {
$sizes_to_convert = array(
'thumbnail',
);
$path = dirname( get_attached_file( $post_id ) );
foreach ( $sizes_to_convert as $size ) {
if ( ! empty( $meta['sizes'][ $size ] ) ) {
$data = $meta['sizes'][ $size ];
if ( $data['mime-type'] === 'image/png' && is_file( $file = "$path/{$data['file']}" ) ) {
if ( $image =@ imagecreatefrompng( $file ) ) {
// Change file extension
$file_jpg = preg_replace( '/\.[^\.]+$/', '', $data['file'] ) . '.jpg';
if ( @ imagejpeg( $image, "$path/$file_jpg", 90 ) ) { // Save new jpg version
// Update metadata with new filename
$meta['sizes'][ $size ]['file'] = $file_jpg;
// Delete png version
@ unlink( $file );
}
@ imagedestroy( $image );
}
}
}
}
return $meta;
}
add_filter( 'wp_generate_attachment_metadata', 'wpse_183835_to_jpeg', 10, 2 );
https://codex.wordpress.org/Function_Reference/wp_generate_attachment_metadata