[hack] crop custom thumbnail sizes
-
I’m pretty new to wordpress but found a limitation that I needed to fix. I’m implementing custom image sizes using add_image_size() and wanted the editors to be able to crop these thumbnails themselves. I found the excellent crop tool in the back end but it only had options for “all image sizes”, “thumbnail only” or “all except thumbnail” – no mention of my custom sizes.
I’ve hacked the code to allow this and thought I’d post it in case it helps anyone and for anyone perhaps to advise me that a) I didn’t need to do this b) I’ve done it badly/dangerously or c) I could do it better – perhaps with a plugin or something? I feel a little insecure with this hack and of course will have to reapply it whenever I update wp.
Anyhow, here’s the code:
/wp-includes/media.php ~ Line 181
adds option variables for the registered image size – to be accessed in image-edit.php/** * Registers a new image size */ function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { global $_wp_additional_image_sizes; $_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop ); add_option($name."_size_w", $width); add_option($name."_size_h", $height); add_option($name."_crop", $crop); }
/wp-admin/includes/image-edit.php ~ Line 179
adds the options to the list of radio buttons in the interface<?php function myfunction($value,$key,$pid) { echo '<label class="imgedit-label">'; echo '<input type="radio" name="imgedit-target-'.$pid.'" value="'.$key.'" /> '.$key.'</label>'; // preview of image size echo wp_get_attachment_image($pid, $key); } array_walk($meta['sizes'],"myfunction", $post_id); ?>
/wp-admin/includes/image-edit.php ~ Line 613
use the $meta[‘sizes’] array instead of hardcoded defaults and add catch all for the custom sizes to be added singly to the $sizes array that will be used to apply the manipulation.if ( $success && ('nothumb' == $target || 'all' == $target) ) { $sizes = apply_filters( 'intermediate_image_sizes', array_keys($meta['sizes']) ); if ( 'nothumb' == $target ) $sizes = array_diff( $sizes, array('thumbnail') ); } $return->fw = $meta['width']; $return->fh = $meta['height']; } elseif ( 'thumbnail' == $target ) { $sizes = array( 'thumbnail' ); $success = $delete = $nocrop = true; } else { $sizes = array( $target ); $success = $delete = $nocrop = true; }
- The topic ‘[hack] crop custom thumbnail sizes’ is closed to new replies.