I managed to find a way to achieve what I needed, which actually is, an update to the attachment meta, to include the new custom sizes.
For this, I used the wp_get_attachment_metadata
and wp_update_attachment_metadata
.
A basic code skeleton for this is like below:
// Get existing image metadata
$metadata = wp_get_attachment_metadata($imageId);
// Get the path of the original image file to initiate a new WP_Image_Editor later on
$path = wp_get_original_image_path($imageId);
// Get the WP_Image_Editor object
$wp_editor = wp_get_image_editor($path, array());
// Resize the image
$result = $wp_editor->resize($size['max_w'], $size['max_h'], $crop);
if(!is_wp_error($result)) {
// Create a reference to the new file
$new_file = $wp_editor->generate_filename();
// Save the new file
$wp_editor->save($new_file);
// Get pathinfo of the file
$fileinfo = pathinfo($new_file);
// Add the new size info to the metadata
$metadata['sizes']['size_name'] = [];
$metadata['sizes']['size_name']['file'] = $fileinfo['basename'];
$metadata['sizes']['size_name']['width'] = $size['max_w'];
$metadata['sizes']['size_name']['height'] = $size['max_h'];
$metadata['sizes']['size_name']['mime-type'] = mime_content_type($new_file);
$metadata['sizes']['size_name']['filesize'] = filesize($new_file);
// Update the metadata
wp_update_attachment_metadata( $banner_image_id, $metadata );
}