Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a series of posts, all with featured images, but I need to be able to customise the crop top right corner. In this instance, I need them to be cropped from top right, but it would be useful to also know how to position that point myself.

At present, the add_image_size() function is taking its crop from the centre of the image. Not always pretty!!

share|improve this question
add comment

3 Answers 3

up vote 10 down vote accepted

Intermediate image generation is extremely rigid. image_resize() keeps it close to code and completely lacks hooks.

Pretty much only option for this is to hook into wp_generate_attachment_metadata and overwrite WP-generated image with your own (which will need bit of a image_resize() fork).

I need this for work so I might be able to share some code later.

Ok, here is rough, but working example. Note that setting up crop in this way requires understanding of imagecopyresampled().

add_filter('wp_generate_attachment_metadata', 'custom_crop');

function custom_crop($metadata) {

    $uploads = wp_upload_dir();
    $file = path_join( $uploads['basedir'], $metadata['file'] ); // original image file
    list( $year, $month ) = explode( '/', $metadata['file'] );
    $target = path_join( $uploads['basedir'], "{$year}/{$month}/".$metadata['sizes']['medium']['file'] ); // intermediate size file
    $image = imagecreatefromjpeg($file); // original image resource
    $image_target = wp_imagecreatetruecolor( 44, 44 ); // blank image to fill
    imagecopyresampled($image_target, $image, 0, 0, 25, 15, 44, 44, 170, 170); // crop original
    imagejpeg($image_target, $target, apply_filters( 'jpeg_quality', 90, 'image_resize' )); // write cropped to file

    return $metadata;
}
share|improve this answer
1  
sounds a lot like messing with the core!! –  Mild Fuzz Jun 24 '11 at 9:35
4  
No, messing with the core would be changing the image_resize function. Rarst was making the point that you'd have to hook into the resizing process, but create the image sizes yourself manually. –  TheDeadMedic Jun 24 '11 at 10:02
    
May I ask if this still works? I just implemented the hook into my functions.php file and I have add_image_size() functions set-up, but the cropped images still get cropped form the center out. –  cr0z3r Dec 9 '11 at 22:40
    
@cr0z3r I know of no reason why it won't work. But note that this is only rough proof-of-concept example, rather than meaningful reliable code. –  Rarst Dec 10 '11 at 13:50
    
Hm, strangely enough, it does not work on my theme - could it be because I was running locally (I highly doubt it)? I will have it up online and show it to you soon. –  cr0z3r Dec 15 '11 at 16:32
add comment

I have developed a solution to this problem that does not require hacking the core: http://bradt.ca/archives/image-crop-position-in-wordpress/

I have also submitted a patch to core: http://core.trac.wordpress.org/ticket/19393

Add yourself as a Cc on the ticket to show your support for it to be added to core.

share|improve this answer
2  
@Rarst’s solution doesn’t change core files too. ;) –  toscho Feb 19 '12 at 15:25
1  
@toscho I guess he didn't mean that the other answer changes core code. –  kaiser Feb 20 '12 at 15:16
add comment

You can use the plugin Thumbnail Crop Position to select the crop position of your thumbnails.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.