Problem/Motivation

With PHP 8.1 passing null to non-nullable argument will generate a deprecation notice.
Uploading an SVG can cause a deprecation notice as $type is defined when calling image_type_to_extension in GDToolkit.php.
$type is not defined as SVG is not a supported type.

Steps to reproduce

Install Drupal 9.x with PHP 8.1.
Add a an image field to an entity that supports svg extensions.
Upload an SVG.
See deprecation message:
Deprecated function: image_type_to_extension(): Passing null to parameter #1 ($image_type) of type int is deprecated in Drupal\system\Plugin\ImageToolkit\GDToolkit->load() (line 202 of core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php).

Proposed resolution

Check the $type is not null before calling image_type_to_extension.
For example.

$function = $this->getType() ? 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE) : NULL;
    if ($function && function_exists($function) && $resource = $function($this->getSource()))

Remaining tasks

Create an MR.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

CommentFileSizeAuthor
#5 3338962.patch856 bytesranjit1032002

Issue fork drupal-3338962

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tcrawford created an issue. See original summary.

cilefen’s picture

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

9.5.x is the bug fix branch.

tcrawford’s picture

Thanks. I will re-roll this against 9.5.x-dev.

ranjit1032002’s picture

Status: Active » Needs review
StatusFileSize
new856 bytes

Created a patch for the issue mentioned, please review.
Thank You.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue was started in an MR and should remain there.

Thank you @tcrawford for steps to reproduce. This will need a test case showing the issue.

sakthi_dev made their first commit to this issue’s fork.

sakthi_dev’s picture

@smustgrave added the patch from #5 to MR.

mondrake’s picture

This issue is a bit obscure to me... the GD toolkit does not support SVG images, since GD does not (and likely will never) support vectorial graphic formats. Normally unsupported file formats would fail much earlier than where you are proposing to fix, i.e. in file extension validations, where the file extension of the file to-be-uploaded is matched against the file extensions that an image toolkit supports. I'm not sure how you can have an

entity that supports svg extensions

? Maybe with contrib?

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.

tcrawford’s picture

@mondrake You are right. The issue only occurs for me in projects using the drupal/rokka module, which uses its own RokkaToolkit that extends the GDToolkit. Rokka supports the svg extension, but there is no related type and so we see the type error.

  /**
   * Get the mime type.
   *
   * @return string
   *   Mime type.
   */
  public function getMimeType(): string {
    $mime_type = $this->getType() ? image_type_to_mime_type($this->getType()) : '';

    // GD library does not support SVG, but Rokka does. So we will trick the
    // toolkit and detect SVG as a mime time the first time the file is uploaded
    // to the tmp folder. If we detect an SVG image, we allow the upload.
    if (empty($mime_type)) {
      $uri = $this->getSource();
      if ($uri && file_exists($uri) && strpos($uri, 'rokka://') === FALSE) {
        $rokka_additional_allowed_mime_types = ['image/svg', 'image/svg+xml'];
        $mime_type_guessed = mime_content_type($uri);
        if (in_array($mime_type_guessed, $rokka_additional_allowed_mime_types)) {
          $mime_type = $mime_type_guessed;
        }
      }
    }

    return $mime_type;
  }

I guess we need to get have the Rokka Toolkit modified to override the load() method also and check that getType() is set.

I am closing this issue as 'cannot reproduce' as it only occurs in combination with the RokkaToolkit.

tcrawford’s picture

Status: Needs work » Closed (cannot reproduce)