Make WordPress Core

Opened 5 months ago

Last modified 6 weeks ago

#60628 assigned defect (bug)

Issues with avif image imagecreatefromavif not working. Resizing not possible.

Reported by: neo2k23's profile neo2k23 Owned by: adamsilverstein's profile adamsilverstein
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.5
Component: Media Keywords: avif has-patch has-unit-tests
Focuses: Cc:

Description

Hi

I don't know if i am at the right place but i am testing the new avif image format on wp 6.5 beta and i have issues with it. The function call imagecreatefromavif does not return a image. So from that moment on it is not possible to create a resized image.

When i upload a avif image in the media library the thumbs are not created. The function call imagecreatefromavif in the class-wp-image-editor-gd.php also does not return a image and does not create the resized thumbs. It fails.

Is this a php issue or a wordpress issue?

Image 1 shows no thumbs are created by wordpress from uploaded avif image files

https://share.zight.com/d5u2OZEA

I tried it with this image from github

https://raw.githubusercontent.com/link-u/avif-sample-images/master/kimono.avif

It simply does not create resized images.

A simple test code with the hardcoded url to the image file returns false

<?php
                $file =  'var/docs/server.tst/public/wp-content/uploads/2024/02/kimono.avif';
                if ( function_exists( 'imagecreatefromavif' ) && ( 'image/avif' === wp_get_image_mime( $file ) ) ) {
                        $image = @imagecreatefromavif( $file );
                        $valid = ( $image != FALSE );
                        if ( !$valid )  echo 'avif imaged failed to create !';
                }

To test this uplaod the image and adjust the hardcoded url to point to the correct image.

Please advice about what is going on.

Thank you for your time.

Attachments (3)

kimono.avif (83.4 KB) - added by neo2k23 5 months ago.
the image avif file used is attached
version-avif.jpg (53.7 KB) - added by neo2k23 5 months ago.
php, gdlib avif support image
60628.diff (950 bytes) - added by skithund 6 weeks ago.

Download all attachments as: .zip

Change History (14)

@neo2k23
5 months ago

the image avif file used is attached

#1 @neo2k23
5 months ago

PS i am using php 8.3 and wp 6.5-beta1-57656

@neo2k23
5 months ago

php, gdlib avif support image

#2 @skithund
5 months ago

AVIF support reported by your GD is a false positive. You're using Ubuntu 20.04, where system libgd doesn't support AVIF.

You've silenced errors from imagecreatefromavif(), but it should give a imagecreatefromavif(): AVIF image support has been disabled warning if AVIF isn't really available.

Tested AVIF support with Ubuntu 20.04 and deb.sury.org repo PHP 8.3.3: AVIF image support has been disabled warning.
Tested AVIF support with Ubuntu 22.04 and deb.sury.org repo PHP 8.3.3: works.

#3 @neo2k23
5 months ago

@skithund

I can confirm the issue is resolved installing ubuntu 22.04

Thank you for pointing me in the right direction. It is really appreciated.

Have a great day.

Please close the issue.

#4 @adamsilverstein
5 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Thanks everyone for digging into this and especially @skithund for the link about the false positive for AVIF support, I wasn't aware of that issue. Looks like it got a fix that will be in an upcoming release (8.4?)

#5 follow-up: @adamsilverstein
8 weeks ago

  • Milestone set to Awaiting Review
  • Resolution worksforme deleted
  • Status changed from closed to reopened

@neo2k23 @skithund - repoening this for a possible WordPress mitigation to the false positive.

I noticed the same issue when working on adding AVIF support to the Performance Lab plugin and came up with the following solution: https://github.com/WordPress/wordpress-develop/pull/6627

It would be great if you can test this patch in your environment and tell me if it fixes the false positive you see?

#6 in reply to: ↑ 5 @skithund
8 weeks ago

Replying to adamsilverstein:

It would be great if you can test this patch in your environment and tell me if it fixes the false positive you see?

imageavif function exists, even though actual support for AVIF isn't there 🫠

Not sure what would be the correct way to actually detect AVIF support. imagecreatefromavif gives AVIF image support has been disabled warning for any dir/file you feed to it, but it has to be valid/existing dir/file.

