Customize cropping of thumbnails
-
I have been looking for a way to custom-crop thumbnails so that they are more like a ‘detail’ of the image, rather than a shrunken miniature of the image. I didn’t find anything that worked for me in the existing plugins library (please point me to a plug-in if you know of one!), so I resorted to modifying the core code in media.php. Since I see this as a sub-optimal solution, I thought I would post my approach here. If it really is the best approach, then others can use it also. If not, then the responses to this thread should provide a better solution…
Thankfully, it’s only necessary to modify one line of code to customize the ‘scaling’ factor of the thumbnail resizing function in media.php. To do this…
Find the following code in media.php:
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
This is telling the resize function to create a size ratio that will shrink the original image so that it just fits into the thumbnail in one dimension and is cropped in the other dimension.
In my case, I have some border areas on my images that I don’t want showing up in my thumbnails, so I want to exclude those borders, which are always less than 25% of the total image width or height on left/right or top/bottom. So I replaced the above code with the following:
$size_ratio = max($new_w / ($orig_w / 2), $new_h / ($orig_h / 2));
This tells resize function, in effect, “Create a thumbnail that excludes at least 25% of the image on all sides (so that, at most, the central 50% of the image is shown in the thumbnail.”
To trim off less (say, a 10% border, which would mean focusing on a thumbnail on the central 80x160px portion of a 100x200px image), then just change the code as follows:
$size_ratio = max($new_w / ($orig_w * 0.8), $new_h / ($orig_h * 0.8));
Extending this logic, to trim off a set pixel border from the amount of the image used to create the thumbnail (say, a 20 pixel border all around, assuming that you are confident that all images can accomodate this), then you could use the following code:
$size_ratio = max($new_w / ($orig_w – 20), $new_h / ($orig_h – 20));
If you wanted to create a full-resolution detail (where the thumbnail just shows a thumbnail-sized ‘chunk’ of the center of the original image at full resolution, then you could use the following code:
$size_ratio = 1;
I implemented this and ran the Regenerate Thumbnails plugin, and it worked perfectly for my purposes. However, as noted above, this is a suboptimal solution, since it requires changes in the core code. If anyone has ideas about how to achieve this using a plug-in or hook, I would welcome a better method.Note: All of the above methods leave the rest of the resize function intact, so the thumbnail will still sample from the center of the image using its calculation of the
$s_x
and$s_y
variables. Others have posted elsewhere on how to modify these variables to shift the location of the thumbnail sample from the center to the top or bottom of the image. I have not looked at combining these methods. It would theoretically be possible to do so, but the odds of breaking the code would be higher…
- The topic ‘Customize cropping of thumbnails’ is closed to new replies.