PHP 8.1 deprecation (passing null to non-nullable argument) in GDToolkit when uploading SVG files.

Created on 3 February 2023, almost 2 years ago
Updated 17 September 2024, 3 months ago

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

🐛 Bug report
Status

Closed: cannot reproduce

Version

11.0 🔥

Component
Image system 

Last updated 1 day ago

Created by

🇨🇭Switzerland tcrawford

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • Issue created by @tcrawford
  • @tcrawford opened merge request.
  • 9.5.x is the bug fix branch.

  • 🇨🇭Switzerland tcrawford

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

  • Status changed to Needs review over 1 year ago
  • 🇮🇳India Ranjit1032002

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

  • 🇺🇸United States smustgrave

    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.

  • Status changed to Needs work over 1 year ago
  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    30,103 pass, 2 fail
  • 🇮🇳India sakthi_dev

    @smustgrave added the patch from #5 to MR.

  • 🇮🇹Italy mondrake 🇮🇹

    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?

  • 🇨🇭Switzerland tcrawford

    @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.

  • Status changed to Closed: cannot reproduce 3 months ago
Production build 0.71.5 2024