Tuesday, December 23, 2014

[Wordpress Tips] How to scale up featured post thumbnail?


  
  WordPress is one of the most powerful CMS! In that, sometimes we want to make featured image to scale up. That moment we write some code in to the functions.php file.

 
  You can find functions.php in directory wp-content/themes/[your theme]. If you find that open it!

  If you want to add some image size, insert this code.

  
   add_image_size( '[name you choose]', [image width], [image height], [do you want to crop?] );

   ex) add_image_size( 'sliderthumb'740400true );


  Then you can use 'sliderthumb' image size!

  Do you use any thumbnail image rebuilder? Go to their configure page. You can see all image size lists.


  But, this time you can not make full size image. That generated images are weired. So let's do insert more code!


  In functions.php insert follow codes.

  function image_crop_fulldimensions($default, $orig_w, $orig_h, $new_w, $new_h, $crop){
    if ( !$crop ) return null; // let the wordpress default function handle this

    $aspect_ratio = $orig_w / $orig_h;
    $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);

    $crop_w = round($new_w / $size_ratio);
    $crop_h = round($new_h / $size_ratio);

    $s_x = floor( ($orig_w - $crop_w) / 2 );
    $s_y = floor( ($orig_h - $crop_h) / 2 );

    return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
}

add_filter('image_resize_dimensions', 'image_crop_fulldimensions', 10, 6);

After this, you can get full dimensions images!!


Let you apply this code!!!

No comments:

Post a Comment