Move GD-specific code from Rotate::validateArguments() to Rotate::execute()

Created on 1 April 2015, over 9 years ago
Updated 27 January 2023, over 1 year ago

Problem

There is GD specific code in Rotate::validateArguments.
Generally, in GD toolkit operation classes (like, gd/Resize gd/Scale etc.) we only have calls to GD functions in their 'execute' methods.

This blocks #2578563: [PP-1] ImageToolkitOperations need traits as it is needed to have toolkit agnostic traits for the ::arguments and ::validateArguments methods for all image toolkit operations, so that contrib toolkits could benefit from those without having to re-add boilerplate code. See the Image Effects module for an example of using traits shared by GD and ImageMagick toolkits operations.

Proposed resolution

  • Move GD specific code from ::validateArguments to ::execute
  • Add test coverage.

The change in the test to add the mock logger is because, with the change to validateArguments() when the exception is thrown, it tries to log it.

Some out of scope changes can be done in follow-up #2796369: [PP-1] Cleanup gd/Rotate::execute() arguments array overloading

Remaining tasks

Review patch. Note changes might make more sense when looking at it in context.

User interface changes

None

API changes

None

Original report by @YesCT

original bug is no longer an issue

1. the new OS X builds have the toolkit
2. the test doesn't actually perform changes to images

In the test class it already stubbed out the 'execute' method of the image toolkit operations, so we can test the Image class without actually performing changes to images (this relates to the original issue report about testRotate() failing).

report was

On PHP 5.5.14 unit tests fail Drupal\Tests\Core\Image\ImageTest::testRotate imagecolorallocatealpha() expects parameter 1 to be resource, null given

Follow-up to #2463879: PHP unit tests fail if intl extension is missing

On running phpunit tests:
https://www.drupal.org/node/2116263

./vendor/bin/phpunit --filter=ImageTest
will run just the one class with the test that fails.

PHP 5.5.14

mac os x native /usr/bin/php

There was 1 error:

1) Drupal\Tests\Core\Image\ImageTest::testRotate
imagecolorallocatealpha() expects parameter 1 to be resource, null given

/Users/ctheys/foo/drupal/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php:60
/Users/ctheys/foo/drupal/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php:177
/Users/ctheys/foo/drupal/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php:127
/Users/ctheys/foo/drupal/core/lib/Drupal/Core/Image/Image.php:148
/Users/ctheys/foo/drupal/core/lib/Drupal/Core/Image/Image.php:190
/Users/ctheys/foo/drupal/core/tests/Drupal/Tests/Core/Image/ImageTest.php:455

FAILURES!
Tests: 7631, Assertions: 40525, Errors: 1, Incomplete: 2.

php -i | grep "GD Version"
GD Version => bundled (2.1.0 compatible)

MAMP php's

PHP 5.4.34

Tests: 7631, Assertions: 40527, Incomplete: 2.

PHP 5.5.18

Tests: 7631, Assertions: 40527, Incomplete: 2.

PHP 5.6.2

Tests: 7631, Assertions: 40527, Incomplete: 2.
📌 Task
Status

Needs work

Version

10.1

Component
Image system 

Last updated 4 days ago

Created by

🇺🇸United States yesct

Live updates comments and jobs are added and updated live.
  • PHP 5.5

    The issue particularly affects sites running on PHP version 5.5.0 or later.

  • Needs change record

    A change record needs to be drafted before an issue is committed. Note: Change records used to be called change notifications.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Status changed to Needs review over 1 year ago
  • Status changed to RTBC over 1 year ago
  • 🇺🇸United States xjm
  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States xjm

    This took awhile to review thoroughly, but it is pretty close to ready. Nice work!

    1. +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
      @@ -37,12 +37,37 @@ protected function arguments() {
      +    if (!is_numeric($arguments['degrees'])) {
      

      Presumably it should also be between -360 and 360? This might be followup territory since technically someone rotating 477 degrees might experience a BC break. I tested in the user interface and entering e.g. "-477" in the widget works fine. It is odd/confusing with the "Randomize" option though.

    2. +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
      @@ -37,12 +37,37 @@ protected function arguments() {
      +      throw new \InvalidArgumentException('Invalid degrees (' . $arguments['degrees'] . ') specified for the image \'rotate\' operation. It should be numeric.');
      ...
      +      throw new \InvalidArgumentException('Invalid background color (' . $arguments['background'] . ') specified for the image \'rotate\' operation. It should be a hex string.');
      

      I confirmed that the method is already documented as @throws for this exception on the interface.

      Should we maybe double-quote these strings to avoid needing to escape the single quotes and to avoid the concatenation with the arguments?

    3. +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
      @@ -37,12 +37,37 @@ protected function arguments() {
      +    // Assure background color is a valid hex string.
      

      "Assure" does not make sense in the context. It's a transitive verb that means "to soothe doubts", e.g., "I assured them that I would review the patch carefully." This should be:

      Ensure that the background color is a valid hex string.

    4. +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
      @@ -37,12 +37,37 @@ protected function arguments() {
      +  /**
      +   * {@inheritdoc}
      +   */
      +  protected function execute(array $arguments) {
      +    // PHP installations using non-bundled GD do not have imagerotate.
      +    if (!function_exists('imagerotate')) {
      +      $this->logger->notice('The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', [
      +        '%file' => $this->getToolkit()->getSource(),
      +      ]);
      +      return FALSE;
      +    }
      
      @@ -80,19 +105,6 @@ protected function validateArguments(array $arguments) {
      -  /**
      -   * {@inheritdoc}
      -   */
      -  protected function execute(array $arguments) {
      -    // PHP installations using non-bundled GD do not have imagerotate.
      -    if (!function_exists('imagerotate')) {
      -      $this->logger->notice('The image %file could not be rotated because the imagerotate() function is not available in this PHP installation.', ['%file' => $this->getToolkit()->getSource()]);
      -      return FALSE;
      -    }
      

      I verified that this is just a diff artifact. These lines are not actually changing. They just appear to be moved because a larger number of lines are moved from one method to another.

    5. +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
      @@ -37,12 +37,37 @@ protected function arguments() {
           // PHP 5.5 GD bug: https://bugs.php.net/bug.php?id=65148: To prevent buggy
           // behavior on negative multiples of 90 degrees we convert any negative
           // angle to a positive one between 0 and 360 degrees.
           $arguments['degrees'] -= floor($arguments['degrees'] / 360) * 360;
      

      Out of scope, but maybe a followup: Does this bug still exist? It's been five years since Drupal supported PHP 5.5.

    6. +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
      @@ -80,19 +105,6 @@ protected function validateArguments(array $arguments) {
      -    return $arguments;
      -  }
      -
      

      At a glance the diff makes it look like this is removing the return value, but it is just a diff artifact due to the lines being moved. I confirmed that execute() never returns $arguments in HEAD, only Booleans.

    7. This should probably have a change record, since it will change what happens when for contrib or custom code that extends it.

    NW primarily for point 3 and for the change record, though followups (or finding existing related issues) would be helpful too. Thanks!

  • 🇮🇹Italy mondrake 🇮🇹

    📌 Remove PHP 5 code for GD Needs work addresses #72.5.

Production build 0.71.5 2024