retina_images.module in Retina Images 8
Same filename and directory in other branches
This module provides an image effect to assist in outputing high resolution images.
File
retina_images.moduleView source
<?php
/**
* @file
* This module provides an image effect to assist in outputing high resolution images.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements hook_image_effect_info_alter().
*/
function retina_images_image_effect_info_alter(&$effects) {
$effects['image_resize']['class'] = 'Drupal\\retina_images\\Plugin\\ImageEffect\\RetinaResizeImageEffect';
$effects['image_scale']['class'] = 'Drupal\\retina_images\\Plugin\\ImageEffect\\RetinaScaleImageEffect';
$effects['image_scale_and_crop']['class'] = 'Drupal\\retina_images\\Plugin\\ImageEffect\\RetinaScaleAndCropImageEffect';
$effects['image_crop']['class'] = 'Drupal\\retina_images\\Plugin\\ImageEffect\\RetinaCropImageEffect';
}
/**
* Implements hook_theme().
*/
function retina_images_theme() {
return array(
'retina_images_image_crop_summary' => array(
'variables' => array(
'data' => NULL,
'effect' => array(),
),
),
'retina_images_image_resize_summary' => array(
'variables' => array(
'data' => NULL,
'effect' => array(),
),
),
'retina_images_image_scale_summary' => array(
'variables' => array(
'data' => NULL,
'effect' => array(),
),
),
'retina_images_image_style_preview' => array(
'variables' => array(
'style' => NULL,
),
),
);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function retina_images_form_image_style_form_alter(&$form, FormStateInterface $form_state) {
// Get the style to pass to our new theme function.
$style = $form_state
->getFormObject()
->getEntity();
// Don't show the preview on the add form.
if (isset($form['preview'])) {
// Override the preview image area to use our own preview.
// Show the thumbnail preview.
$preview_arguments = array(
'#theme' => 'retina_images_image_style_preview',
'#style' => $style,
);
$form['preview'] = array(
'#type' => 'item',
'#title' => \Drupal::translation()
->translate('Preview'),
'#markup' => drupal_render($preview_arguments),
// Render preview above parent elements.
'#weight' => -5,
);
}
}
/**
* Prepares variables for retina image style preview templates.
*
* Default template: retina-images-image-style-preview.html.twig.
*
* @param array $variables
* An associative array containing:
* - style: \Drupal\image\ImageStyleInterface image style being previewed.
*/
function template_preprocess_retina_images_image_style_preview(&$variables) {
// Style information.
$style = $variables['style'];
$variables['style_id'] = $style
->id();
$variables['style_name'] = $style
->label();
// Cache bypass token.
$variables['cache_bypass'] = REQUEST_TIME;
// Sample image info.
$sample_width = 160;
$sample_height = 160;
$image_factory = \Drupal::service('image.factory');
// Set up original file information.
$original_path = \Drupal::config('image.settings')
->get('preview_image');
$original_image = $image_factory
->get($original_path);
$variables['original'] = array(
'url' => file_url_transform_relative(file_create_url($original_path)),
'width' => $original_image
->getWidth(),
'height' => $original_image
->getHeight(),
);
if ($variables['original']['width'] > $variables['original']['height']) {
$variables['preview']['original']['width'] = min($variables['original']['width'], $sample_width);
$variables['preview']['original']['height'] = round($variables['preview']['original']['width'] / $variables['original']['width'] * $variables['original']['height']);
}
else {
$variables['preview']['original']['height'] = min($variables['original']['height'], $sample_height);
$variables['preview']['original']['width'] = round($variables['preview']['original']['height'] / $variables['original']['height'] * $variables['original']['width']);
}
// Set up derivative file information.
$preview_file = $style
->buildUri($original_path);
$variables['preview_link'] = Url::fromRoute('retina_images.image.style_preview', [
'image_style' => $style
->id(),
]);
// Create derivative if necessary.
if (!file_exists($preview_file)) {
$style
->createDerivative($original_path, $preview_file);
}
$preview_image = $image_factory
->get($preview_file);
$variables['derivative'] = array(
'url' => file_url_transform_relative(file_create_url($preview_file)),
'width' => $preview_image
->getWidth(),
'height' => $preview_image
->getHeight(),
);
$style
->transformDimensions($variables['derivative'], $preview_file);
if ($variables['derivative']['width'] > $variables['derivative']['height']) {
$variables['preview']['derivative']['width'] = min($variables['derivative']['width'], $sample_width);
$variables['preview']['derivative']['height'] = round($variables['preview']['derivative']['width'] / $variables['derivative']['width'] * $variables['derivative']['height']);
}
else {
$variables['preview']['derivative']['height'] = min($variables['derivative']['height'], $sample_height);
$variables['preview']['derivative']['width'] = round($variables['preview']['derivative']['height'] / $variables['derivative']['height'] * $variables['derivative']['width']);
}
// Build the preview of the original image.
$variables['original']['rendered'] = array(
'#theme' => 'image',
'#uri' => $original_path,
'#alt' => t('Sample original image'),
'#title' => '',
'#attributes' => array(
'width' => $variables['original']['width'],
'height' => $variables['original']['height'],
'style' => 'width: ' . $variables['preview']['original']['width'] . 'px; height: ' . $variables['preview']['original']['height'] . 'px;',
),
);
// Build the preview of the image style derivative. Timestamps are added
// to prevent caching of images on the client side.
$variables['derivative']['rendered'] = array(
'#theme' => 'image',
'#uri' => $variables['derivative']['url'] . '?cache_bypass=' . $variables['cache_bypass'],
'#alt' => t('Sample modified image'),
'#title' => '',
'#attributes' => array(
'width' => $variables['derivative']['width'],
'height' => $variables['derivative']['height'],
'style' => 'width: ' . $variables['preview']['derivative']['width'] . 'px; height: ' . $variables['preview']['derivative']['height'] . 'px;',
),
);
}
Functions
Name | Description |
---|---|
retina_images_form_image_style_form_alter | Implements hook_form_FORM_ID_alter(). |
retina_images_image_effect_info_alter | Implements hook_image_effect_info_alter(). |
retina_images_theme | Implements hook_theme(). |
template_preprocess_retina_images_image_style_preview | Prepares variables for retina image style preview templates. |