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()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Make add_image_size() crop from the top. */ 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; } add_filter('wp_generate_attachment_metadata', 'custom_crop'); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.