wp-cli / media-command

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

Avoid deleting meta for other sizes when running `wp media regenerate` with `--image_size` #170

Closed wojtekn closed 1 year ago

wojtekn commented 1 year ago

In this PR, I propose to fix the regenerate command issue, which causes removing other sizes from post meta when the command is used with the --image_size option.

Testing steps:

  1. Upload the image
  2. Change the size settings for medium in WordPress:
  3. Regenerate the thumbnails:
wp media regenerate --image_size=medium --skip-delete --only-missing
  1. Check postmeta table for _wp_attachment_metadata e.g.:
wp post list --post_type=attachment
wp post meta get 5 _wp_attachment_metadata

The actual fix and tests come from https://github.com/wp-cli/media-command/pull/141.

Fixes https://github.com/wp-cli/media-command/issues/130

danielbachhuber commented 1 year ago

Documenting the steps I took to reproduce the original issue:

$ wp core version
6.1.1
$ wp site empty --uploads --yes && wp db reset --yes && wp core install
Success: The site at 'http://vanilla.test' was emptied.
Success: Database reset.
Success: WordPress installed successfully.
$ wp media import test.jpg
Imported file 'test.jpg' as attachment ID 4.
$ ls wp-content/uploads/2022/12/test*
wp-content/uploads/2022/12/test-1024x768.jpg  wp-content/uploads/2022/12/test-1536x1152.jpg wp-content/uploads/2022/12/test-300x225.jpg   wp-content/uploads/2022/12/test-scaled.jpg
wp-content/uploads/2022/12/test-150x150.jpg   wp-content/uploads/2022/12/test-2048x1536.jpg wp-content/uploads/2022/12/test-768x576.jpg   wp-content/uploads/2022/12/test.jpg
$ wp option update medium_size_w 450
Success: Updated 'medium_size_w' option.
$ wp option update medium_size_h 450
Success: Updated 'medium_size_h' option.
$ wp media regenerate --image_size=medium --only-missing --skip-delete --yes
Found 1 image to regenerate.
1/1 Regenerated "medium" thumbnail for "test" (ID 4).
Success: Regenerated 1 of 1 images.
$ ls wp-content/uploads/2022/12/test*
wp-content/uploads/2022/12/test-1024x768.jpg  wp-content/uploads/2022/12/test-1536x1152.jpg wp-content/uploads/2022/12/test-300x225.jpg   wp-content/uploads/2022/12/test-768x576.jpg   wp-content/uploads/2022/12/test.jpg
wp-content/uploads/2022/12/test-150x150.jpg   wp-content/uploads/2022/12/test-2048x1536.jpg wp-content/uploads/2022/12/test-450x338.jpg   wp-content/uploads/2022/12/test-scaled.jpg
$ wp post meta get 4 _wp_attachment_metadata
array (
  'width' => 2560,
  'height' => 1920,
  'file' => '2022/12/test-scaled.jpg',
  'filesize' => 847507,
  'sizes' =>
  array (
    'medium' =>
    array (
      'file' => 'test-450x338.jpg',
      'width' => 450,
      'height' => 338,
      'mime-type' => 'image/jpeg',
      'filesize' => 41704,
    ),
  ),
  'image_meta' =>
  array (
    'aperture' => '1.6',
    'credit' => '',
    'camera' => 'iPhone 12 Pro',
    'caption' => '',
    'created_timestamp' => '1663191161',
    'copyright' => '',
    'focal_length' => '4.2',
    'iso' => '400',
    'shutter_speed' => '0.016666666666667',
    'title' => '',
    'orientation' => '1',
    'keywords' =>
    array (
    ),
  ),
  'original_image' => 'test.jpg',
)

If I checkout your branch and re-run those steps, I end up with:

wp post meta get 4 _wp_attachment_metadata
array (
  'width' => 2560,
  'height' => 1920,
  'file' => '2022/12/test-scaled.jpg',
  'filesize' => 847507,
  'sizes' =>
  array (
    'medium' =>
    array (
      'file' => 'test-450x338.jpg',
      'width' => 450,
      'height' => 338,
      'mime-type' => 'image/jpeg',
      'filesize' => 41704,
    ),
    'large' =>
    array (
      'file' => 'test-1024x768.jpg',
      'width' => 1024,
      'height' => 768,
      'mime-type' => 'image/jpeg',
      'filesize' => 172363,
    ),
    'thumbnail' =>
    array (
      'file' => 'test-150x150.jpg',
      'width' => 150,
      'height' => 150,
      'mime-type' => 'image/jpeg',
      'filesize' => 8472,
    ),
    'medium_large' =>
    array (
      'file' => 'test-768x576.jpg',
      'width' => 768,
      'height' => 576,
      'mime-type' => 'image/jpeg',
      'filesize' => 104958,
    ),
    '1536x1536' =>
    array (
      'file' => 'test-1536x1152.jpg',
      'width' => 1536,
      'height' => 1152,
      'mime-type' => 'image/jpeg',
      'filesize' => 347092,
    ),
    '2048x2048' =>
    array (
      'file' => 'test-2048x1536.jpg',
      'width' => 2048,
      'height' => 1536,
      'mime-type' => 'image/jpeg',
      'filesize' => 574890,
    ),
  ),
  'image_meta' =>
  array (
    'aperture' => '1.6',
    'credit' => '',
    'camera' => 'iPhone 12 Pro',
    'caption' => '',
    'created_timestamp' => '1663191161',
    'copyright' => '',
    'focal_length' => '4.2',
    'iso' => '400',
    'shutter_speed' => '0.016666666666667',
    'title' => '',
    'orientation' => '1',
    'keywords' =>
    array (
    ),
  ),
  'original_image' => 'test.jpg',
)
wojtekn commented 1 year ago

@danielbachhuber, thanks for bringing this PR through the finish line.