Skip to content

Commit

Permalink
Initial port to Backdrop
Browse files Browse the repository at this point in the history
  • Loading branch information
herbdool committed Oct 17, 2018
1 parent 31a0241 commit 287caff
Show file tree
Hide file tree
Showing 11 changed files with 502 additions and 101 deletions.
339 changes: 339 additions & 0 deletions LICENSE.txt

Large diffs are not rendered by default.

86 changes: 56 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
##ABOUT
Focal Point
===========

Focal Point allows you to specify the portion of an image that is most
important. This information can be used when the image is cropped or cropped and
scaled so that you don't, for example, end up with an image that cuts off the
subject's head.

This module borrows heavily from the ImageField Focus module but it works in a
fundamentally different way. In this module the focus is defined as a single
point on the image. Among other things this helps to solve the problem of
guaranteeing the size of a cropped image as described here:
https://drupal.org/node/1889542.

Additionally, Focal Point integrates both with standard image fields as well as
media fields provided by the media module (7.x-2.x).
In this module the focus is defined as a single point on the image. Among other
things this helps to solve the problem of guaranteeing the size of a cropped image
as described here: <https://drupal.org/node/1889542>.

There is an update path provided (during installation) that will migrate
existing imagefield_focus data to focal_points.

##DEPENDENCIES
Dependencies
------------

- entity
- image
* entity
* image

##USUAGE
Installation
------------

### Setting up image fields
Install this module using the official Backdrop CMS instructions at <https://backdropcms.org/guide/modules>.

Install the module as usual. Once enabled you can specify which type of image
fields should
use focal point by visiting the configuration screen.
Setting up image fields
-----------------------

For media image fields, there is no setup involved. You cannot turn off focal
point on individual media fields. Note that focal point will not work properly
with the 1.x branch of the media module
You can specify which type of image fields should use focal point by visiting the configuration screen.

### Setting the focal point for an image
Setting the focal point for an image
------------------------------------

To set the focal point on an image, go to the content edit form (ex. the node
edit form) and upload an image. You will notice a crosshair in the middle of the
Expand All @@ -44,7 +40,9 @@ image. Done.
As a bonus, you can double-click the crosshair to see the exact coordinates (in
percentages) of the focal point.

### Cropping your image
Cropping your image
-------------------

The focal point module comes with two image effects:

1. focal point crop
Expand All @@ -54,33 +52,61 @@ Both effects will make sure that the define focal point is as close to the
center of your image as possible. It guarantees the focal point will be not be
cropped out of your image and that the image size will be the specified size.

###OTHER CONFIGURATIONS
Other configurations
--------------------

The focal point module's configuration form has only three options. You can
enable focal point on standard image fields and/or media image fields.
Additionally, you can specify what image preset to use for the preview image if
none is already provided. Typically this is only used for Media fields.
none is already provided.

