0

For my post I have a box for the post image and the size of the box is 214 x 73. I want the image to fit the width of the box and have it cropped at 73px height.

This is what I have so far in my functions.php file

<?php add_theme_support( 'post-thumbnails'); set_post_thumbnail_size(); 


if ( function_exists( 'add_image_size' ) ) { 
    add_image_size( 'post-thumb', 214, 73 );
    }?>

and this is what I have in my content.php file:

<?php the_post_thumbnail('post-thumb'); ?>

What am I doing right/wrong and what do I need to fix to make it work? Also is it possible to make the image centered horizontally when it crops it so that you see the middle of the image?

You can check out my theme here: http://benlevywebdesign.com/wordpress/ to see the post thumbnail box

2
  • Is there a specific post that illustrates the problem?
    – Steve
    Nov 4 '12 at 21:00
  • @Stephen the first regular post(second post from the top) Nov 4 '12 at 21:01
2

I don't think your code is the issue here. But there are a few caveats that you need to be aware of and account for.

First off always keep in mind that when using add_image_size() that WordPress does not regenerate the images size if it was uploaded before you registered the new size. The newly registered size will only apply to future uploads.

  • The new size will not be created by default for images uploaded previously. You can use the 'Regenerate Thumbnails' or similar plugin to create and update the registered sizes.
  • Images uploaded after you've registered the new size will automatically have the correct image sizes generated.

Unless you are setting the 'crop' option on the add_image_size() to TRUE you're actually scaling the image to fit within the boundaries of the dimensions. So uploading an image that falls outside of either boundary (height or width) will result in scaled down version of your upload. My understanding is that the scaling is proportional, so in your case if you uploaded an image that 428 px w x 73 px high the resulting image would be 214 px w x 36 px high.

This can get pretty hairy when providing custom sizes to an end-user.

5
  • I always think it's going to be a quick post, but by the time I edit and clarify someone else has already answered! :)
    – Steve
    Nov 4 '12 at 21:39
  • @ChipBennett I wish I could choose both answers as the correct ones but I cant Nov 4 '12 at 21:55
  • @Stephen would you like to help some more and help me solve some other issues and questions I have? Join me in the chat room I created chat.stackexchange.com/rooms/6311/… Nov 4 '12 at 21:58
  • @benlevywebdesign no worries. I think Stephen should get the exp; that's why I up-voted him. :) Nov 4 '12 at 21:59
  • @ChipBennett would you like to help some more and help me solve some other issues and questions I have? Join me in the chat room I created chat.stackexchange.com/rooms/6311/… Nov 4 '12 at 22:04
1

Two things:

  1. Be sure to set $crop to true (hard-crop) in your add_image_size() call. It defaults to false (box-crop):

    add_image_size( 'post-thumb', 214, 73, true );
    
  2. If images have already been added to the post, you'll need to regenerate the intermediate images, e.g. via the Regenerate Thumbnails Plugin.

Edit

Also: you can remove the set_post_thumbnail_size() call, since you're not passing any parameters to it.

0
-3

You can use TimThumb script to crop image with PHP to fit it with your space. It's a great script to crop images automatically with PHP. Many premium themes and plugins use it to generate Post Featured images and others.

Anyway, Use TimThumb at your own risk as it may cause security flaws.

1
  • 1
    There's no need to use TimThumb for this use-case. Using core-supported features is always preferable. Nov 4 '12 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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