WordPress.org

Ready to get started?Download WordPress

Forums

add_image_size doesn't have a crop feature (16 posts)

  1. rpophessagr
    Member
    Posted 4 years ago #

    Hi,

    I'm playing with 3.0 beta2 and was trying to understand the thumbnails better. I saw that set_post_thumbnail_size offers an option to zoom/crop the image, but adding true to add_image_size didn't have the same results.

    add_image_size( 'featured', 200, 225, true );

    Am I doing something wrong, or is it not coded to do this? And if it's not, is there are work-around (without resorting to timthumb)

    Thanks!

  2. rpophessagr
    Member
    Posted 4 years ago #

    followup, I'm looking in the wp-includes/media.php file (line 188) and it looks like

    add_image_size( 'featured', 200, 225, true );

    should work, it's just not cropping the images correctly on my site. So is it a bug or am I just setting it wrong.

    Thanks in advance!

  3. noyz319
    Member
    Posted 4 years ago #

    I'me use WP 3 rc1 and am having this exact same error. It's not doing the hard-cropping as it should.

  4. stylishbeachbum
    Member
    Posted 4 years ago #

    This occurs in 2.9.2 as well. People have posted a couple of times in the Forums about it, and many times in Blogs. But nobody seems to have come up with an explanation.

    http://wordpress.org/support/topic/391035?replies=5

    What platform are you working on?

  5. Charles Kelley
    Member
    Posted 4 years ago #

    This worked like a charm for me. The problem lies in you putting images as thumbnails that were already uploaded before you specified the image size you wanted. Any new images you upload and set should work like a charm. As for the old one, try this plugin that regenerates all your thumbnails:

    http://wordpress.org/extend/plugins/regenerate-thumbnails/

  6. Alex Mansfield
    Member
    Posted 4 years ago #

    @rpophessagr Yes, it should work. However, I'm pretty sure the cropping feature requires the php-gd library to be installed on the server. Unfortunately, installing it will not crop images that were uploaded before php-gd was installed. You will need to either manually upload the images again, or use the Regenerate Thumbnails plugin.

  7. Joe Banks
    Member
    Posted 3 years ago #

    OK, so one is able to add new image sizes via add_image_size, and you can also adjust/finetune the crop the default WP thumbnail after you've uploaded the photo.

    But how do you adjust/finetune the crop of the images added via add_image_size?

  8. Ventureweb
    Member
    Posted 3 years ago #

    Hey all. After many hours spent trying to get this to work, I was finally successful! The issue that I have found is that it appears in WP 3.0+ the add_image_size is called too late.

    In order to get it to work within my dev version for a clients new blog, I simply turned on support for the thumbs using add_theme_support( 'post-thumbnails' ); within the functions.php. Then set it within the index.php using <?php the_post_thumbnail('thumbnail'); ?>.

    After this you need to go to the Settings>Media and set the thumbnail size to the dimensions that you wish for it to have. I hope that this speeds up someone elses process because this is much to time consuming and there is a real lack of accurate documentation within the codex—quite dissappointing really.

  9. baia
    Member
    Posted 3 years ago #

    Ventureweb : I did just as you say and it doesn't work.

  10. krypton1
    Member
    Posted 3 years ago #

    Tried Ventureweb's solution, reuploaded an image. Couldn't get it to work either :(

  11. Ventureweb
    Member
    Posted 3 years ago #

    Hey guys. Not too sure what is happening with your sites but here is the code that got post thumbnails working on mine.

    This is in my functions.php file. Note the resource credit, you can visit that site for more info if this is still muddy:

    // Switching on support for post thumbnails/lead images - WordPress 2.9 onward. http://themesforge.com/wordpress/add-new-native-wordpress-post-thumbnails-to-your-theme/
      add_theme_support( 'post-thumbnails' );
      add_image_size( 'single-post-thumbnail', 560, 372 ); // Inner post image dimensions

    This is in my code where I need it. My view (list of articles):

    <?php if ( function_exists( 'add_theme_support' ) ) : ?>
      <?php if ( has_post_thumbnail() ) : ?>
        <div class="postthumb">
          <?php the_post_thumbnail('thumbnail'); ?>
        </div>
      <?php endif; ?>
    <?php endif; ?>

    This is for the single post:
    <?php the_post_thumbnail( 'single-post-thumbnail' ); ?>

    I would rather not just post all of my code snippets up on here, but I obviously didn't write the best how to in the couple of minutes that I blasted that up. This seems like the quickest way to show how I got it working. If after this and the bits I mentioned above, it still doesn't work, I unfortunately cannot help you guys. Sorry.

    Please let me know how you make out with this.

  12. MatthewRuddy
    Member
    Posted 3 years ago #

    Ventureweb, that is only a temporary solution that will help for some, but doesn't fix the issue originally posted by the threat author.

    I have encountered this feature too. I've tried re-uploading the images and using the 'Regenerate Thumbnails' plugin and neither have fixed the issue.

    Resizing the same image using 'set_post_thumbnail_size' cropped the image to the exact specified dimensions (for this example, 300 wide x 200 high). However, using add_image_size produced an image with dimensions 266 wide x 200 high, hard cropped or soft cropped (it made no difference). It seems to me as though this crop feature is indeed broken using the add_image_size function. Its a real pain for web designers that need to have multiple image sizes that are hard cropped.

    What's worse is that WordPress have now stopped allowing us to upload themes that use timthumb! Their explanation is that we should now use the_post_thumbnail, which according to the above doesn't work correctly, so at the moment I'm stuck.

  13. esmi
    Forum Moderator
    Posted 3 years ago #

    I've not had any problems using the_post_thumbnail. Just ensure that you call add_image_size early enough - using after_setup_theme for example.

  14. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    add_image_size() should indeed be called in the after_setup_theme hook. I've used the cropping before, works fine and seemingly correctly to me.

    This code works for me:

    add_action( 'after_setup_theme', 'theme_setup' );
    function theme_setup() {
      add_image_size( 'theme-custom-size', 100, 100, true );
    }

    Then later in the theme, when I want to get an image at that size:
    echo wp_get_attachment_image($ID, 'theme-custom-size');

    Actually, I'm using it more like this:
    echo previous_image_link('theme-custom-size');

    But the same basic principle applies. To get the post thumbnail (aka featured image) in your custom size, then you simply do this:
    the_post_thumbnail( 'theme-custom-size' );

    Easy. Works fine.

    I think the confusion may come from the initial post in this topic. I have no idea why the topic author used "featured". The default size name for the featured image is "post-thumbnail". To set the default image size of that image you'd use this code:
    add_image_size( 'post-thumbnail', 100, 100, true );

    By doing that, the post thumbnail would be defaulted to 100x100, cropped. The code to display it could simply be this in that case because of the default size setting being adjusted:
    the_post_thumbnail();

    Note that images are never increased in size. So if you're trying to hard crop an image to bigger than the actual image size, then it won't work.

  15. Samuel Wood (Otto)
    Tech Ninja
    Posted 3 years ago #

    BTW, note that for the image resizing to work at all, you have to have the GD libraries installed. If you're missing those, then no resizing happens.

  16. oloranya
    Member
    Posted 3 years ago #

    @Otto - thanks for posting that bit of code. It fixed my issues with image cropping.

Topic Closed

This topic has been closed to new replies.

About this Topic