Additionally, you can select what method to use when calculating the initial
focal point value on image upload. Out of the box, the smart crop module (if
it's installed) can be selected or you can write your own. See
focal_point.api.php for an example of this. __Use this option with care
however since it can require a lot of memory and image processing.__
focal_point.api.php for an example of this. **Use this option with care
however since it can require a lot of memory and image processing.**

###Updating from imagefield_focus to focal_point
Updating from imagefield_focus to focal_point
---------------------------------------------

1. Make sure you are on Publisher 7.29.0 or greater
2. **DO NOT** disable imagefield_focus
3. Enable focal_point.
- During the install process, this module will check every file entity that
* During the install process, this module will check every file entity that
has "imagefield_focus" data and convert that data to "focal_points" by using
the exact center of the focus rectangle as the focal point.
4. Convert your image styles. Any image style you are using that uses the *Focus
Crop* or *Focus Scale & Crop* effects should be changed to use the focal
point equivilents.
- Don't forget to reexport your features if necessary.
- **It is not recommended that you create new styles and delete the old ones
* Don't forget to reexport your features if necessary.
* **It is not recommended that you create new styles and delete the old ones
as this can mess with (among other things) existing views and display suite
settings.**
5. Once you have confirmed that your images are displaying as desired, it is
safe to disable imagefield_focus.

Support
-------

Submit your issue at <https://github.com/backdrop-contrib/focal_point/issues>.

License
-------

This project is GPL v2 software. See the LICENSE.txt file in this directory for complete text.

Maintainers
-----------

Seeking maintainers.

Ported to Backdrop by:

* herbdool <https://github.com/herbdool>

Credits
-------

This module is based on the Focal Point module for Drupal--starting at version 7.x-1.1--originally written and maintained by:

* bleen <https://www.drupal.org/u/bleen>
10 changes: 10 additions & 0 deletions config/focal_point.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"_config_name": "focal_point.settings",
"focal_point_default_method": "",
"focal_point_enabled_for": {
"image": "image",
"media": "media"
},
"focal_point_test_drive_focal_point": "50, 50",
"focal_point_test_drive_image": null
}
23 changes: 15 additions & 8 deletions focal_point.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
* System form for focal point configuration settings.
*/
function focal_point_configuration_form($form, &$form_state) {
$config = config('focal_point.settings');

$default_options = array();
$default_info = focal_point_get_default_method_info();
foreach ($default_info as $key => $info) {
$default_options[$key] = check_plain($info['label']);
}
$form['#config'] = 'focal_point.settings';
$form['focal_point_default_method'] = array(
'#type' => 'select',
'#title' => t('Calculate intial focal point using'),
'#descrition' => t('This can slow down your site, especially if you are bulk uploading images.'),
'#options' => array('' => t('Disabled')) + $default_options,
'#default_value' => variable_get('focal_point_default_method', ''),
'#default_value' => $config->get('focal_point_default_method'),
'#access' => !empty($default_options),
);

Expand All @@ -32,7 +35,7 @@ function focal_point_configuration_form($form, &$form_state) {
'#type' => 'checkboxes',
'#title' => 'Enable focal point for the following:',
'#options' => $options,
'#default_value' => variable_get('focal_point_enabled_for', array_keys($options)),
'#default_value' => $config->get('focal_point_enabled_for'),
);
$form['focal_point_test_drive_link'] = array(
'#type' => 'markup',
Expand All @@ -46,9 +49,10 @@ function focal_point_configuration_form($form, &$form_state) {
* Form builder for the "test drive" page.
*/
function focal_point_test_drive_form($form, &$form_state) {
$fid = variable_get('focal_point_test_drive_image', NULL);
$config = config('focal_point.settings');
$fid = $config->get('focal_point_test_drive_image');
$file = !is_null($fid) ? file_load($fid) : NULL;
$focal_point = variable_get('focal_point_test_drive_focal_point', FOCAL_POINT_DEFAULT);
$focal_point = $config->get('focal_point_test_drive_focal_point');

if (!empty($fid) && $file) {
// Display the image and include the focal point indicator and field.
Expand Down Expand Up @@ -82,14 +86,17 @@ function focal_point_test_drive_form($form, &$form_state) {
* @see focal_point_test_drive_form()
*/
function focal_point_test_drive_form_submit(&$form, &$form_state) {
$config = config('focal_point.settings');

// Save the test drive focal point setting.
if (isset($form_state['values']['focal_point_test_drive_focal_point'])) {
$old_value = variable_get('focal_point_test_drive_focal_point', FOCAL_POINT_DEFAULT);
$old_value = $config->get('focal_point_test_drive_focal_point');
if ($old_value !== $form_state['values']['focal_point_test_drive_focal_point']) {
variable_set('focal_point_test_drive_focal_point', $form_state['values']['focal_point_test_drive_focal_point']);
$config->set('focal_point_test_drive_focal_point', $form_state['values']['focal_point_test_drive_focal_point']);
$config->save();

// Delete old derivative images since the focal point changed.
$fid = variable_get('focal_point_test_drive_image', NULL);
$fid = $config->get('focal_point_test_drive_image');
$file = !is_null($fid) ? file_load($fid) : NULL;
if ($file) {
image_path_flush($file->uri);
Expand Down Expand Up @@ -125,7 +132,7 @@ function focal_point_preview_page($fid, $focal_point = '') {
image_path_flush($file->uri);
}
else {
drupal_set_message(t('Invalid request. The preview images could not be displayed.'), 'error');
backdrop_set_message(t('Invalid request. The preview images could not be displayed.'), 'error');
$output = array();
}

Expand Down
4 changes: 2 additions & 2 deletions focal_point.api.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function hook_focal_point_default_method_info_alter(&$info) {
*
* @param array $element
* A keyed array with:
* - 'fid' : the Drupal file ID
* - 'fid' : the Backdrop file ID
* - 'focal_point' : the string representing the focal point position.
*/
function hook_focal_point_save($element) {
Expand All @@ -67,7 +67,7 @@ function hook_focal_point_save($element) {
* Hook invoked after deleting a Focal_Point record.
*
* @param int $fid
* The Drupal file ID associated to the deleted Focal point.
* The Backdrop file ID associated to the deleted Focal point.
*/
function hook_focal_point_delete($fid) {
// Your code here.
Expand Down
18 changes: 9 additions & 9 deletions focal_point.effects.inc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function focal_point_scale_and_crop_effect(&$image, $data) {
return image_crop_effect($image, $crop_data);
}

// At worst use the default effect and let Drupal handle the errors that
// At worst use the default effect and let Backdrop handle the errors that
// likely exist at this point.
return image_scale_and_crop_effect($image, $data);
}
Expand All @@ -64,24 +64,24 @@ function focal_point_crop_form($data = array()) {
'#collapsed' => TRUE,
);
$form['focal_point_advanced']['shift_x'] = array(
'#type' => 'textfield',
'#type' => 'number',
'#title' => 'Horizontal Shift',
'#description' => t('If set images will be cropped as though the focal point indicator has been shifted left (positive values) or shifted right (negative values) by the number of pixels specified.'),
'#default_value' => (isset($data['focal_point_advanced']['shift_x']) ? $data['focal_point_advanced']['shift_x'] : ''),
'#field_suffix' => t('pixels'),
'#size' => 10,
'#element_validate' => array('image_effect_integer_validate'),
'#allow_negative' => TRUE,
'#min' => -100000000,
'#max' => 1000000000,
);
$form['focal_point_advanced']['shift_y'] = array(
'#type' => 'textfield',
'#type' => 'number',
'#title' => 'Veritical Shift',
'#description' => t('If set images will be cropped as though the focal point indicator has been shifted up (positive values) or shifted down (negative values) by the number of pixels specified.'),
'#default_value' => (isset($data['focal_point_advanced']['shift_y']) ? $data['focal_point_advanced']['shift_y'] : ''),
'#field_suffix' => t('pixels'),
'#size' => 10,
'#element_validate' => array('image_effect_integer_validate'),
'#allow_negative' => TRUE,
'#min' => -100000000,
'#max' => 1000000000,
);

return $form;
Expand All @@ -95,7 +95,7 @@ function focal_point_crop_effect(&$image, $data) {
return image_crop_effect($image, $crop_data);
}

// At worst use the default effect and let Drupal handle the errors that
// At worst use the default effect and let Backdrop handle the errors that
// likely exist at this point.
return image_crop_effect($image, $data);
}
Expand All @@ -122,7 +122,7 @@ function focal_point_effect_crop_data($image, $data) {
$focal_point = $file->focal_point;

// Special handling for preview images.
$parameters = drupal_get_query_parameters();
$parameters = backdrop_get_query_parameters();
// Query parameters overwrite the stored focal point.
if (!empty($parameters['focal_point'])) {
$focal_point = focal_point_validate($parameters['focal_point']) ? $parameters['focal_point'] : $focal_point;
Expand Down
4 changes: 3 additions & 1 deletion focal_point.info
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name = Focal Point
description = Users can specify the focal point of an image for use during cropping.
core = 7.x
backdrop = 1.x
type = module

dependencies[] = entity
dependencies[] = entity_plus
dependencies[] = image

configure = admin/config/media/focal_point

0 comments on commit 287caff

Please sign in to comment.