0

For a custom post type I have an image upload/media library field, that adds images as attachments in the post.

While saving the post I am resizing the selected image - to some new non existing images sizes with WP_Image_Editor.

This is the part of the code, when I generate the new image sizes:

$path = wp_get_original_image_path( $imageObject['ID'] );

$wp_editor = wp_get_image_editor( $path, array() );

$result = $wp_editor->resize($size['max_w'], $size['max_h'], $crop);

if( !is_wp_error( $result ) ) {

  $wp_editor->save( $wp_editor->generate_filename() );
}

So, I am saving the new sizes, next to the original and the existing ones.

I am trying to load one of those new image sizes using: wp_get_attachment_image_src($imageObject['ID'], [$size['max_w'], $size['max_h']]);

Or, I would like to be able to find the new sizes in the sizes element of the image array when I call wp_get_attachment_image($imageObject['ID']).

But, I am not able to get that new image size.

Is this possible at all? Am I missing something? (--probably yes)

Do I have to save the new images in a different way? Or do I have to add any additional steps before or after saving, in order to add the new sizes to the image?

And what about naming those new sizes?

And, how about creating a new responsive image with an srcset with these new sizes?

*I am not interested in taking the add_image_size route.

1 Answer 1

0

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 );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.