Commit 19f73693 authored by alexpott's avatar alexpott

Issue #1364670 by mondrake, mrfelton, dinarcon, das-peter, zuuperman,...

Issue #1364670 by mondrake, mrfelton, dinarcon, das-peter, zuuperman, er.pushpinderrana, cchanana, drupaldrop, fietserwin: ImageStyle::transformDimensions unable to deal with all effects
parent 91efdc30
......@@ -278,7 +278,7 @@ function template_preprocess_image_style(&$variables) {
'height' => $variables['height'],
);
$style->transformDimensions($dimensions);
$style->transformDimensions($dimensions, $variables['uri']);
$variables['image'] = array(
'#theme' => 'image',
......
......@@ -307,9 +307,9 @@ public function createDerivative($original_uri, $derivative_uri) {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions) {
public function transformDimensions(array &$dimensions, $uri) {
foreach ($this->getEffects() as $effect) {
$effect->transformDimensions($dimensions);
$effect->transformDimensions($dimensions, $uri);
}
}
......
......@@ -70,7 +70,7 @@ public static function create(ContainerInterface $container, array $configuratio
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions) {
public function transformDimensions(array &$dimensions, $uri) {
// Most image effects will not change the dimensions. This base
// implementation represents this behavior. Override this method if your
// image effect does change the dimensions.
......
......@@ -43,8 +43,16 @@ public function applyEffect(ImageInterface $image);
* - height: the height in pixels, or NULL if unknown
* When either of the dimensions are NULL, the corresponding HTML attribute
* will be omitted when an image style using this image effect is used.
* @param string $uri
* Original image file URI. It is passed in to allow an effect to
* optionally use this information to retrieve additional image metadata
* to determine dimensions of the styled image.
* ImageEffectInterface::transformDimensions key objective is to calculate
* styled image dimensions without performing actual image operations, so
* be aware that performing IO on the URI may lead to decrease in
* performance.
*/
public function transformDimensions(array &$dimensions);
public function transformDimensions(array &$dimensions, $uri);
/**
* Returns the extension the derivative would have have after applying this
......
......@@ -127,8 +127,18 @@ public function createDerivative($original_uri, $derivative_uri);
* @param array $dimensions
* Associative array passed by reference. Implementations have to store the
* resulting width and height, in pixels.
* @param string $uri
* Original image file URI. It is passed in to allow effects to
* optionally use this information to retrieve additional image metadata
* to determine dimensions of the styled image.
* ImageStyleInterface::transformDimensions key objective is to calculate
* styled image dimensions without performing actual image operations, so
* be aware that performing IO on the URI may lead to decrease in
* performance.
*
* @see ImageEffectInterface::transformDimensions
*/
public function transformDimensions(array &$dimensions);
public function transformDimensions(array &$dimensions, $uri);
/**
* Determines the extension of the derivative without generating it.
......
......@@ -36,7 +36,7 @@ public function applyEffect(ImageInterface $image) {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions) {
public function transformDimensions(array &$dimensions, $uri) {
// The new image will have the exact dimensions defined for the effect.
$dimensions['width'] = $this->configuration['width'];
$dimensions['height'] = $this->configuration['height'];
......
......@@ -42,7 +42,7 @@ public function applyEffect(ImageInterface $image) {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions) {
public function transformDimensions(array &$dimensions, $uri) {
// If the rotate is not random and the angle is a multiple of 90 degrees,
// then the new dimensions can be determined.
if (!$this->configuration['random'] && ((int) ($this->configuration['degrees']) == $this->configuration['degrees']) && ($this->configuration['degrees'] % 90 == 0)) {
......
......@@ -36,7 +36,7 @@ public function applyEffect(ImageInterface $image) {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions) {
public function transformDimensions(array &$dimensions, $uri) {
if ($dimensions['width'] && $dimensions['height']) {
Image::scaleDimensions($dimensions, $this->configuration['width'], $this->configuration['height'], $this->configuration['upscale']);
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\image\Tests;
use Drupal\image\Entity\ImageStyle;
use Drupal\simpletest\WebTestBase;
/**
......@@ -39,7 +40,7 @@ function testImageDimensions() {
/** @var $style \Drupal\image\ImageStyleInterface */
$style = entity_create('image_style', array('name' => 'test', 'label' => 'Test'));
$style->save();
$generated_uri = 'public://styles/test/public/'. drupal_basename($original_uri);
$generated_uri = 'public://styles/test/public/'. \Drupal::service('file_system')->basename($original_uri);
$url = $style->buildUrl($original_uri);
$variables = array(
......@@ -231,6 +232,48 @@ function testImageDimensions() {
$style->addImageEffect($effect);
$style->save();
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />');
// Test URI dependent image effect.
$style = ImageStyle::create(['name' => 'test_uri', 'label' => 'Test URI']);
$effect = [
'id' => 'image_module_test_uri_dependent',
'data' => [],
'weight' => 0,
];
$style->addImageEffect($effect);
$style->save();
$variables = [
'#theme' => 'image_style',
'#style_name' => 'test_uri',
'#uri' => $original_uri,
'#width' => 40,
'#height' => 20,
];
// PNG original image. Should be resized to 100x100.
$generated_uri = 'public://styles/test_uri/public/'. \Drupal::service('file_system')->basename($original_uri);
$url = $style->buildUrl($original_uri);
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="100" height="100" alt="" class="image-style-test-uri" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 100);
$this->assertEqual($image_file->getHeight(), 100);
// GIF original image. Should be resized to 50x50.
$file = $files[1];
$original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
$generated_uri = 'public://styles/test_uri/public/'. \Drupal::service('file_system')->basename($original_uri);
$url = $style->buildUrl($original_uri);
$variables['#uri'] = $original_uri;
$this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="50" height="50" alt="" class="image-style-test-uri" />');
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
$this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
$image_file = $image_factory->get($generated_uri);
$this->assertEqual($image_file->getWidth(), 50);
$this->assertEqual($image_file->getHeight(), 50);
}
/**
......
......@@ -23,7 +23,7 @@ class NullTestImageEffect extends ImageEffectBase {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions) {
public function transformDimensions(array &$dimensions, $uri) {
// Unset image dimensions.
$dimensions['width'] = $dimensions['height'] = NULL;
}
......
<?php
/**
* @file
* Contains \Drupal\image_module_test\Plugin\ImageEffect\UriDependentTestImageEffect.
*/
namespace Drupal\image_module_test\Plugin\ImageEffect;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ImageEffectBase;
/**
* Performs an image operation that depends on the URI of the original image.
*
* @ImageEffect(
* id = "image_module_test_uri_dependent",
* label = @Translation("URI dependent test image effect")
* )
*/
class UriDependentTestImageEffect extends ImageEffectBase {
/**
* {@inheritdoc}
*/
public function transformDimensions(array &$dimensions, $uri) {
$dimensions = $this->getUriDependentDimensions($uri);
}
/**
* {@inheritdoc}
*/
public function applyEffect(ImageInterface $image) {
$dimensions = $this->getUriDependentDimensions($image->getSource());
return $image->resize($dimensions['width'], $dimensions['height']);
}
/**
* Make the image dimensions dependent on the image file extension.
*
* @param string $uri
* Original image file URI.
*
* @return array
* Associative array.
* - width: Integer with the derivative image width.
* - height: Integer with the derivative image height.
*/
protected function getUriDependentDimensions($uri) {
$dimensions = [];
$extension = pathinfo($uri, PATHINFO_EXTENSION);
switch (strtolower($extension)) {
case 'png':
$dimensions['width'] = $dimensions['height'] = 100;
break;
case 'gif':
$dimensions['width'] = $dimensions['height'] = 50;
break;
default:
$dimensions['width'] = $dimensions['height'] = 20;
break;
}
return $dimensions;
}
}
......@@ -302,6 +302,7 @@ function template_preprocess_responsive_image(&$variables) {
* ID.
* - width: The width of the image (if known).
* - height: The height of the image (if known).
* - uri: The URI of the image file.
* @param \Drupal\breakpoint\BreakpointInterface $breakpoint
* The breakpoint for this source tag.
* @param array $multipliers
......@@ -324,7 +325,7 @@ function responsive_image_build_source_attributes(ImageInterface $image, array $
// Loop through the image styles for this breakpoint and multiplier.
foreach ($image_style_mapping['image_mapping']['sizes_image_styles'] as $image_style_name) {
// Get the dimensions.
$dimensions = responsive_image_get_image_dimensions($image_style_name, array('width' => $width, 'height' => $height));
$dimensions = responsive_image_get_image_dimensions($image_style_name, array('width' => $width, 'height' => $height), $variables['uri']);
// Get MIME type.
$derivative_mime_type = responsive_image_get_mime_type($image_style_name, $extension);
$derivative_mime_types[] = $derivative_mime_type;
......@@ -385,12 +386,14 @@ function responsive_image_build_source_attributes(ImageInterface $image, array $
* An associative array containing:
* - width: The width of the source image (if known).
* - height: The height of the source image (if known).
* @param string $uri
* The URI of the image file.
*
* @return array
* Dimensions to be modified - an array with components width and height, in
* pixels.
*/
function responsive_image_get_image_dimensions($image_style_name, array $dimensions) {
function responsive_image_get_image_dimensions($image_style_name, array $dimensions, $uri) {
// Determine the dimensions of the styled image.
if ($image_style_name == RESPONSIVE_IMAGE_EMPTY_IMAGE) {
$dimensions = array(
......@@ -398,11 +401,8 @@ function responsive_image_get_image_dimensions($image_style_name, array $dimensi
'height' => 1,
);
}
else {
$entity = ImageStyle::load($image_style_name);
if ($entity instanceof Drupal\image\Entity\ImageStyle) {
$entity->transformDimensions($dimensions);
}
elseif ($entity = ImageStyle::load($image_style_name)) {
$entity->transformDimensions($dimensions, $uri);
}
return $dimensions;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment