imagify-plugin icon indicating copy to clipboard operation
imagify-plugin copied to clipboard

Fatal error when having image size with integer name

Open wordpressfan opened this issue 1 year ago • 6 comments

Before submitting an issue please check that you’ve completed the following steps:

  • Made sure you’re on the latest version Yes
  • Used the search feature to ensure that the bug hasn’t been reported before Yes

Describe the bug

In a customer's site we face the following error:

[23-May-2024 08:15:14 UTC] PHP Fatal error:  Uncaught TypeError: strpos() expects parameter 1 to be string, int given in /home/customer/www/staging2.xxx.com/public_html/wp-content/plugins/imagify/classes/Optimization/Process/AbstractProcess.php:1641
Stack trace:
#0 /home/customer/www/staging2.xxx.com/public_html/wp-content/plugins/imagify/classes/Optimization/Process/AbstractProcess.php(1641): strpos(200, '@imagify-webp')
#1 [internal function]: Imagify\Optimization\Process\AbstractProcess->Imagify\Optimization\Process\{closure}(200)
#2 /home/customer/www/staging2.xxx.com/public_html/wp-content/plugins/imagify/classes/Optimization/Process/AbstractProcess.php(1642): array_filter(Array, Object(Closure))
#3 /home/customer/www/staging2.xxx.com/public_html/wp-content/plugins/imagify/inc/functions/admin-ui.php(100): Imagify\Optimization\Process\AbstractProcess->is_full_next_gen()
#4 /home/customer/www/staging2.xxx.com/public_html/wp-content/plugins/imagify/inc/functions/admin-ui.php(529): g in /home/customer/www/staging2.xxx.com/public_html/wp-content/plugins/imagify/classes/Optimization/Process/AbstractProcess.php on line 1641

and this happens exactly here:

https://github.com/wp-media/imagify-plugin/blob/a5b782019155e99326c7fddf1b031750b0c50c82/classes/Optimization/Process/AbstractProcess.php#L1641

In the PR (#877) we fixed similar issue when the image size name is not integer, but we once the image is optimized we save the sizes in the attachment metas to be grabbed later on the mentioned line (as far as I understand) but in php if the array has numeric keys even if u added them as strings, this will be cast to be integer then it throws the previously mentioned fatal error.

To Reproduce Steps to reproduce the behavior:

  1. Add the following snippet
add_action( 'after_setup_theme', function() {
    add_image_size( '200', 200, 200 );
} );
  1. Optimize any image and wait till it's optimized for this new size.
  2. Open any post and choose the featured image
  3. once media modal is opened, fatal error should be thrown and images not loaded in the modal.

Expected behavior Images should load without a problem.

I fixed this by casting the $key variable here:

https://github.com/wp-media/imagify-plugin/blob/a5b782019155e99326c7fddf1b031750b0c50c82/classes/Optimization/Process/AbstractProcess.php#L1641

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

Acceptance Criteria (for WP Media team use only) Clear instructions for developers, to be added before the grooming

wordpressfan avatar May 23 '24 09:05 wordpressfan

https://secure.helpscout.net/conversation/2611409187/494381/ https://secure.helpscout.net/conversation/2596568595/491337/ https://secure.helpscout.net/conversation/2606649666/493399/ https://secure.helpscout.net/conversation/2588163786/489827/ https://secure.helpscout.net/conversation/2586375548/489522/ https://secure.helpscout.net/conversation/2536803121/480046/

saranshj4 avatar Jun 24 '24 11:06 saranshj4

https://wordpress.org/support/topic/error-deleting-old-images/

saranshj4 avatar Jun 24 '24 11:06 saranshj4

imagify.2.2.2.zip A patched version that resolved the issue on some user sites.

saranshj4 avatar Jun 24 '24 12:06 saranshj4

https://secure.helpscout.net/conversation/2642186273/500616/

saranshj4 avatar Jul 04 '24 12:07 saranshj4

Another relevant ticket with proposed solution https://secure.helpscout.net/conversation/2680413623/507765?folderId=676697

My hosting support was able to determine the issue. It seems that the Imagify plugin is not fully compatible with your version of the WordPress core and/or PHP 8.2. The Wordpress error dump identified line 1641 in /home/cypresspainting/www/www/wp-content/plugins/imagify/classes/Optimization/Process/AbstractProcess.php. They did come up with a fix for this problem Replaced return strpos( $key, $this->format ) === false; with this: return is_string($key) && strpos( $key, $this->format ) === false;

markonikolic985 avatar Aug 16 '24 05:08 markonikolic985

https://secure.helpscout.net/conversation/2706721399/512163/

saranshj4 avatar Sep 13 '24 12:09 saranshj4