'hidden', '#default_value' => $sizes[$key]['imagecache'], ); // Hide unused elements $operation = $form['image_sizes'][$key]['operation']['#default_value']; if( ($operation != 'scale') && ($operation != 'scale_crop') ) { $form['image_sizes'][$key]['width']['#type'] = 'hidden'; $form['image_sizes'][$key]['height']['#type'] = 'hidden'; } } $form['image_sizes']['#description'] .= t('

Choose an imagecache preset to use to generate this derivative image. If using imagecache, the operation and sizes shown here will be ignored

', array('!imagecache_settings' => url('admin/build/imagecache')) ); // We switch values behind the scenes, and incidentally bypass the normal image.module validation. $form['image_sizes']['#validate'] = array('imagecache_change_image_size_settings' => array() ); } } /** * Manipulate the settings being saved ... to fool image.module * * Reads the operation, saves it in our own imagecache setting, then resets the * operation to something image.module will recognise. * It's pulling a bait and switch with the form - all just to clean up the UI. * * TODO Image.module could co-operate a bit more by extending the 'operations' * for us to add to directly. So call this a proof-of concept until a better * way. */ function imagecache_change_image_size_settings(&$form) { foreach (element_children($form) as $preset_id) { $operation = $form[$preset_id]['operation']['#value']; if (($operation != 'scale') && ($operation != 'scale_crop')) { form_set_value($form[$preset_id]['imagecache'], $operation); form_set_value($form[$preset_id]['operation'], 'scale'); if(!($form[$preset_id]['width'] || $form[$preset_id]['height'])) { // Dummy values to fudge it even more for image.module form_set_value($form[$preset_id]['width'], 100, $form_state); form_set_value($form[$preset_id]['height'], 100, $form_state); } } else { form_set_value($form[$preset_id]['imagecache'], ''); } } } /** * Implementation of hook_image_alter() * * Capture the image_build_derivatives phase of image.module * and insert our own manipulations to it any time an image is manipulated. * * This runs the imagecache builder over the input file and places it in the * output destination. */ function imagecache_image_alter($node, $destination, $sizename) { $sizes = image_get_sizes(); $size_def = $sizes[$sizename]; // Appended to the dimensions is our 'imagecache' id value. Maybe. if ($presetid = $size_def['imagecache']) { $original = file_create_path($node->images['_original']); $preset = imagecache_preset($presetid); $result = imagecache_build_derivative($preset['actions'], $original, $destination ); } }