$trueData[2]) { // Width > height $ratio = $trueData[1] / $size[0]; $new_height = round($trueData[2] / $ratio); $new_width = $size[0]; } else { // Height > width $ratio = $trueData[2] / $size[1]; $new_height = $size[1]; $new_width = round($trueData[1] / $ratio); } } // This would be the path of our resized image if the dimensions existed $imageExt = pathinfo( $imagePath, PATHINFO_EXTENSION ); $imagePath = preg_replace( '/^(.*)\.' . $imageExt . '$/', sprintf( '$1-%sx%s.%s', $new_width, $new_height, $imageExt ) , $imagePath ); $att_url = wp_get_attachment_url( $id ); // If it already exists, serve it if ( file_exists( $imagePath ) ) { return array( dirname( $att_url ) . '/' . basename( $imagePath ), $new_width, $new_height, $crop ); } // If not, resize the image... $resized = image_make_intermediate_size( get_attached_file( $id ), $size[0], $size[1], $crop ); // Get attachment meta so we can add new size $imagedata = wp_get_attachment_metadata( $id ); // Save the new size in WP so that it can also perform actions on it $imagedata['sizes'][ $size[0] . 'x' . $size[1] ] = $resized; wp_update_attachment_metadata( $id, $imagedata ); // Resize somehow failed if ( ! $resized ) { return false; } // Then serve it return array( dirname( $att_url ) . '/' . $resized['file'], $resized['width'], $resized['height'], $crop ); } return false; } add_filter( 'image_downsize', 'gambit_otf_regen_thumbs_media_downsize', 10, 3 ); }