GdImage cannot serialize in PHP8.1

Created on 31 January 2023, almost 2 years ago
Updated 30 January 2024, 11 months ago

Problem/Motivation

When use image.factory service to resize image files (which is functional provided by Batch Resize Image β†’ module will return an error message below
Exception: Serialization of 'GdImage' is not allowed in serialize() in PHP 8.1.

I've tried several image operations (scale, resize, crop) were can not serialize in PHP 8.1, but it works in PHP 7.4.

πŸ› Bug report
Status

Postponed: needs info

Version

9.5

Component
Image moduleΒ  β†’

Last updated 2 days ago

Created by

πŸ‡ΉπŸ‡ΌTaiwan gloomcheng

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @gloomcheng
  • If there is custom code you must provide it in a bug report so that the community can reproduce the bug.

  • πŸ‡ΉπŸ‡ΌTaiwan gloomcheng

    I just run the resize functional provieded from Batch Image Resize module without any custom code.
    I do some modify to confirm that this issue is most related core instead of Batch Image Resize, and can confirm the module can work on PHP 7.4 environment.

  • Status changed to Postponed: needs info almost 2 years ago
  • Please explain in some detail why this is a Drupal core bug based on your research.

    Add to the issue summary steps to reproduce the bug that contributors can follow.

  • πŸ‡ΉπŸ‡ΌTaiwan gloomcheng

    At first, the Batch Resize Image module do very simple thing which is call Image::resize method to resize image assets, and I have tried to set breakpoint to confirm the error message happened when $image->resize() runs.

            if ($file->filesize > $threshold_size && ($width > $size_limit || $height > $size_limit)) {
              // Computing the new height and width based on user input.
              if ($width > $height) {
                $width = $size_limit;
                $height = $height * $width / $width;
              }
              else {
                $height = $size_limit;
                $width = $width * $height / $height;
              }
    
              // Updating the image size as per set threshold.
              $image->resize($width, $height);
              $image->save();
    
              // Chnaging the image size in the database.
              $file_entity = \Drupal::entityTypeManager()->getStorage('file')->load($file->fid);
              $file_entity->save();
              $result['message'] = '"' . $file->filename . "\" has been transformed.";
              $context['results']['resized'][] = $result;
            }
    

    Ref: BatchResizeImageForm.php L102:L122

    And then, I have tried to modify to use $image->scale() and other methods to validate but get same error.
    Therefore, I use alternative similar module Image Compression β†’ to confirm and got the same result, it also faced the issue which can not serialized GdImage object.

    Finally I decide to switch PHP version to 7.4 and then the module works, so that I guess this issue most related Drupal core.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Without the contrib module is this reproducible in core. Sounds like all tests and examples involve using a contrib module that is failing.

Production build 0.71.5 2024