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:

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

tep3. 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:

$s_y = 0; //floor( ($orig_h - $crop_h) / 2 );

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 calledRegenerate 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.

Watermark images using php


// this script creates a watermarked image from an image file – can be a .jpg .gif or .png file 

// where watermark.gif is a mostly transparent gif image with the watermark – goes in the same directory as this script 

// where this script is named watermark.php 

// call this script with an image tag 

// <img src=”watermark.php?path=imagepath”> where path is a relative path such as subdirectory/image.jpg 

$imagesource =  $_GET[‘path’]; 

$filetype = substr($imagesource,strlen($imagesource)-4,4); 

$filetype = strtolower($filetype); 

if($filetype == “.gif”)  $image = @imagecreatefromgif($imagesource);  

if($filetype == “.jpg”)  $image = @imagecreatefromjpeg($imagesource);  

if($filetype == “.png”)  $image = @imagecreatefrompng($imagesource);  

if (!$image) die(); 

$watermark = @imagecreatefromgif(‘watermark.gif’); 

$imagewidth = imagesx($image); 

$imageheight = imagesy($image);  

$watermarkwidth =  imagesx($watermark); 

$watermarkheight =  imagesy($watermark); 

$startwidth = (($imagewidth – $watermarkwidth)/2); 

$startheight = (($imageheight – $watermarkheight)/2); 

imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); 





The script centers the watermark, but can easily be modified to place it along any edge if you would prefer a different location.   For example removing the “/2” in the $startwidth and $startheight variable calculations will put the watermark in the lower right corner. 

You can then make the original images inaccessible if you put the following lines in a .htaccess file in the directory with the images: 

RewriteEngine On
RewriteCond %{REQUEST_URI} !error.gif$
RewriteRule .(gif|jpg|png)$ /error.gif [L] 

This will redirect any attempt to directly access an image in that directory to the error image specified.   Note that the error image is specified on two lines in the .htaccess file.   The first line allows the error image to bypass the rewrite.