The image scale effect allows one to scale an image while preserving the aspect ratio. So, only 1 of the new dimensions need to be specified.

The implementation of this effect assumes that as soon as this effect is applied, the current dimensions are known. Subsequently, the arithmetic to preserve aspect ratio is done in non image toolkit specific code and the results are passed to the resize effect.

Though the assumption that intermediate dimensions are known may hold for the GD toolkit that is part of core, it does not hold for the Imagemagick toolkit. The Imagemagick toolkit would just pass the 1 available parameter to the imagemagick processing but cannot do so as the effect is changed into a resize effect before being processed by toolkit specific code.

This error will reveal itself if the scale effect is chained after another effect that cannot compute resulting dimensions, like random rotate.

Issue #524562: Combine image scale and resize effects and add preserve aspect ratio option to scale might (will) be influenced by this patch. But as this is a bug that needs backporting to D7, I filed it as a separate issue.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fietserwin’s picture

Status: Needs work » Needs review
FileSize
24.98 KB

Please find attached a patch that solve the error indicated. Though solving it was not such a big deal, it turned out that quite some tests had to be changed to pass with this patch.

A summary of changes in this patch:

  • Treat scale as a separate effect and let the toolkit determine if it needs to calculate dimensions or that the underlying image handling can do without.
  • ImageDimensionsUnitTest gave a number of failed tests as, apparently, the RDF module was enabled in my test environment, and that add additional typeof="foaf:Image" attrobutes to style image tags. These tests were thus testing the wrong thing and are now chagned to check if the width and height attributes are added (including their correct values) or not.
  • Moved function image_scale_dimensions() from image.effects.inc to image.inc where it replaces the now removed function image_dimensions_scale(). (By moving it, it can be called by the GD toolkit).
  • due to the previous change, I had to change the tests of ImageDimensionsScaleTestCase which has now been renamed to ImageScaleDimensionsTestCase.
  • Updated the documentation of affected function to the new standards.
  • The image toolkit test mockup needed to be changed to cater for the fact that scale is now a separate toolkit effect.
  • Changed a number of tests that test if the resize toolkit effect was called (with already calculatd new dimensions) when applying a scale effect. Note: these tests appear in both ImageToolkitUnitTest (simpletest module) and ImageEffectsUnitTest (image module). This seems a bit overdone to me.

Status: Active » Needs work

The last submitted patch, drupal-1554074-1.patch, failed testing.

fietserwin’s picture

FileSize
25.44 KB

Oops, I forgot to reset to the remote 8.x branch but reset to my local 8.x branch instead and missed a large file movement/renaming refactoring.

Trying again. Locally I did get some exception about not being permitted to remove a config file, but that's not due to my changes.

Status: Needs review » Needs work

The last submitted patch, 1554074-3.patch, failed testing.

fietserwin’s picture

Status: Needs work » Needs review
FileSize
25.03 KB

One more try, without my local .htaccess changes.

cweagans’s picture

Issue tags: +Needs backport to D7
claudiu.cristea’s picture

Interesting but I'm not sure I understood. Looking in the code I saw that the missed dimension is computed before the toolkit is called (with resize). So, when ImageMagick toolkit is called, both dimensions are known and the toolkit is expected to resize. What else?

fietserwin’s picture

Status: Needs review » Needs work

#7: With Imagemagick as toolkit, it may occur that $this->getWidth() and $this->getHeight() return NULL because the width and height in the current transformation process are unknown. this can happen after an autorotate (based on exif data; contrib image effect) or after a random rotate.

For the imagemagick toolkit this is not bad. The convert binary will know the dimensions when actually processing the effects and can scale based on just the scale dimensions (i.e. the effect data).

However, before getting there ImageUtility::scaleDimensions() will fail with a division by zero (NULL/NULL).

Setting to needs work, because a reroll will be needed anyway.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

a_grizzli’s picture

Issue summary: View changes

This issue is still a problem, when there is a need to use ImageMagick Toolkit and combine image operations with scaling...

mourad-zitouni’s picture

Yes I'm having a problem with Image style also

mondrake’s picture

#14, #15: I am quite sure in Drupal 8 this is no longer a problem, since several steps have been taken to improve dimension calculation at runtime.

Can you confirm? If so, let's move this issue to D7 only.

mourad-zitouni’s picture

I tested it today in a local (Apache/2.4.25, PHP 7.2.4) and I'm having an error trying to display an image :

Image scale failed using the gd toolkit on

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Emil Stoianov’s picture

This is still an issue in D9.5
Images displayed with a scale effect, in the image style configuration, do not have dimensions displayed in the image tag
Trying to get those in a preprocess function

    $image = \Drupal::service('image.factory')->get(DRUPAL_ROOT . $variables['img_element']['#uri']);
    $width = $image->getWidth();

Which also has it empty.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.