Halaman

Senin, 01 Juli 2013

[Wordpress]problem of wordpress post thumbnail images not resized correctly

Let's open the article with the following code snippet, taken from WP's(WordPress'es) function image_get_intermediate_size from the file media.php from

   if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
   $file = $data['file'];
    list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
    return compact( 'file', 'width', 'height' );
   }

Well, what's wrong with that statement? there is nothing wrong with that statement, it is just that when you have an original image size which width or height was the same as the default thumbnail size, since the autogenerated thumbnail width is always lesser than the original image, then the 'autogenerated' thumbnail size will always be returned

Consider the following example, you have your original image at the resolution of 200px * 135px.
Note that the default thumbnail size generated by WP is 150px * 135px, and then you have the following code which is intended to show the featured image for the current page:

<?php echo get_the_post_thumbnail($page->ID, array(200, 135)); ?>

You will not get your 200px * 135px as you intended, instead the code will give you a 150px * 135px thumbnail size, since the later rule from the if stament presented earlier fits to these sizes perfectly, it will gives you a cropped and scaled image of 150px * 135px

A fast fix would be specifying get_the_post_thumbnail($page->ID, array(200, 136)) which will give you a 'slightly distorted' thumbnail, another fix would be implementing a default thumbnail size via WP's function set_post_thumbnail_size on your theme's function.php file