wp-cli / media-command

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

Images skipped due to "no regeneration needed" have outdated meta data #208

Open jamieburchell opened 2 weeks ago

jamieburchell commented 2 weeks ago

Bug Report

Describe the current, buggy behavior

I changed the "medium" image size in the WordPress media settings from 360x360px to 400x400px and ran the following command:

wp media regenerate --missing-only

The images were regenerated, but some were skipped as "No thumbnail regeneration needed". I later discovered that the meta data in the image attachments for those that were skipped still referenced the 360px version of the "medium" sized image. I identified all affected image attachments and then ran:

wp media regenerate [attachment-ids]

This successfully updated the attachment meta and removed the reference to the 360px version of the image. I believe this occurred on images that were <= 400px.

It appears that if the command sees that the original image is the same size or less than the target, it is skipped but in doing so the meta is not updated to reference the original image.

Describe how other contributors can replicate this bug

Example meta (note 360px image still referenced as "medium" size):

a:6:{s:5:"width";i:400;s:6:"height";i:400;s:4:"file";s:13:"2024/11/a.jpg";s:8:"filesize";i:222592;s:5:"sizes";a:3:{s:6:"medium";a:5:{s:4:"file";s:13:"a-360x360.jpg";s:5:"width";i:360;s:6:"height";i:360;s:9:"mime-type";s:10:"image/jpeg";s:8:"filesize";i:49522;}s:9:"thumbnail";a:5:{s:4:"file";s:13:"a-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";s:8:"filesize";i:9796;}s:5:"Small";a:5:{s:4:"file";s:13:"a-250x250.jpg";s:5:"width";i:250;s:6:"height";i:250;s:9:"mime-type";s:10:"image/jpeg";s:8:"filesize";i:25537;}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}

Additionally, the old 360px medium size image file is not deleted from the filesystem.

Example meta after running wp media regenerate [attachment-id] on affected image (note 360px version removed from meta and filesystem):

a:6:{s:5:"width";i:400;s:6:"height";i:400;s:4:"file";s:13:"2024/11/a.jpg";s:8:"filesize";i:222592;s:5:"sizes";a:2:{s:9:"thumbnail";a:5:{s:4:"file";s:13:"a-150x150.jpg";s:5:"width";i:150;s:6:"height";i:150;s:9:"mime-type";s:10:"image/jpeg";s:8:"filesize";i:9796;}s:5:"Small";a:5:{s:4:"file";s:13:"a-250x250.jpg";s:5:"width";i:250;s:6:"height";i:250;s:9:"mime-type";s:10:"image/jpeg";s:8:"filesize";i:25537;}}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}

Describe what you would expect as the correct outcome

Image meta should be updated.

Let us know what environment you are running this on


OS:     Linux 5.14.0-427.40.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Oct 16 14:57:47 UTC 2024 x86_64
Shell:  /sbin/nologin
PHP binary:     /usr/bin/php
PHP version:    8.2.25
php.ini used:   /etc/php.ini
MySQL binary:   /bin/mysql
MySQL version:  mysql  Ver 15.1 Distrib 10.5.22-MariaDB, for Linux (x86_64) using  EditLine wrapper
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /var/www/vhosts/X/releases/194
WP-CLI packages dir:
WP-CLI cache dir:       /usr/share/httpd/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:  /var/www/vhosts/X/releases/194/wp-cli.yml
WP-CLI version: 2.11.0```
dilipom13 commented 2 weeks ago

The above command is working for me again I will review it.