Reproduction steps:

Start a Docker shell in Ubuntu 23.04, which has PHP 8.1 and system GD without actual AVIF support

docker run -it ubuntu:23.04 /bin/bash

Install curl and PHP (in that docker shell) and fetch kimono.avif from this ticket

apt update && apt install -y curl php8.1-cli php8.1-gd
curl -O https://core.trac.wordpress.org/attachment/ticket/60628/kimono.avif

Test commands

root@047417a26e6d:/# php -r "var_dump(IMG_AVIF);"
int(256)
root@047417a26e6d:/# php -r "var_dump(function_exists('imageavif'));"
bool(true)
root@047417a26e6d:/# php -r "var_dump(function_exists('imagecreatefromavif'));"
bool(true)
root@047417a26e6d:/# php -r "imagecreatefromavif('/kimono.avif');"
PHP Warning:  imagecreatefromavif(): AVIF image support has been disabled
root@047417a26e6d:/# php -r "var_dump(imagecreatefromavif('/usr/bin/curl'));"
PHP Warning:  imagecreatefromavif(): AVIF image support has been disabled
Last edited 7 weeks ago by skithund (previous) (diff)

#7 @skithund
8 weeks ago

PHP versions 8.2.17 and 8.3.4 got the fix for image format detection backported, so newer PHP versions should be ok.

PHP 8.1 doesn't get the backport, since it's already out of support (receiving only security support).

#8 @skithund
7 weeks ago

This is the only reliable way to fix this, that I can think of.

// Only PHP versions 8.2.17, 8.3.4 and later have reliable image format detection
if ( version_compare( PHP_VERSION, '8.2.17', '>=' ) && ( version_compare( PHP_VERSION, '8.3.0', '<' ) || version_compare( PHP_VERSION, '8.3.4', '>=' ) ) ) {
  return ( $image_types & IMG_AVIF ) !== 0;
} else {
  return false;
}

Calling the actual imageavif() function to test for AVIF support doesn't even return false. Returns true and "AVIF image support has been disabled" warning.

From PHP doc

Return Values
Returns true on success or false on failure.
Caution However, if libgd fails to output the image, this function returns true.

#9 follow-up: @adamsilverstein
6 weeks ago

  • Keywords needs-patch needs-unit-tests avif added
  • Owner set to adamsilverstein
  • Status changed from reopened to assigned

hi @skithund - I missed this earlier when committing the fix. To start, a unit test should be able to reproduce the issue with the sample images.

@skithund
6 weeks ago

#10 in reply to: ↑ 9 @skithund
6 weeks ago

Replying to adamsilverstein:

To start, a unit test should be able to reproduce the issue with the sample images.

I'm not familiar with WordPress unit tests, but attached is a unit test I put together and it fails when ran with a broken PHP installation (eg. Ubuntu 23.04 default PHP 8.1).

imageavif() creates a zero-byte file if GD fails.

Working:

$ vendor/bin/phpunit --filter Tests_Image_Functions::test_php_avif_support
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

.                                                                   1 / 1 (100%)

Time: 00:00.206, Memory: 177.00 MB

OK (1 test, 2 assertions)

With broken PHP/GD:

$ vendor/bin/phpunit --filter Tests_Image_Functions::test_php_avif_support
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

F                                                                   1 / 1 (100%)

Time: 00:00.220, Memory: 195.00 MB

There was 1 failure:

1) Tests_Image_Functions::test_php_avif_support
filesize( '/tmp/php-gd.avif' ) should be greater than 0 bytes.
Failed asserting that 0 is greater than 0.

/app/tests/phpunit/tests/image/functions.php:1177

FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
Last edited 6 weeks ago by skithund (previous) (diff)

This ticket was mentioned in PR #6749 on WordPress/wordpress-develop by @skithund.


6 weeks ago
#11

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

Add unit test for PHP AVIF support. PHP GD library might not have actual AVIF support, even though it reports to have such.

Trac ticket: https://core.trac.wordpress.org/ticket/60628

Note: See TracTickets for help on using tickets.