wp-media / imagify-plugin

Speed up your website with lighter images without losing quality.
https://imagify.io
73 stars 26 forks source link

Fatal error when having image size with integer name #888

Closed wordpressfan closed 1 month ago

wordpressfan commented 6 months ago

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

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 );
    } );
  2. Optimize any image and wait till it's optimized for this new size.
  3. Open any post and choose the featured image
  4. 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

saranshj4 commented 5 months ago

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 commented 5 months ago

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

saranshj4 commented 5 months ago

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

saranshj4 commented 4 months ago

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

markonikolic985 commented 3 months ago

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;

saranshj4 commented 2 months ago

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