Cameron
Cameron

Reputation: 28853

WordPress Post Thumbnail

I'm using the post thumbnail feature to show portfolio work, so for example <?php the_post_thumnail(); ?> the problem is I want to show a particular width of image only, which is 640 but because my pieces are website they have varying heights.

I'm using this code to create the images: add_image_size( '640', 480 ,true ); but it's specifying the height how can I ONLY specify the width??

Thanks

Ps. I know I could use CSS, but I want to actually create images at 640 wide and not use any timthumb stuff either. Thanks

Upvotes: 4

Views: 710

Answers (2)

Gipetto
Gipetto

Reputation: 1048

You can use add_image_size to define sizes that require one dimension and that will proportionally scale the other:

add_image_size('width-640', 640, 0, false);
add_image_size('height-640', 0, 640, false);

You can also achieve this with one line

add_image_size('constrain-640', 640, 640, false);

This will proportionally crop an image so that its longest side is no larger than 640px.

Upvotes: 4

shaans
shaans

Reputation: 554

You can calculate the ratio height from width. NewHeight = Height * NewWidth / Width;

Ex: OldSize(400, 300)
    NewSize(NewHeight, 640)
    NewHeight = 400*640/300
              = 853

Upvotes: 1

Related Questions