Some suggestions:
If you want to try out the Image Editor API you can try to replace
imagejpeg( $resource, $image, 35 );
with:
$editor = wp_get_image_editor( $image );
if ( ! is_wp_error( $editor ) )
{
$editor->set_quality( 35 );
$editor->save( $image );
}
unset( $editor );
Also try to test e.g. these parts:
$resource = imagecreatefromjpeg( $image );
if( false !== $resource )
{
imagejpeg( $resource, $image, 35 );
imagedestroy( $resource );
}
in a standalone PHP script, to make sure it’s not a PHP or GD related issue.
PS: Here’s a different approach where we try to modify the image quality before the image size is generated.
Testing the plugin
Let’s check the plugin from the main question.
Test install
- WordPress version 4.9-alpha-40917
- PHP 7.0.19
Here’s the generated hello-image
(670×300) size, of the SMALL (670 × 377) image, from the question, where we set the quality to 40:
imagejpeg( $resource, $image, 40 );
Here’s the same but with the quality set to 0:
imagejpeg( $resource, $image, 0 );
So it looks like we don’t get the same behavior as described in the question, that when the quality is set to 40, it becomes 0, for the hello-image
size of the 670×377 original image.