simple_image_rotate.module in Simple Image Rotate 7
Same filename and directory in other branches
Allows users to rotate images on node forms.
To use this feature the "Enable rotate icon" checkbox should be marked for an image field on its settings page (Administration ┬╗ Structure ┬╗ Content types ┬╗ Your content type ┬╗ Manage fields ┬╗ Your image field) and an acting user should have the "rotate images" permission. It will add a small rotate icon near uploaded image. Clicking to this icon rotates image clockwise with jQuery only. After saving the node the image file is actually rotated and renamed. The module requires jQuery library and therefore you will probably need to install the jQuery Update module.
File
simple_image_rotate.moduleView source
<?php
/**
* @file
* Allows users to rotate images on node forms.
*
* To use this feature the "Enable rotate icon" checkbox should be marked for
* an image field on its settings page (Administration ┬╗ Structure ┬╗ Content
* types ┬╗ Your content type ┬╗ Manage fields ┬╗ Your image field) and an acting
* user should have the "rotate images" permission. It will add a small rotate
* icon near uploaded image. Clicking to this icon rotates image clockwise with
* jQuery only. After saving the node the image file is actually rotated and
* renamed. The module requires jQuery library and therefore you will probably
* need to install the jQuery Update module.
*/
/**
* Implements hook_permission().
*/
function simple_image_rotate_permission() {
return array(
'rotate images' => array(
'title' => t('Rotate images'),
'description' => t('Allows a user to rotate images.'),
),
);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Add enable rotate icon checkbox to image field forms.
*/
function simple_image_rotate_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
$form['instance']['enable_rotate'] = array(
'#type' => 'checkbox',
'#title' => t('Enable rotate icon'),
'#description' => t('If checked rotate icon will appear near each uploaded image.'),
'#default_value' => isset($form['#instance']['enable_rotate']) ? $form['#instance']['enable_rotate'] : 0,
'#weight' => 16,
);
}
/**
* Implements hook_field_widget_form_alter().
*/
function simple_image_rotate_field_widget_form_alter(&$element, &$form_state, $context) {
// Check if user can rotate images.
if ($context['field']['type'] == 'image' && user_access('rotate images')) {
// Check if image rotation is enabled.
if (isset($context['instance']['enable_rotate']) && $context['instance']['enable_rotate']) {
// Loop through all images.
foreach (element_children($element) as $delta) {
// Skip upload new image button.
if (isset($element['#file_upload_delta']) && $delta >= $element['#file_upload_delta']) {
continue;
}
// Add rotate icon and hidden rotate field.
$element[$delta]['rotate'] = array(
'#type' => 'hidden',
'#attributes' => array(
'class' => 'rotate',
),
'#value' => 0,
);
$element[$delta]['rotate_icon'] = array(
'#markup' => l(t('↻'), '', array(
'html' => TRUE,
'attributes' => array(
'title' => t('Rotate image clockwise'),
'class' => array(
'rotate-icon',
),
'data-rotate' => 0,
),
)),
);
}
drupal_add_js(drupal_get_path('module', 'simple_image_rotate') . '/js/simple_image_rotate.js');
drupal_add_css(drupal_get_path('module', 'simple_image_rotate') . '/css/simple_image_rotate.css');
}
}
}
/**
* Implements hook_entity_presave().
*/
function simple_image_rotate_entity_presave($entity, $type) {
simple_image_rotate_rotate_image($entity, $type);
}
/**
* Implements hook_profile2_presave().
*/
function simple_image_rotate_profile2_presave($profile) {
simple_image_rotate_rotate_image($profile, 'profile2');
}
/**
* Rotates image.
*/
function simple_image_rotate_rotate_image($entity, $type) {
// Load all image fields.
$image_fields = array_keys(field_read_fields(array(
'type' => 'image',
)));
// Loop through all image fields.
foreach ($image_fields as $field) {
// Image field exists in the entity.
if (isset($entity->{$field})) {
// Loop through all images of this field.
foreach ($entity->{$field}[LANGUAGE_NONE] as $delta => $value) {
// If rotate angle is indicated.
if (isset($value['rotate']) && $value['rotate']) {
// Load image file.
$file = file_load($value['fid']);
// Get new filename for rotated image.
$new_uri = $file->uri;
$file_counter = 1;
if (preg_match('#_r([\\d]+)\\.([^.]+)$#i', $new_uri, $matches)) {
$file_counter = $matches[1] + 1;
$new_uri = str_replace($matches[0], '_r' . $file_counter . '.jpg', $new_uri);
}
// Increment filename counter if filename is occupied.
while (file_exists($new_uri)) {
$pos = strrpos($new_uri, '.');
$new_uri = substr_replace($new_uri, '_r' . $file_counter, $pos, 0);
}
// Move image file to a new location.
if (file_move($file, $new_uri, 'FILE_EXIST_ERROR')) {
// Update file object after moving file.
$file = file_load($value['fid']);
// Rotate image and save image object.
$image = image_load($file->uri);
image_rotate($image, $value['rotate']);
if (image_save($image)) {
// Reload image to get new width, height and filesize.
$image = image_load($file->uri);
// Update image width and height in entity.
$entity->{$field}[LANGUAGE_NONE][$delta]['width'] = $image->info['width'];
$entity->{$field}[LANGUAGE_NONE][$delta]['height'] = $image->info['height'];
// Update file.
$file->filesize = $image->info['file_size'];
file_save($file);
}
}
}
}
}
}
}
Functions
Name![]() |
Description |
---|---|
simple_image_rotate_entity_presave | Implements hook_entity_presave(). |
simple_image_rotate_field_widget_form_alter | Implements hook_field_widget_form_alter(). |
simple_image_rotate_form_field_ui_field_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
simple_image_rotate_permission | Implements hook_permission(). |
simple_image_rotate_profile2_presave | Implements hook_profile2_presave(). |
simple_image_rotate_rotate_image | Rotates image. |