Skip to content

Unable to Write AVIF to Buffer Using .avif Suffix #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
freshleafmedia opened this issue Apr 6, 2023 · 2 comments
Closed

Unable to Write AVIF to Buffer Using .avif Suffix #197

freshleafmedia opened this issue Apr 6, 2023 · 2 comments

Comments

@freshleafmedia
Copy link

freshleafmedia commented Apr 6, 2023

If I try to re-encode an image from JPEG to AVIF everything works as expected:

$image = \Jcupitt\Vips\Image::newFromFile('path/to/image.jpg');
$image->writeToFile('path/to/image.avif');

However if I try to write the AVIF image to a buffer I get an error:

$image = \Jcupitt\Vips\Image::newFromFile('path/to/image.jpg');
$buffer = $image->writeToBuffer('.avif');
libvips error: VipsForeignSave: ".avif" is not a known buffer format

The difference seems to be in the vips_foreign_find methods:

FFI::vips()->vips_foreign_find_save('.avif'); // "VipsForeignSaveHeifFile"
FFI::vips()->vips_foreign_find_save_buffer('.avif'); // null

I can get it to do it if I use the following workaround:

$image = \Jcupitt\Vips\Image::newFromFile('path/to/image.jpg');
$buffer = $image->writeToBuffer('.heif', ['compression' => 'av1'])

Am I doing something wrong or is this expected behaviour?

@jcupitt
Copy link
Member

jcupitt commented Apr 7, 2023

Hello @freshleafmedia,

This PR should fix your problem:

#191

It needs merging, I've been too busy to help sort it out, unfortunately. Maybe this weekend.

Background: libvips used to support file and memory read and write. In 8.10 we added a new virtual IO system based on two new classes called source and target that lets you read and write from any IO-like object. For example, you can do this:

$ vips copy k2.jpg .avif | cat > x.avif
$ file x.avif
x.avif: ISO Media, AVIF Image

Very handy for things like S3 -- you can write directly to a bucket over a pipe.

The old buffer/file API has been mostly moved onto this new thing, but there are a few holes, like the one you've found. Adding proper source / target support to php-vips would fix these problems, and add useful new functionality.

@jcupitt
Copy link
Member

jcupitt commented Jun 30, 2023

I've made a PR for this: #207

Any testing very welcome! I'll close this issue -- any discussion should continue on that PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants