wp-cli / media-command

Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
MIT License
44 stars 41 forks source link

Add a couple debug scenarios for square image regeneration #173

Closed danielbachhuber closed 1 year ago

danielbachhuber commented 1 year ago

From https://github.com/wp-cli/media-command/pull/169

danielbachhuber commented 1 year ago

Test suite:

001 Scenario: Debug square image size part two # features/media-regenerate.feature:16
      Then STDOUT should be empty              # features/media-regenerate.feature:23
        $ file /tmp/wp-cli-test-cache/white-150-square.jpg
        /tmp/wp-cli-test-cache/white-150-square.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, progressive, precision 8, 150x150, components 1

        cwd: /tmp/wp-cli-test-run--63a36067645c77.65434519/
        run time: 0.0037679672241211
        exit status: 0 (Exception)

002 Scenario: Debug square image size part two # features/media-regenerate.feature:26
      Then STDOUT should be empty              # features/media-regenerate.feature:36
        $ wp post meta get 4 _wp_attachment_metadata
        array (
          'width' => 150,
          'height' => 150,
          'file' => 'white-150-square.jpg',
          'filesize' => 219,
          'sizes' => 
          array (
            'thumbnail' => 
            array (
              'file' => 'white-150-square-150x150.jpg',
              'width' => 150,
              'height' => 150,
              'mime-type' => 'image/jpeg',
              'filesize' => 251,
            ),
          ),
          'image_meta' => 
          array (
            'aperture' => '0',
            'credit' => '',
            'camera' => '',
            'caption' => '',
            'created_timestamp' => '0',
            'copyright' => '',
            'focal_length' => '0',
            'iso' => '0',
            'shutter_speed' => '0',
            'title' => '',
            'orientation' => '0',
            'keywords' => 
            array (
            ),
          ),
        )

        cwd: /tmp/wp-cli-test-run--63a36069dd4398.75544501/
        run time: 0.28598713874817
        exit status: 0 (Exception)

Locally:

001 Scenario: Debug square image size part two # features/media-regenerate.feature:16
      Then STDOUT should be empty              # features/media-regenerate.feature:23
        $ file /var/folders/6t/fzd50mwd57766jb0qdj2vl100000gn/T/wp-cli-test-cache/white-150-square.jpg
        /var/folders/6t/fzd50mwd57766jb0qdj2vl100000gn/T/wp-cli-test-cache/white-150-square.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, progressive, precision 8, 150x150, components 1

        cwd: /var/folders/6t/fzd50mwd57766jb0qdj2vl100000gn/T/wp-cli-test-run--63a3825ae67990.52853513/
        run time: 0.0071618556976318
        exit status: 0 (Exception)

002 Scenario: Debug square image size part two # features/media-regenerate.feature:26
      Then STDOUT should be empty              # features/media-regenerate.feature:36
        $ wp post meta get 4 _wp_attachment_metadata
        array (
          'width' => 150,
          'height' => 150,
          'file' => 'white-150-square.jpg',
          'filesize' => 219,
          'sizes' =>
          array (
          ),
          'image_meta' =>
          array (
            'aperture' => '0',
            'credit' => '',
            'camera' => '',
            'caption' => '',
            'created_timestamp' => '0',
            'copyright' => '',
            'focal_length' => '0',
            'iso' => '0',
            'shutter_speed' => '0',
            'title' => '',
            'orientation' => '0',
            'keywords' =>
            array (
            ),
          ),
        )

        cwd: /var/folders/6t/fzd50mwd57766jb0qdj2vl100000gn/T/wp-cli-test-run--63a3825f9a2cf9.40035046/
        run time: 0.21935796737671
        exit status: 0 (Exception)
danielbachhuber commented 1 year ago

My guess is that something is wonky inside of image_size_dimensions() (https://github.com/WordPress/wordpress-develop/blob/0cb8475c0d07d23893b1d73d755eda5f12024585/src/wp-includes/media.php#L530-L657) or the resize() method of WP_Image_Editor_Imagick or WP_Iimage_Editor_GD (https://github.com/WordPress/wordpress-develop/blob/0cb8475c0d07d23893b1d73d755eda5f12024585/src/wp-includes/class-wp-image-editor-imagick.php#L270-L293). It's hard to say what it might be, though.

danielbachhuber commented 1 year ago

Oh! The problem is definitely with WP_Image_Editor_Imagick.

As it turns out, I was running my previous tests with GD. I had installed ImageMagick, but hadn't yet enabled pecl install imagick.

Once I did so, the problem manifested right away:

$ wp site empty --uploads --yes && wp db reset --yes && wp core install
Success: The site at 'https://vanilla.test' was emptied.
Success: Database reset.
Success: WordPress installed successfully.
$ wp option update uploads_use_yearmonth_folders 0
Success: Updated 'uploads_use_yearmonth_folders' option.
$ wp media import white-150-square.jpg --title="My imported small attachment" --porcelain
4
$ ls wp-content/uploads
2022                         white-150-square-150x150.jpg white-150-square.jpg
danielbachhuber commented 1 year ago

Debugging complete 🚢