golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.77k stars 17.64k forks source link

x/image/webp: support for WebP Encoding #45121

Open ainsleyclark opened 3 years ago

ainsleyclark commented 3 years ago

Hi there,

Currently we are able to decode webp images, which is great. But why can't we encode them?

Given that PSI is such a crucial factor for getting websites to rank, it's important that any server should be able to convert and serve webp images.

I have to jump through hoops at the moment and use the webp executable which is far from ideal.

Please please add an encoder!

Many many thanks.

ALTree commented 3 years ago

cc @nigeltao

ianlancetaylor commented 3 years ago

A WebP encoder would be a lot of work. If someone wants to write one, that would be great. But as far as I know nobody is working on that.

ainsleyclark commented 3 years ago

That's a shame, whilst I understand that it may be a bit of work, it feels like it should be out of the box considering webp and go are both made by you guys!

Adding C bindings when converting to webp using third party packages can be messy, as well as using the executables.

I would love to offer a hand to contribute to the /x/image library but wouldn't know where to start.

nigeltao commented 3 years ago

I understand that it may be a bit of work

It's not a bit of work. As Ian said, it's a lot of work. :-)

DemiMarie commented 3 years ago

That's a shame, whilst I understand that it may be a bit of work, it feels like it should be out of the box considering webp and go are both made by you guys!

Adding C bindings when converting to webp using third party packages can be messy, as well as using the executables.

The good news is that image encoding is much less of a security minefield than image decoding is, simply because the input format (a bitmap) is far, far simpler.

That said, have you considered ImageFlow? It’s free software (AGPL) available as both a daemon and a library. Commercial licenses are also available.

ainsleyclark commented 3 years ago

Hi there,

Thank you for the comments, I'm just wondering if this is being considered for a feature request?

Many thanks.

ivanjaros commented 2 years ago

Yeah, this is so weird to have a reader but not writer in Go when Go and WebP are both Google's projects. Especially when there was all thus fuss about replacing "outdated" formats with modern Web* yet not providing native tools to do so. c'est la vie, i guess.

FlavioCFOliveira commented 2 years ago

Please consider this request

ivanjaros commented 2 years ago

Just thinking out loud here but instead of writing a native Go writer, why not just build it into Go itself? Again, both are Google projects. And while at it, having native support for vp8, vp9, av1... VIDEO would be a game-changer. It could be part of the Go build process where the additional code would get built into the executable only if used(called).

NinoM4ster commented 2 years ago

Starting a new website project with a Go backend and was planning on using WebP for image thumbnails and it's really sad to see that there's no native encoder.. :(

I would really like to avoid using C bindings (due to performance and memory safety concerns), but it seems that's the only option at the moment, except for executing the official webp binary, which is also not the best idea, since you'd have to either write the file to disk or come up with a complicated virtual FS just for it.

I also considered using Rust, but that's way too advanced for me.

Edit: I found https://github.com/nickalie/go-webpbin which seems quite decent. haven't tested it yet though.

ainsleyclark commented 2 years ago

@ivanjaros AV1 support would be 🔥

ivanjaros commented 2 years ago

@ivanjaros AV1 support would be 🔥

indeed. having native support for multimedia instead of having to call ffmpeg externally or some crazy cgo bridging would be sweet.

ex-tag commented 2 years ago

WebP is supported by all major browsers https://caniuse.com/?search=webp

This is currently blocking me from moving to WebP

ex-tag commented 2 years ago

I've tried to use other WebP encoder / decoders for Golang, which there are very few. None of them are pure Golang, or officially supported by the Standard Library in the way Gif, Jpeg, or Png have support.

Lmao, that Golang has no WebP support, when Google has the code and engineers for both in-house.

ex-tag commented 2 years ago

@ainsleyclark - I don't think AVIF is widely supported, so I can understand why that is not yet included in the Golang Standard Library.

ainsleyclark commented 2 years ago

@ex-tag considering the shift with Intel and Nvidia recently and major providers using AV1 and particularly AVIF for websites, future steps for supporting these fantastic encoders would be hugely beneficial for the stdlib. But can appreciate its not a small job.

wjkoh commented 9 months ago

Could go-libwebp by Jack Mordaunt offer a potential solution? I'm aware that rewriting libwebp in Go would be quite time-consuming, but Jack Mordaunt utilized ccgo and was able to port the libwebp C code to native Go code, making it functional, if I understand correctly. I'm not an expert, and I'm unsure of any potential risks this approach may present, but I wanted to share the idea. @JackMordaunt

dastanaron commented 9 months ago

I think there is a better solution. Example may use c-bindings from library on C language.

JackMordaunt commented 9 months ago

Could go-libwebp by Jack Mordaunt offer a potential solution? I'm aware that rewriting libwebp in Go would be quite time-consuming, but Jack Mordaunt utilized ccgo and was able to port the libwebp C code to native Go code, making it functional, if I understand correctly. I'm not an expert, and I'm unsure of any potential risks this approach may present, but I wanted to share the idea. @JackMordaunt

Thanks for the reference!

Yes, it's functional. However there's a lot of downside to the transpilation approach which bars it from being anything official.

For anyone interested, go-libwebp also allows calling into dynamic libraries if available. However this uses purego to make those calls without requiring cgo. purego is also an experimental tool.