-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Added suport for AVIF to getimagesize(). #5127
Conversation
Is there any publicly available spec for this? [2] doesn't seem to correlate with this code, the relevant parts seem to depend on HEIF instead. |
Just to note, please rebase onto master instead of merging master into your feature branch. |
I'm sorry |
I used |
Perfect :) |
Libgd just added support for avif/heif via libavif and libheif in 2.3.2 release. There's related feature request https://bugs.php.net/bug.php?id=78832 Looks both libraries needs dependencies evaluation but maybe it's enough to add |
Hey, I'm excited to see this! I'm the one who wrote the PR for AVIF support in libavif. I was looking at adding this support for |
@morsssss Thank you!! |
Hi @y-hashida ! I took a look here - but I admit that the details about the contents of the header file are out of my range of experience. I wonder if @wantehchang would be familiar with this... |
Or perhaps @joedrago ? Joe, if you've got a moment, could you help make sure this is being done properly? |
I'm no expert on PHP, but this implementation certainly makes a lot of assumptions. It appears to be blindly looking for fourcc codes like Long story short, this is certainly "wrong", but I don't know what level of accuracy you are looking for here. For the most common single-image, color-item-happens-to-come-before-alpha-item, mdat-comes-after-meta-box, no-false-positives-in-the-AV1-payload AVIF, this will likely find the right numbers. It could occasionally be spectacularly wrong though, and I don't know if that is worth the extra effort here, or if that could simply be warned in the docs and people would have to protect themselves accordingly. |
Thanks, @joedrago ! @y-hashida , we're looking for another way to do this... |
If it is okay to use libavif, then the avifDecoderParse() function can be used to get the width, height, bit depth, and the number of channels from the AVIF image container. |
It would be quite convenient to use libavif! I just fear that the PHP team won't to include that as an additional dependency - especially as none of the other supported image formats come with a similar requirement. If we didn't have another solution, maybe a compromise would involve including the AVIF functionality only if libavif was found during build. I see that sort of mechanism for other image formats in the GD config here - but I don't know if that propagates into this part of the codebase. |
@cmb69 , would you like to weigh in here? |
Offline discussion with @joedrago indicates that it's pretty hard to get the image's characteristics consistently without using libavif. And no one loves the idea of conditionally including libavif. However, it is not hard to confirm that the image type is actually AVIF. And, at least, that's all that CMSs like Joomla, WordPress, and Drupal use this function for. @cmb69 proposed that we let @y-hashida , how do you feel about this? I wish I had a better solution - but this is a start! And, as @cmb69 points out, people with libavif would be easily able to get the image's dimensions once it was decoded. |
Sorry for the late reply.
I see! Let me confirm to make sure.
|
I want to follow the latest version of php, so I will use |
79ce22d
to
d9b7186
Compare
Actually... I believe the idea is to have |
@joedrago has a nice algorithm which can be borrowed from libavif. I could post it here, but I'd like to give him the chance to do so himself first if he wants to |
(Reworded and tweaked from a private email chain:) Detecting if a file is self-identifying as AVIF is not too tough, due to various restrictions with BMFF's FileTypeBox. I have an obvious bias here, but this is a fairly simple C routine from libavif called avifPeekCompatibleFileType() that shouldn't be too hard to re-implement: https://github.com/AOMediaCodec/libavif/blob/master/src/read.c#L2210 These two snippets are from the BMFF standard (ISO 14496-12), of which AVIF is built on top of: The general idea here is that the first "box" inside of the file must be a FileTypeBox, which means you can simply read the box's header (4 bytes for size, 4 bytes for type), and if the type isn't ftyp, it is not an AVIF. If you do see To recap, you'd check to see from the first 8 bytes if the file even smells a little bit like BMFF, and if so, read in the rest of the box (based on the length supplied by the first 4 bytes) to see if |
btw @y-hashida I'm happy to take this on, but wanted to give you the chance to adjust your own PR first! I didn't want to try to take this over if you preferred to do it yourself. |
Thanks for your concern. |
No, there isn't a limit; in fact, there are mechanisms (see the implementation of Box) to allow for a 64bit size! That said, I think it is quite reasonable to put an artificial limit on your Very, very minor aside: I glanced at your code, and one minor note I'd make is changing the name of the helper function |
Thanks, @joedrago ! Now I see why that implementation is written in that way... making the change, and giving credit where credit is due :) |
See #7091 |
It could be closed? The current work in #7253 Meantime used to try build
at the build time
Also getting
|
php-src/ext/gd/libgd/gd_avif.c Line 173 in a3f5b11
looks wrong. Is it supposed to check offset < 0 ?
|
@cmb69 Both size < 0 and offset < 0 don't make sense, as they're both unsigned. We should probably cherry-pick libgd/libgd@f6a111c? |
@nikic, oh, right! |
Filed PR #7296 |
Source - libgd/libgd@f6a111c Related to #5127 (comment)
Upcoming PHP 8.1 is announced with full AVIF support. Do any of you know why this isn't supported yet? |
getimagesize() should recognize AVIF files. Can you please provide a failing AVIF? |
Sorry for being unclear. Essential to have are key 0 and key 1 for width and height. These are converted jpegs to avif format by ImageMagick and/or GD with the new PHP 8.1-RC2 avif methods. Array
(
[0] => 0
[1] => 0
[2] => 19
[3] => width="0" height="0"
[mime] => image/avif
) BTW, do they have a [bits] key like webp files? |
Hi @ophian . See this comment above... unfortunately it wasn't practical for The good news is that a group of folks at Google who work with AVIF are working right now on a function that would in fact be able to detect image size. If they succeed, I'll be submitting a PR here :) |
@morsssss what's left here? this PR looks outdated |
Oh, we already merged PR #7091. Are there, maybe, some useful improvements in this PR? Otherwise the PR should be closed. |
Indeed, this PR has been superseded by #7091 and others. It can be closed. |
Closing per @morsssss comment above. Thank you for the PR @y-hashida nonetheless! |
Yes, many thanks to @y-hashida for starting this journey! |
I'm sorry I couldn't continue development to this PR. |
@morsssss |
@cmb69 , the work in progress is on the order of 700-800 lines of code. It takes some work to find the size of an AVIF image reliably :) Does it feel reasonable to add this to PHP? It's not much compared to the existing codebase, but it's a lot more than we have for other images. |
@morsssss, if that is a stand-alone library, we can bundle temporarily until it is available on the most relevant systems. We should support scenarios where the libraray is not available (falling back to the existing code). It's likely a good idea to discuss that on the internals mailing list, though. |
AVIF is a compression format for images derived from the AV1 video format.
WebP's file reduction is -24.5%, whereas AVIF's file reduction is -61.1%, so AVIF format is enable highly efficient compression[1].
In this pull request, for introducing AVIF, We added AVIF support for getimagesize() and used image/avif as MIME type.
References
[1]:https://aomedia.org/wp-content/uploads/2019/11/CyrilConcolato_Netflix-AVIF-AOM-Research-Symposium-2019.pdf
[2]:https://aomediacodec.github.io/av1-avif
[3]:https://www.iso.org/standard/66067.html