4

how to change post thumbnail crop position in wordpress


I get many complaints about wordpress thumbnail (aka post featured image in wordpress 3) crop position lately. Many of my clients tell that they need top part of the image as thumbnail rather than the useless middle part. That’s why i dived into the core and got the solution for cropping top part of the thumbnails.

Here i will show you how to change crop behaviour of wordpress.

Cropping function is called image_resize and it is located in media.php.

Step1. Open media.php file under wp-includes folder.
Step2. Find the function named “image_resize_dimensions” (Around line 309). Unfortunately this function is not pluggable and doesn’t use any hooks so we will edit it directly. Find the lines:
[php]$s_x = floor( ($orig_w – $crop_w) / 2 );
$s_y = floor( ($orig_h – $crop_h) / 2 );[/php]
Step3. Those two variables define the start point of crop that will be used. Here is a sample image that will be cropped with those variables shown:

Step4. If you want top of part of the image as post featured image then $s_y value must remain zero. So we change that line to:
[php]$s_y = 0; //floor( ($orig_h – $crop_h) / 2 );[/php]
Step5. Save the file and upload it to wp-includes folder.

Last Step. A post image is cropped at the first time it is uploaded. To update thumbnails you need this great plugin called Regenerate Thumbnails. This plugin takes post thumbnails and crops them again using your latest media settings. After you install it click on the “Regenerate All Thumbnails” button under tools. It will automatically crop all the post thumbnails again. This will take a minute or two depending on your server and number of posts you have. After the regeneration is complete all your thumbnails will be cropped from top.

You can download the modified media.php for wordpress 2.9.2 here: media.php

4 Comments

  1. Cavamondo :

    Excellent fix … do you reckon it would be possible to extend the crop function to not only the thumbnail but to all the media upload sizes as well? Small, Medium and large etc

    • As far as i know wordpress only crop thumbnails as they are theme dependent. Some themes use square thumbnails while others use rectangular. I will look into that.
      Thanks for the advice.

  2. edubaldan :

    Mate,

    Very good post. I was looking after it for a couple of days.

    I figure out that it may be a good idea, cropping images in the middle, but sometimes we want different results, like your client's case and my client's case.
    Well, I had accepted that I couldn't change this behavior, but was sure that WP had a workaround. So, I tried to use the built-in jCrop, applying the crop just for the thumbnails, but I don't know why it doesn't save the image correctly. The result was the original image, strange isn't it?
    But if I do the same thing, but apply for all sizes, it saves the cropped image as I want. Of course, loosing the original size. =O(

    I'm a PHP developer for a long time, but I started working with WP a short time ago. I'm trying to never hack the core, but sometimes, like this time, that seams that we don't have any other good solution.

    It's funny because sometimes, WP doesn't look like that was designed to be modular. Take a look at this post – http://wpengineer.com/a-solution-for-the-wordpr... – Why the hack CSS styles are been printed with that function? Why so many functions on WP prints HTML stuff, instead of returning values?
    Maybe I have to open my mind for it, but by know, it has been very painful to work with it.

    I wish I could use Expression Engine on my projects.

    Well, thanks a lot for the solution and sorry for all that talk hehe.

    Cheers!

  3. Mel :

    Worked great. Thank you!

One Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*