php-src icon indicating copy to clipboard operation
php-src copied to clipboard

AVIF export very bad quality

Open WonderFlags opened this issue 4 years ago • 11 comments

Description

The following code:

list($width,$height)=getimagesize("test.jpeg");
$src=imagecreatefromjpeg("test.jpeg");
$tmp1=imagecreatetruecolor(500,500);
imagecopyresampled($tmp1,$src,0,0,0,0,500,500,$width,$height);
imageavif($tmp1,"test.avif","70");
<?php

Resulted in this output: DFHX1

But I expected this output instead: kcy5r

As you can see, the image degradation is... astounding. No matter what functions i use beforehand (imagepalettetotruecolor, imagealphablending, imagesavealpha, imagesetinterpolation, imageantialias...) to enhace it, the result is always this poor.

Any suggestions? Is there something wrong with GD2 itself?

  • Same code with imagewebp works flawlessly, for example

PHP Version

PHP 8.1.3

Operating System

Windows 10 x64

WonderFlags avatar Feb 25 '22 20:02 WonderFlags

good morning,

this is not something that GD control. AVIF is based on video encoding and compression and can be very destructive (used mainly with photographiesl). As you mentioned, other codec provides better results.

The use of palette, or other unrelated functions does not help here.

You may try to increase the quality (there is a lossless model).

also play with the avif console tool to test may help.

On Sat, Feb 26, 2022, 3:27 AM Benji Frugoni @.***> wrote:

Description

The following code:

list($width,$height)=getimagesize("test.jpg");$tmp1=imagecreatetruecolor(500,500);imagecopyresampled($tmp1,$src,0,0,0,0,500,500,$width,$height);imageavif($tmp1,"test.avif","70");<?php

Resulted in this output:

DFHX1

But I expected this output instead:

kcy5r

As you can see, the image degradation is... astounding. No matter what functions i use beforehand (imagepalettetotruecolor, imagealphablending, imagesavealpha, imagesetinterpolation, imageantialias...) to enhace it, the result is always this poor.

Any suggestions? Is there something wrong with GD2 itself? PHP Version

PHP 8.1.3 Operating System

Windows 10 x64

— Reply to this email directly, view it on GitHub https://github.com/php/php-src/issues/8154, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACE6KESJFY7E52DDXU6H3DU47Q2DANCNFSM5PLG35EQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

pierrejoye avatar Feb 26 '22 02:02 pierrejoye

Thank you Pierre ofr trying to help,

I actually tried that, if you set the quality higher that 80 it starts doing weird things like making the whole picture pink

Result with Q=100 17068e396ab7f6f5b970

WonderFlags avatar Feb 26 '22 11:02 WonderFlags

could you attach the original picture please?

also list your configuration, php, bundled gd or external (with version), operating system and versions of libavif please?

On Sat, Feb 26, 2022, 6:06 PM Benji Frugoni @.***> wrote:

Thank you Pierre ofr trying to help,

I actually tried that, if you set the quality higher that 80 it starts doing weird things like making the whole picture pink

Result with Q=100 [image: 17068e396ab7f6f5b970] https://user-images.githubusercontent.com/11628948/155840844-ab133359-1ff3-43a0-87e4-115860f77d42.png

— Reply to this email directly, view it on GitHub https://github.com/php/php-src/issues/8154#issuecomment-1052015567, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACE6KC5ITNKI2SXRTMLWQDU5CX3HANCNFSM5PLG35EQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

pierrejoye avatar Feb 26 '22 12:02 pierrejoye

Wamp 3.2.7 PHP 8.1.3 Bundled GD (2.1.0 compatible) Windows 10 x64 libavif... not sure, whatever comes bundled in the GD. I imagine it is libavif-0.8.2

Original image 1945x1945bb

WonderFlags avatar Feb 26 '22 17:02 WonderFlags

It seems something is not right in this build. @cmb69

Using libgd master and libavif 0.8.4:

#include "gd.h"
#include "gdtest.h"
#include <stdio.h>

int main()
{
	gdImagePtr im;
	FILE *fp;

	im = gdImageCreateTrueColor(10, 10);
	fp = gdTestFileOpen("avif/phpgh_8154.jpeg");
	if (!gdTestAssert(fp != NULL)) {
		printf("failed to open src file.\n");
		return gdNumFailures();
	}

	im = gdImageCreateFromJpeg(fp);
	fclose(fp);
	if (!im) {
		printf("failed1.\n");
	}
	if (!gdTestAssert(im != NULL)) {
		printf("failed.\n");
		return gdNumFailures();
	}

	fp = fopen("out.avif", "wb");
	if (!gdTestAssert(fp != NULL)) {
		printf("failed to open out file.\n");
		return gdNumFailures();
	}

	gdImageAvif(im, fp);
	fclose(fp);

	return gdNumFailures();
}

