function _simplecrop_define_initial_crop_area in SimpleCrop 7
Calculates initial area of crop selection.
Parameters
$width: Width of displayed image.
$height: Height of displayed image.
$settings: Field instance settings for initial crop area.
Return value
array|bool Initial area or FALSE if no initial area.
1 call to _simplecrop_define_initial_crop_area()
- simplecrop_field_widget_process in includes/
simplecrop.field.inc - Element #process callback for the image_image field type.
File
- includes/
simplecrop.field.inc, line 703 - Contains information about field widget and related functions.
Code
function _simplecrop_define_initial_crop_area($width, $height, $settings) {
// If crop area should be as big as possible.
if ($settings['initial_area'] == SIMPLECROP_CROP_AREA_MAXIMIZE) {
// If we don't have maximum area defined in settings - then use image
// resolution as initial area.
if (empty($settings['max_area']['width']) || empty($settings['max_area']['height'])) {
$area_width = $width;
$area_height = $height;
}
else {
$area_width = $settings['max_area']['width'] < $width ? $settings['max_area']['width'] : $width;
$area_height = $settings['max_area']['height'] < $height ? $settings['max_area']['height'] : $height;
}
}
elseif ($settings['initial_area'] == SIMPLECROP_CROP_AREA_MINIMIZE) {
// If we don't have minimal area defined in settings - then no initial area.
if (empty($settings['min_area']['width']) || empty($settings['min_area']['height'])) {
return FALSE;
}
else {
$area_width = $settings['min_area']['width'] < $width ? $settings['min_area']['width'] : $width;
$area_height = $settings['min_area']['height'] < $height ? $settings['min_area']['height'] : $height;
}
}
else {
return FALSE;
}
// Now we have initial area width and height, but we need also to
// match it to selected aspect ratio.
// @TODO: possible issue when new height or width is less/more than required.
if (!empty($settings['ratio']['width']) && !empty($settings['ratio']['height'])) {
$aspect_ratio = $settings['ratio']['width'] / $settings['ratio']['height'];
$image_ratio = $area_width / $area_height;
// Width is too big.
if ($aspect_ratio < $image_ratio) {
$area_width = round($area_width * $aspect_ratio / $image_ratio);
}
elseif ($aspect_ratio > $image_ratio) {
$area_height = round($area_height * $image_ratio / $aspect_ratio);
}
}
// Now we need to calculate an initial position of initial area.
$x = ($width - $area_width) / 2;
$x2 = $x + $area_width;
$y = ($height - $area_height) / 2;
$y2 = $y + $area_height;
return array(
'x' => $x,
'y' => $y,
'x2' => $x2,
'y2' => $y2,
);
}