Make WordPress Core

Opened 10 months ago

Last modified 21 hours 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: 6.8 Priority: normal
Severity: normal Version: 6.5
Component: Media Keywords: avif has-patch has-unit-tests early reporter-feedback
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 10 months ago.
the image avif file used is attached
version-avif.jpg (53.7 KB) - added by neo2k23 10 months ago.
php, gdlib avif support image
60628.diff (950 bytes) - added by skithund 6 months ago.

Download all attachments as: .zip

Change History (21)

@neo2k23
10 months ago

the image avif file used is attached

#1 @neo2k23
10 months ago

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

@neo2k23
10 months ago

php, gdlib avif support image

#2 @skithund
10 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
9 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
9 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
6 months 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
6 months 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 6 months ago by skithund (previous) (diff)

#7 @skithund
6 months 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
6 months 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 months 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 months ago

#10 in reply to: ↑ 9 @skithund
6 months 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 months ago by skithund (previous) (diff)

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


6 months 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

#12 @adamsilverstein
3 months ago

  • Milestone changed from Awaiting Review to 6.7

#14 @adamsilverstein
8 weeks ago

@neo2k23 - I tried improving detection in https://github.com/WordPress/wordpress-develop/pull/7465, if possible can you try testing that PR?

@skithund In my PR, I tried detecting support the same way you wrote the test, however I'm a little concerned about this bit from the docs for imageavif:
Caution
However, if libgd fails to output the image, this function returns true.

None of our test environments have GD with AVIF support as far as I can tell. I may need to adjust my PR to try to favor GD for this test as Imagick is favored when it supports AVIF.

#15 @adamsilverstein
6 weeks ago

  • Keywords early added
  • Milestone changed from 6.7 to 6.8

I believe the changes in https://github.com/WordPress/wordpress-develop/pull/7465 are ready to land, although I would love some additional testing / review.

Since we are close to release candidate for 6.7, I'm going to punt this to 6.8 and try to land it early so we get wider testing.

#16 follow-up: @neo2k23
6 weeks ago

tried testing but the server just works fine with avif. It resizes perfectly. Tried a new server with php 8.3 and cpanel. also works just fine. What should i do next.

#17 in reply to: ↑ 16 ; follow-up: @adamsilverstein
3 days ago

  • Keywords reporter-feedback added

Replying to neo2k23:

tried testing but the server just works fine with avif. It resizes perfectly. Tried a new server with php 8.3 and cpanel. also works just fine. What should i do next.

To calrify @neo2k23 - can you still reproduce the issue you opened the ticket for?

ie. is this still true? "The function call imagecreatefromavif does not return a image. So from that moment on it is not possible to create a resized image."

Last edited 3 days ago by adamsilverstein (previous) (diff)

#18 in reply to: ↑ 17 @neo2k23
21 hours ago

Replying to adamsilverstein:

Replying to neo2k23:

tried testing but the server just works fine with avif. It resizes perfectly. Tried a new server with php 8.3 and cpanel. also works just fine. What should i do next.

To calrify @neo2k23 - can you still reproduce the issue you opened the ticket for?

ie. is this still true? "The function call imagecreatefromavif does not return a image. So from that moment on it is not possible to create a resized image."

i cant test it anymore on the old server as i ditched it. I am running now on ubuntu 22.04 or higher and having no issues.

I tested on serveral other linux servers and they all work fine. My issue was related to ubuntu 20.04 server

Note: See TracTickets for help on using tickets.