Skip to content

Conversation

@wojtekn
Copy link
Contributor

@wojtekn wojtekn commented Dec 15, 2022

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 #141.

Fixes #130

@danielbachhuber
Copy link
Member

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',
)

@danielbachhuber danielbachhuber added this to the 2.0.16 milestone Dec 16, 2022
@danielbachhuber danielbachhuber changed the title Fix regenerate command that deletes other sizes meta Avoid deleting meta for other sizes when running wp media regenerate with --image_size Dec 16, 2022
Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍 Thanks for working on this, @wojtekn.

I made a couple of changes:

  • Moved your tests to a scenario specific to --image_size= and --only-missing.
  • Reverted your test changes associated with the random failure. We can discuss that in #169

@danielbachhuber danielbachhuber merged commit b68d71e into wp-cli:main Dec 16, 2022
@wojtekn wojtekn deleted the fix/regenerate-command-that-deletes-other-sizes-meta branch December 19, 2022 10:07
@wojtekn
Copy link
Contributor Author

wojtekn commented Dec 19, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regenerate with --image_size deletes all other sizes from postmeta

2 participants