WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Thumbnail cropping wrong?

[Resolved] Thumbnail cropping wrong?

  • My current template was displaying pictures in 1280×400. However it was using the full picture for this and scaled it. So 80% of the file was invisible. This was increasing each page with 250 KB.

    To correct this I have added
    add_image_size( 'featured-sdk', 1280, 400, true ); // Hard Crop Mode SDK for featured image size reduction

    The output is 1200×400 for some pictures. This is not the cropping I need.

    Basically I want it to scale from X to 1280 and then cut off the remaining part.

    To make matters worse I am calling the picture like this

    function et_nexus_featured_bg() {
    	if ( ! is_home() && ! is_category() && ! is_singular() ) return;
    
    	if ( is_singular() && ( ( ( $et_single_image = get_post_meta( get_the_ID(), '_et_single_bg_image', true ) ) && '' != $et_single_image ) || '' != get_the_post_thumbnail() ) ) {
    
    		if ( '' != $et_single_image ) {
    			$bg_image = $et_single_image;
    		} else {
    			$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' );
    			$bg_image = $image[0];
    		}
    
    		$tempImage = get_thumbnail();
    		$imageEnd = substr($tempImage['thumb'], -4);
    		$tempImage = substr($tempImage['thumb'],0, -4);
    		$tempImage .= "-1280x400";
    		$tempImage .= $imageEnd;
    
    		$bg_image = $tempImage;
    
    		printf( '<style>.post-thumbnail { background-image: url(%s); }</style>',
    			esc_url( $bg_image )
    		);
    
    		return;
    	}

    I could not get it to work so I am hardcoding to look for 1280×400 extentions. But since some pictures are 1200×400 (or different) this fails!

Viewing 5 replies - 1 through 5 (of 5 total)
  • I might be mis-interpreting your issue but are you passing in your added image size $name in? As a parameter like this:

    get_the_post_thumbnail('featured-sdk')

    Also, if you are creating a new image size for use on an existing website, you probably need to regenerate the thumbnails in order for the new image sizes to be created on the already uploaded image files. For this, I use a plug in called http://wordpress.org/plugins/regenerate-thumbnails/

    I hope that helps.

    My issue is two fold
    1) normal way to get thumbnail to display did not work
    2) cropping with “renegerate thumbnail” resulted in WRONG width in the filename

    Issue2: It appeared my original files were sometimes 1100 width. When cropped to 1280 it somehow decides to keep it at 1200.

    Issue1:

    The Codex wax keeps returning the FULL image url.

    {
    			$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'sdk-featured' );
    			$bg_image = $image[0];
    		}

    It completely ignores sdk-featured and just outputs the default image url.

    Update: I just found out I can edit. Sorry for double post!

    This function DOES return my 1280×400 image:

    if ( '' != $et_single_image ) {
    			$bg_image = $et_single_image;
    		} else {
    			$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), array(1280, 400) );
    			$bg_image = $image[0];
    		}
    No idea why "sdk-featured" does not work.But I guess this is even better because it selects a smaller image when the 1280x400 has failed. Basically solving all my issues:D.

    Glad you found a solution.

    Thanks for caring!

    (ps I threw awawy the”hack”

    $tempImage = get_thumbnail();
    		$imageEnd = substr($tempImage['thumb'], -4);
    		$tempImage = substr($tempImage['thumb'],0, -4);
    		$tempImage .= "-1280x400";
    		$tempImage .= $imageEnd;
    
    		$bg_image = $tempImage;

    😀 ).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Thumbnail cropping wrong?’ is closed to new replies.