out

pierrejoye avatar Feb 27 '22 04:02 pierrejoye

I don't see (relevant) implementation differences between the bundled GD and external libgd but a few syntax sugars changes.

I think upgrading libavif for the windows may fix that.

pierrejoye avatar Feb 27 '22 08:02 pierrejoye

On Windows, PHP-8.1 with libavif-0.9.0 (which is used by PHP 8.1.3), the OP script yields for me (packaged as ZIP to avoid any potential issues with converting to PNG): gh8154.zip In other words, I cannot reproduce the bad quality that has been reported.

If I use 100 as quality, which is supposed to trigger lossless encoding, I get a good result (without any color shift to reddish).

I wonder whether there is some other libavif.dll grabbed up by (or even shipped with) Wamp 3.2.7, or whether there is an issue with the PNG conversion.

cmb69 avatar Feb 27 '22 09:02 cmb69

Closing issue. Indeed libavif of my Windows 10 x64 was causing the issue. Good for PHP!

WonderFlags avatar Feb 27 '22 11:02 WonderFlags

I am experiencing the same issue. For me, I am seeing color shifting with all compression qualities. On a scaled image, I can save as JPG and WEBP fine, but when saved as AVIF there is a green line at the bottom of images, and text is messed up, as if the green channel is misaligned with the other color channels. When saving higher quality (including 100) the images, which are generally dark color backgrounds (greys and blacks) are completely miscolored with green and pink tones.

The AVIF files created using php are completely useless for me.

Windows 10 64-bit (build 19044 AMD64) PHP Version => 8.1.16 AND 8.2.3 (thread safe) GD Version => bundled (2.1.0 compatible)

How do I find my libavif version, and upgrade it?

AVIF quality 100 (colors completely corrupted) mountains 576 avif-100

AVIF quality 80 (last line is green, and text color channels misaligned) mountains 576 avif-80

AVIF quality -1 (last line is green, and text color channels misaligned) mountains 576 avif--1

JPG (looks fine) mountains 576

WEBP (looks fine) mountains 576 webp

  • note: github wont allow avif/webp, so I had to convert all to png

bradts1 avatar Mar 06 '23 00:03 bradts1

Issue #1 - There is a rounding bug that affects AVIF output only.

After playing with this a bit, I've determined that part of the issue with misaligned color channels appears to be due to rounding errors. I found the same issue affected both GD and magick command line output. So it appears to be in the underlying avif libraries, and not an issue with php per se.

I am rescaling a 960x526 image down to 576 pixel width using imagescale() without a height parameter so that GD calculates the proper height to maintain aspect. GD calculates this as 315 pixels, which works perfectly fine with both JPG and WEBP output.

The images are manipulated beforehand, a single time, and only then saved out to three different file formats, so given that JPG and WEBP handle this perfectly fine, but AVIF doesn't, I will assume this is a bug in the AVIF system and not a problem with my manipulations, as there are not separate manipulations done for the AVIF output.

Reviewing the actual scaling math myself I found that the scaled height is not an even integer, and is actually 315.6. GD appears to round this down, which is expected because rounding up requires stretching the image to fill an extra pixel. If I manually specify the height on my rescale rounded up to 316 pixels the issue with the green line disappears and the text looks crisp (for both GD and magick).

Additionally, I found the misalignment issue only shows up when quality is set at 89 or lower. At quality=90: the image looks good at either height. At quality=89: the green line only shows up only when the height is 315 (either hard coded or automatically calculated), not when it is manually set to 316.

Quality=90

height=316 (good) mountains 576 316q90-avif

height=315 (good) mountains 576 315q90-avif

Quality=89

height=316 (acceptable, though much lower quality than 90) mountains 576 316q89-avif

height=315 (misaligned color channels) mountains 576 315q89-avif

Issue #2 - corrupted colors

And, as for the completely mis-colored output. This only happens when I specify an output quality of 100 on AVIF images. imageavif( $image, $outfile, 100 );

If I specify an output quality of 99, the image looks perfect. So, this too appears to be a math related issue.

GD saved AVIF with quality=100. mountains 576 avif

GD saved AVIF with quality=99. mountains 576

So, it appears, the underlying AVIF library still has some bugs to be worked out.

bradts1 avatar Mar 06 '23 04:03 bradts1

@bradts1 please create a new issue, not many people are reading comments on closed issues. And if you need to attach unsupported files put them into zip or tar.gz.

KapitanOczywisty avatar Mar 06 '23 10:03 KapitanOczywisty