View source
<?php
require_once dirname(__FILE__) . '/focal_point.effects.inc';
define('FOCAL_POINT_DEFAULT', '50,50');
function focal_point_menu() {
$items = array();
$items['admin/config/media/focal_point'] = array(
'type' => MENU_NORMAL_ITEM,
'title' => 'Focal Point',
'description' => 'Configure how the Focal Point module behaves and take a test drive.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'focal_point_configuration_form',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'focal_point.admin.inc',
);
$items['focal_point/test-drive'] = array(
'type' => MENU_CALLBACK,
'title' => 'Focal Point Test Drive',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'focal_point_test_drive_form',
),
'access arguments' => array(
'preview focal point',
),
'file' => 'focal_point.admin.inc',
);
$items['focal_point/preview/%/%'] = array(
'type' => MENU_CALLBACK,
'title' => 'Image Preview',
'page callback' => 'focal_point_preview_page',
'page arguments' => array(
2,
3,
),
'access arguments' => array(
'preview focal point',
),
'file' => 'focal_point.admin.inc',
);
return $items;
}
function focal_point_permission() {
return array(
'preview focal point' => array(
'title' => t('Preview focal point results'),
),
);
}
function focal_point_theme($existing, $type, $theme, $path) {
return array(
'focal_point_image_style' => array(
'variables' => array(
'focal_point' => NULL,
'style_name' => NULL,
'path' => NULL,
'width' => NULL,
'height' => NULL,
'alt' => '',
'title' => NULL,
'attributes' => array(),
),
'file' => 'focal_point.theme.inc',
),
'focal_point_image_resize_summary' => array(
'variables' => array(
'data' => NULL,
),
'file' => 'focal_point.theme.inc',
),
'focal_point_image_crop_summary' => array(
'variables' => array(
'data' => NULL,
),
'file' => 'focal_point.theme.inc',
),
);
}
function focal_point_admin_paths() {
$paths = array(
'focal_point/test-drive' => TRUE,
'focal_point/preview/*/*' => TRUE,
);
return $paths;
}
function focal_point_image_default_styles() {
$styles = array();
$styles['focal_point_preview'] = array(
'label' => 'Focal Point Preview',
'effects' => array(
array(
'name' => 'image_scale',
'data' => array(
'width' => 250,
'height' => NULL,
'upscale' => 1,
),
'weight' => 0,
),
),
);
return $styles;
}
function focal_point_file_load($files) {
$focal_points = focal_point_get_multiple(array_keys($files));
foreach ($files as &$file) {
$file->focal_point = isset($focal_points[$file->fid]) ? $focal_points[$file->fid] : '';
if ($file->fid == variable_get('focal_point_test_drive_image', NULL)) {
$file->focal_point = variable_get('focal_point_test_drive_focal_point', FOCAL_POINT_DEFAULT);
}
}
}
function focal_point_form_file_entity_add_upload_alter(&$form, &$form_state) {
if (isset($form_state['step']) && $form_state['step'] == 4) {
$fid = isset($form_state['values']['upload']) ? $form_state['values']['upload'] : $form_state['storage']['upload'];
$file = file_load($fid);
_focal_point_form_append_focal_point_preview($form, $file);
}
}
function focal_point_form_file_entity_edit_alter(&$form, &$form_state) {
if (!isset($form_state['file'])) {
return;
}
_focal_point_form_append_focal_point_preview($form, $form_state['file']);
}
function _focal_point_form_append_focal_point_preview(&$form, $file) {
if (_focal_point_supported_field_type('media') && is_object($file) && _focal_point_supported_file($file)) {
$default_value = !empty($file->focal_point) ? $file->focal_point : _focal_point_guess_default($file->fid);
$focal_point_id = drupal_html_id('focal-point-media');
$form['focal_point'] = array(
'#type' => 'container',
'#prefix' => '<label>' . t('Focal Point') . '</label>',
'indicator' => _focal_point_indicator($focal_point_id),
'thumbnail' => array(
'#type' => 'markup',
'#theme' => 'image_style',
'#style_name' => 'focal_point_preview',
'#path' => $file->uri,
'#alt' => isset($file->alt) ? $file->alt : '',
'#title' => isset($file->title) ? $file->title : '',
),
'preview_link' => _focal_point_preview_link($file->fid, $focal_point_id, $file->focal_point),
'focal_point' => _focal_point_field($focal_point_id, $default_value),
'focal_point_help' => _focal_point_help(),
);
$form['preview']['#access'] = FALSE;
$form['#validate'][] = 'focal_point_form_validate';
}
}
function focal_point_field_widget_form_alter(&$element, &$form_state, $context) {
$widget_type = $context['instance']['widget']['type'];
if (_focal_point_supported_widget_type($widget_type)) {
foreach (element_children($element) as $delta) {
$element[$delta]['#attributes']['class'][] = 'focal_point';
$element[$delta]['#process'][] = 'focal_point_widget_process';
$element[$delta]['#focal_point_id'] = drupal_html_id('focal-point-' . $context['instance']['field_name'] . '-' . $delta);
}
}
}
function focal_point_widget_process($element, &$form_state, $form) {
$access = (bool) (!empty($element['#file']));
if ($access) {
$default_value = !empty($element['#default_value']['focal_point']) ? $element['#default_value']['focal_point'] : _focal_point_guess_default($element['#file']->fid);
}
else {
$default_value = !empty($element['#default_value']['focal_point']) ? $element['#default_value']['focal_point'] : '';
}
$element['focal_point'] = _focal_point_field($element['#focal_point_id'], $default_value, $access);
return $element;
}
function focal_point_preprocess_image_widget(&$variables) {
$element =& $variables['element'];
if (isset($element['preview']) && isset($element['#focal_point_id'])) {
unset($element['preview']['#weight']);
$preview = array(
'indicator' => _focal_point_indicator($element['#focal_point_id']),
'thumbnail' => $element['preview'],
'preview_link' => _focal_point_preview_link($element['#file']->fid, $element['#focal_point_id'], isset($element['focal_point']['#default_value']) ? $element['focal_point']['#default_value'] : FOCAL_POINT_DEFAULT),
);
$element['preview'] = $preview;
$element['focal_point_help'] = _focal_point_help();
}
}
function focal_point_form_validate($form, &$form_state) {
$focal_point = isset($form_state['values']['focal_point']) ? $form_state['values']['focal_point'] : '';
if (!focal_point_validate($focal_point)) {
form_set_error('focal_point', t('Focal points should be in the format ##,## without spaces where the numbers are between 1 and 100.'));
}
}
function focal_point_element_form_validate($element, &$form_state, $form) {
$focal_point = isset($element['#value']) ? $element['#value'] : '';
if (!focal_point_validate($focal_point)) {
form_error($element, t('Focal points should be in the format ##,## without spaces where the numbers are between 1 and 100.'));
}
}
function focal_point_field_attach_presave($entity_type, $entity) {
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
foreach (field_info_instances($entity_type, $bundle) as $instance) {
$field_name = $instance['field_name'];
if (_focal_point_supported_widget_type($instance['widget']['type'])) {
$images = array();
try {
$wrapper = entity_metadata_wrapper($entity_type, $entity);
$images = $wrapper->{$field_name}
->value();
} catch (EntityMetadataWrapperException $e) {
watchdog_exception('focal_point', $e);
}
if (!empty($images) && is_array($images)) {
_focal_point_images_save($images);
}
}
}
}
function focal_point_file_presave($file) {
if (isset($file->fid) && isset($file->focal_point)) {
focal_point_save($file->fid, $file->focal_point);
}
}
function focal_point_file_delete($file) {
focal_point_delete($file->fid);
}
function focal_point_get_default_method_info() {
$info =& drupal_static(__FUNCTION__);
if (!isset($info)) {
$info = module_invoke_all('focal_point_default_method_info');
drupal_alter('focal_point_default_method_info', $info);
}
return $info;
}
function focal_point_save($fid, $focal_point) {
$existing_focal_point = focal_point_get($fid);
$record = array(
'fid' => $fid,
'focal_point' => $focal_point,
);
if ($existing_focal_point == $focal_point) {
return;
}
if ($existing_focal_point) {
if (!empty($focal_point)) {
_focal_point_save($record, 'fid');
}
else {
focal_point_delete($fid);
}
}
elseif (!empty($focal_point)) {
_focal_point_save($record);
}
$cached =& drupal_static('focal_point_get_multiple');
unset($cached[$fid]);
entity_get_controller('file')
->resetCache(array(
$fid,
));
}
function _focal_point_save($record, $primary_keys = array()) {
drupal_alter('focal_point_pre_save', $focal_point, $fid, $existing_focal_point);
drupal_write_record('focal_point', $record, $primary_keys);
_focal_point_flush($record['fid']);
module_invoke_all('focal_point_save', $record);
}
function focal_point_validate($focal_point) {
if (empty($focal_point) || preg_match('/^(100|[0-9]{1,2})(,)(100|[0-9]{1,2})$/', $focal_point)) {
return TRUE;
}
else {
return FALSE;
}
}
function focal_point_get_multiple($fids) {
$focal_points =& drupal_static(__FUNCTION__, array());
$missing = array_diff($fids, array_keys($focal_points));
if ($missing) {
$result = db_query('SELECT fid, focal_point FROM {focal_point} WHERE fid IN (:fids)', array(
':fids' => $missing,
))
->fetchAllKeyed();
$focal_points += $result;
}
return array_intersect_key($focal_points, drupal_map_assoc($fids));
}
function focal_point_get($fid) {
$result = focal_point_get_multiple(array(
$fid,
));
return isset($result[$fid]) ? $result[$fid] : '';
}
function focal_point_parse($focal_point = '') {
if (empty($focal_point)) {
$focal_point = FOCAL_POINT_DEFAULT;
}
return array_combine(array(
'x-offset',
'y-offset',
), explode(',', $focal_point));
}
function focal_point_delete($fid) {
_focal_point_flush($fid);
db_delete('focal_point')
->condition('fid', $fid)
->execute();
module_invoke_all('focal_point_delete', $fid);
}
function _focal_point_guess_default($fid) {
$method = variable_get('focal_point_default_method', '');
if ($method && ($file = file_load($fid)) && ($image = image_load($file->uri))) {
$methods = focal_point_get_default_method_info();
if (isset($methods[$method])) {
if ($coords = call_user_func($methods[$method]['callback'], $image)) {
list($x, $y) = $coords;
$x = intval($x / $image->info['width'] * 100);
$y = intval($y / $image->info['height'] * 100);
return $x . ',' . $y;
}
}
}
return FOCAL_POINT_DEFAULT;
}
function _focal_point_field($indicator_id = 'focal-point', $default_value = FOCAL_POINT_DEFAULT, $access = TRUE) {
return array(
'#type' => 'textfield',
'#title' => 'Focal Point',
'#description' => t('Specify the focus of this image in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75'),
'#default_value' => $default_value,
'#access' => $access,
'#element_validate' => array(
'focal_point_element_form_validate',
),
'#attributes' => array(
'class' => array(
'focal-point',
),
'data-focal-point-id' => $indicator_id,
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'focal_point') . '/js/focal_point.js',
),
'css' => array(
drupal_get_path('module', 'focal_point') . '/css/focal_point.css',
),
),
);
}
function _focal_point_help() {
return array(
'#prefix' => '<div class="focal-point-help">',
'#markup' => t('Click and drag the crosshair to target the most important portion of the image. This portion of the image will never be cropped.'),
'#suffix' => '</div>',
);
}
function _focal_point_images_save($images) {
$images = isset($images['fid']) ? array(
$images,
) : $images;
foreach ($images as $image) {
if (isset($image['focal_point'])) {
focal_point_save($image['fid'], $image['focal_point']);
}
}
}
function _focal_point_supported_widget_type($type) {
$supported = FALSE;
if (_focal_point_supported_field_type('image')) {
$supported_widget_types = array(
'image_image',
'image_miw',
);
drupal_alter('focal_point_supported_widget_types', $supported_widget_types);
if (in_array($type, $supported_widget_types)) {
$supported = TRUE;
}
}
return $supported;
}
function _focal_point_supported_field_type($type) {
$enabled_for = variable_get('focal_point_enabled_for', array(
'image',
'media',
));
return in_array($type, $enabled_for, TRUE);
}
function _focal_point_supported_file($file) {
if (is_int($file)) {
$file = file_load($file);
}
$supported_file_types = array(
'image',
);
drupal_alter('focal_point_supported_file_types', $supported_file_types);
return in_array($file->type, $supported_file_types);
}
function _focal_point_flush($fid) {
if ($file = file_load($fid)) {
image_path_flush($file->uri);
}
}
function _focal_point_indicator($indicator_id = 'focal-point') {
$indicator = array(
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'focal-point-indicator',
),
'id' => $indicator_id,
),
'#markup' => '',
'#attached' => array(
'library' => array(
array(
'system',
'ui.draggable',
),
),
),
);
return $indicator;
}
function _focal_point_get_image_styles() {
module_load_include('inc', 'focal_point', 'focal_point.effects');
$focal_point_effects = array_keys(focal_point_image_effect_info());
$focal_point_styles = array();
$styles = image_styles();
foreach ($styles as $isid => $style) {
foreach ($style['effects'] as $effect) {
$focal_point_style = in_array($effect['name'], $focal_point_effects, TRUE);
if ($focal_point_style) {
$focal_point_styles[$isid] = $style;
break;
}
}
}
return $focal_point_styles;
}
function _focal_point_preview($fid, $focal_point, $show_original = TRUE) {
$file = is_numeric($fid) ? file_load($fid) : NULL;
$output = array();
$output['focal_point_examples'] = array(
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'focal_point_examples',
),
),
);
if ($file) {
if ($show_original) {
$output['focal_point_examples']['original'] = array(
'#type' => 'markup',
'#prefix' => '<h2>' . t('Original Image') . '</h2>',
'#theme' => 'image',
'#path' => $file->uri,
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'focal-point-preview--original',
),
),
);
}
$styles = _focal_point_get_image_styles();
if (!empty($styles)) {
foreach ($styles as $isid => $style) {
if (module_exists('borealis') && strpos($style['name'], 'borealis') === 0) {
continue;
}
$label = isset($style['label']) ? $style['label'] : $style['name'];
$output['focal_point_examples'][$isid] = array(
'#type' => 'markup',
'#prefix' => '<h2>' . $label . '</h2>',
'#theme' => 'focal_point_image_style',
'#style_name' => $style['name'],
'#path' => $file->uri,
'#focal_point' => $focal_point,
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'focal-point-preview--' . $style['name'],
),
),
);
}
}
else {
$output['focal_point_examples']['#markup'] = '<div class="messages warning">' . t('There are no styles defined that use a focal point effect. To see how this module will work you should <a href="@url">create a new image style</a> that uses one.', array(
'@url' => url('/admin/config/media/image-styles'),
)) . '</div>';
}
}
return $output;
}
function _focal_point_preview_link($fid, $indicator_id = 'focal-point', $focal_point = '') {
if (!user_access('preview focal point')) {
return;
}
$focal_point = empty($focal_point) ? FOCAL_POINT_DEFAULT : $focal_point;
$link_text = t('Image Preview');
$link_path = 'focal_point/preview/' . $fid . '/' . $focal_point;
return array(
'#type' => 'markup',
'#markup' => l($link_text, $link_path, array(
'attributes' => array(
'target' => '_blank',
'class' => array(
'focal-point-preview-link',
'overlay-exclude',
),
'data-focal-point-id' => $indicator_id,
),
)),
);
}
function image_focus_focal_point_default_method_info() {
$info['image_focus'] = array(
'label' => t('Image Focus Crop module'),
'callback' => 'image_focus_get_focal_point',
);
return $info;
}
function smartcrop_focal_point_default_method_info() {
$info['smartcrop'] = array(
'label' => t('Smartcrop module'),
'callback' => 'focal_point_get_smartcrop_focal_point',
);
return $info;
}
function focal_point_get_smartcrop_focal_point($image) {
module_load_include('inc', 'focal_point', 'focal_point.smartcrop');
return focal_point_smartcrop_estimation($image);
}