[Support request] Using cropped custom thumbnail sizes

Home Forums Support [Support request] Using cropped custom thumbnail sizes

Home Forums Support Using cropped custom thumbnail sizes

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1479165
    Simon

    Hi Tom and team

    I’m going slightly crazy here, and think I have read every helpful response on this forum, but can’t find one to put me right on this…

    I’ve taken over a site, and starting completely afresh with GeneratePress beta and the latest GenerateBlocks. I have a lot of images that need to be used, in various sizes and ratios. Plus people will post images in various sizes and ratios in future.

    So for the photos in posts/pages, and for the featured images, I want to create cropped versions at ratios 5:6 (for staff photos) and 16:9 (for featured images in posts), so things can line up and look coherent. (That’s in addition to the usual uncropped thumb, medium, medium_large and large.)

    I’m using the code below in ‘Code Snippets’ to create the new sizes and bring them into the block editor. But when I ‘Regenerate Thumbnails’ most of the cropped sizes appear with a red X, as if they have failed, but show the new filename as if it has worked – see below.

    When I try to use those images in the block editor, either the sizes don’t show up as an option, or if they do, the photos block offers the file name with the right size, but creates an error.

    What am I missing or doing wrongly? Surely this should work…?

    These all have a red X to the left, and won’t work.

    person20: 200×240 pixels (cropped to fit) young-family-25-15-0865-200x240.jpg
    person40: 400×480 pixels (cropped to fit) young-family-25-15-0865-400x480.jpg
    small169: 480×270 pixels (cropped to fit) young-family-25-15-0865-480x270.jpg
    med169: 640×360 pixels (cropped to fit) young-family-25-15-0865-640x360.jpg
    medlarge169: 960×540 pixels (cropped to fit) young-family-25-15-0865-960x540.jpg

    The code I am using in Code Snippets is below.

    Related small question: I have also tried and failed to remove two sizes that seem to be there by default, but which I do not need. They are listed with the names: “1536×1536” and “2048×2048”. Please suggest how to lose them.

    Hope you can help on these 2 things.
    Thank you!
    Simon

    Code Snippets code:

    // created using GP responses and https://developer.wordpress.org/reference/functions/add_image_size/
    
    update_option( 'medium_large_size_w', 720 );
    update_option( 'medium_large_size_h', 720 );
    update_option( 'medium_large_crop', 0 );
    
    add_action('after_setup_theme', 'tu_add_image_sizes');
    function tu_add_image_sizes() {
        add_image_size( 'person20', 200, 240, true ); 
        add_image_size( 'person40', 400, 480, true ); 
    	
        add_image_size( 'small169', 480, 270, true );
        add_image_size( 'med169', 640, 360, true );
        add_image_size( 'medlarge169', 960, 540, true );
        add_image_size( 'large169', 1280, 720, true );
    
        add_image_size( 'smallish', 480, 480, false);
    }
    
    // and from: https://premium.wpmudev.org/blog/wordpress-image-sizes/
    
    add_filter( 'image_size_names_choose','aimh_custom_image_sizes' );
    
    function aimh_custom_image_sizes( $sizes ) {
    return array_merge( $sizes, array(
    
    //Add your custom sizes here
    'person40' => __( 'Person photo' ),
    'large169' => __( '16:9 ratio' ),
    ) );
    }
    #1479201
    Elvin
    Staff
    Customer Support

    Hi,

    When I try to use those images in the block editor, either the sizes don’t show up as an option, or if they do, the photos block offers the file name with the right size, but creates an error.

    Can you provide a screenshot of what error your getting? By the way, it makes sense that some sizes don’t show up as they are not added in your aimh_custom_image_sizes() function. Make sure to add all of them in you array if you want them to appear on the block editor options.

    Related small question: I have also tried and failed to remove two sizes that seem to be there by default, but which I do not need. They are listed with the names: “1536×1536” and “2048×2048”. Please suggest how to lose them.

    Here’s an answer about removing registered images. – https://generatepress.com/forums/topic/why-wordpress-is-generating-additional-image-copies/#post-1453270

    #1486477
    Simon

    Hi Elvin

    Thank you for the response.

    The unset of unwanted large sizes is still unsuccessful. They are still listed by Regenerate Thumbnails. I tested the code by removing another default size and it worked, so it seems perhaps to be not recognising ‘1536×1536’ as a name? Is that possible?

    Suggestions appreciated. Thank you. Code below.

    The added custom sizes now seem to be working. Code below in case it’s useful to others.

    // disable generated image sizes - not working
    function shapeSpace_disable_image_sizes($sizes) {
    	
    	unset($sizes['1536x1536']);    // disable 2x medium-large size
    	unset($sizes['2048x2048']);    // disable 2x large size
    	return $sizes;
    }
    add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
    
    // add custom image sizes - now working
    // Media settings - large currently set at 1280x720  medlarge at 960x960  med at 640x640  thumb at 240x240, all uncropped.
    
    add_action('after_setup_theme', 'tu_add_image_sizes');
    function tu_add_image_sizes() {
        add_image_size( 'smallish', 480, 480, false);
    	
        add_image_size( 'person20', 200, 240, true ); 
        add_image_size( 'persona40', 400, 480, true ); 
    	
        add_image_size( 'small169', 480, 270, true );
        add_image_size( 'med169', 640, 360, true );
        add_image_size( 'medlarge169', 960, 540, true );
        add_image_size( 'large169', 1280, 720, true );
    }
    
    //Add custom sizes to image block editor - working
    add_filter( 'image_size_names_choose','aimh_custom_image_sizes' );
    
    function aimh_custom_image_sizes( $sizes ) {
    return array_merge( $sizes, array(
    
    	'person20' => __( 'person photo' ),
    	'med169' => __( 'med 16:9 cropped' ),
    	'large169' => __( 'lge 16:9 cropped' ),
    ) );
    }
    
    #1487543
    Elvin
    Staff
    Customer Support

    Hi,

    I’ve reviewed the code and indeed, it isn’t working well because of the code was for non-default sizes.

    But we’re removing default ones so the code should be a little different.

    You can use this instead.

    add_filter( 'intermediate_image_sizes', 'remove_default_img_sizes', 10, 1);
    
    function remove_default_img_sizes( $sizes ) {
      $targets = ['1536x1536', '2048x2048'];
    
      foreach($sizes as $size_index=>$size) {
        if(in_array($size, $targets)) {
          unset($sizes[$size_index]);
        }
      }
    
      return $sizes;
    }

    Reference: https://wordpress.stackexchange.com/a/357960

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.