diff --git a/manualcrop.admin.inc b/manualcrop.admin.inc index db13345..c945fa2 100644 --- a/manualcrop.admin.inc +++ b/manualcrop.admin.inc @@ -618,6 +618,13 @@ function manualcrop_field_widget_settings_form($widget_type, $settings) { '#default_value' => $settings['manualcrop_default_crop_area'], ); + $form['manualcrop']['manualcrop_automatic_default_crop'] = array( + '#type' => 'checkbox', + '#title' => t('Automatically crop to default crop area.'), + '#description' => t('Automatically create a default crop area when uploading an image. Note: Only manualcrop_crop_and_scale is supported and maximize selection is implicitly assumed.'), + '#default_value' => $settings['manualcrop_automatic_default_crop'], + ); + $form['manualcrop']['manualcrop_maximize_default_crop_area'] = array( '#type' => 'checkbox', '#title' => t('Maximize default crop area'), diff --git a/manualcrop.helpers.inc b/manualcrop.helpers.inc index 4c1e185..1f2c2a3 100644 --- a/manualcrop.helpers.inc +++ b/manualcrop.helpers.inc @@ -81,6 +81,8 @@ function manualcrop_default_widget_settings() { 'manualcrop_instant_crop' => FALSE, // Show a default crop area when opening an uncropped image? 'manualcrop_default_crop_area' => TRUE, + // Automatically crop an uploaded image? + 'manualcrop_automatic_default_crop' => FALSE, // Maximize the default crop area? 'manualcrop_maximize_default_crop_area' => FALSE, // Exclude or include the selected styles? diff --git a/manualcrop.module b/manualcrop.module index dd894c7..8ababeb 100644 --- a/manualcrop.module +++ b/manualcrop.module @@ -388,6 +388,69 @@ function manualcrop_croptool_prepare_form(&$form, &$form_state, $js_identifier, } /** + * Return the default crop size for images. + * + * This prevents a user to need to: + * "Click 'upload', click 'crop', click 'save'" + * to prevent a validation error. + * + * @param $file + * The file object that contains the image. + * @param $image_style + * The name of the manualcrop* image style to use. + * @param $settings + * Widget settings. + * + * @return + * Returns the default crop size or '' if not appliable. + */ +function manualcrop_get_default_crop_size($file, $image_style, $settings) { + $image = image_get_info($file->uri); + + // Generate image style settings. + $styles = manualcrop_styles_with_crop(); + if (!isset($styles[$image_style])) { + return ''; + } + $name = $styles[$image_style]['effect']['name']; + $style_data = $styles[$image_style]['effect']['data']; + + // @todo: Maximise default crop selection is implicit on here. + if (empty($settings['manualcrop_automatic_default_crop'])) { + return ''; + } + + // @todo: Support manualcrop_crop as well + if ($name != 'manualcrop_crop_and_scale') { + return ''; + } + + // This code is copied mainly from the javascript code + // and needs to be synchronized with ManualCrop.maximizeSelection. + + $x = 0; + $y = 0; + $width = $image['width']; + $height = $image['height']; + + $ratioWidth = $style_data['width']; + $ratioHeight = $style_data['height']; + + if (($ratioWidth / $ratioHeight) > ($image['width'] / $image['height'])) { + // Crop from top and bottom. + $height = floor(($width / $ratioWidth) * $ratioHeight); + $y = floor(($image['height'] - $height) / 2); + } + else { + // Crop from sides. + $width = floor(($image['height'] / $ratioHeight) * $ratioWidth); + $x = floor(($image['width'] - $width) / 2); + } + + return "$x|$y|$width|$height"; +} + +/** * Add a croptool to the form element. This extends the FAPI widget or simply adds * a new form item to enable cropping in a regular form. * @@ -553,7 +616,7 @@ function manualcrop_croptool_process(&$element, &$form_state, $js_identifier, $f foreach ($styles as $style_name => $info) { $container['manualcrop_selections'][$style_name] = array( '#type' => 'hidden', - '#default_value' => (isset($item['manualcrop_selections'][$style_name]) ? $item['manualcrop_selections'][$style_name] : ''), + '#default_value' => (isset($item['manualcrop_selections'][$style_name]) ? $item['manualcrop_selections'][$style_name] : manualcrop_get_default_crop_size($file, $style_name, $settings)), '#attributes' => array( 'id' => 'manualcrop-area-' . $file->fid . '-' . $style_name, 'class' => array('manualcrop-cropdata'),