英文:
Process image given multiple URL parameters in Go
问题
我可能更喜欢使用pressly/chi,但我猜这没有什么区别。假设给定这样的输入URL:example.com/Jd8saD.jpg?resize=420x320&fit=crop&rotate=90
,那么它会因为r.Get("/:image", ImageGET)
而进入以下的GET函数:
func ImageGET(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("resize") != "" {
// 做一些处理
}
if r.URL.Query().Get("crop") != "" {
// 做一些处理
}
if r.URL.Query().Get("watermark") != "" {
// 做一些处理
}
// 其他处理
}
现在,我的问题是,我应该如何设计处理图像的函数,以便它能正确高效地处理所有内容?我不希望你编写处理调整大小的代码,但这些函数应该是什么样子的?也许是这样的:
func Resize(size string) (imgfile?, error) {
// 调整大小的处理
}
那么返回的imgfile
应该是什么?是一个包含一些相关图像信息的结构体吗?
英文:
I would probably prefer to use pressly/chi, but I guess it makes no difference. I imagine given an input URL like this example.com/Jd8saD.jpg?resize=420x320&fit=crop&rotate=90
, then it would go to the following GET function because of r.Get("/:image", ImageGET)
:
function ImageGET(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("resize") != "" {
// do something
}
if r.URL.Query().Get("crop") != "" {
// do something
}
if r.URL.Query().Get("watermark") != "" {
// do something
}
etc
}
Now, my question is, how should I design whatever function does the image processing so that it will process everything properly and efficiently? I don't expect you to write code that will handle the resizing, but how would these functions look like? Perhaps:
function Resize(size string) (imgfile?, error) {
// the resize stuff
}
What would that returned imgfile
even be? A struct containing some relevant img info?
答案1
得分: 1
可能,imgfile
将满足 image.Image
接口,而不是保存在磁盘上的数据(即实际的 jpg 文件)。
Image
是一个有限的矩形颜色网格,颜色值来自于一个颜色模型。
许多第三方的 golang 图像库使用 image.Image
来操作图像。
我会在 imageGET
函数中使用一个标准的 image.Image
接口,通过文件名检索(读取到内存中),并根据查询进行修改。你还可以查看标准库中的 jpeg golang 库。
function ImageGET(w http.ResponseWriter, r *http.Request) {
// GetJPEGImageFromFileSystem 必须解码 os.File 的内容,并返回一个 golang image.Image 接口
img, _ := GetJPEGImageFromFileSystem(r.URL.Query().Get("filename"))
if r.URL.Query().Get("resize") != "" {
// 如果需要调整大小,在内存中覆盖 image.Image,但不修改磁盘上的图像文件
img, _ = Resize(img, r.URL.Query().GET("resize"))
}
// 等等...
}
function Resize(img image.Image, size string) (image.Image, error) {
// 调整大小的代码 -- 或者,可以直接在原始处理程序中调用一个依赖项来调整大小
return resizedImage, nil
}
希望这能帮到你!
英文:
Likely,
> imgfile
will satisfy the image.Image
interface and not the data saved on disk (ie. the actual jpg file)
> Image is a finite rectangular grid of color.Color values taken from a color model.
Lots of 3rd party golang image libraries use image.Image
to manipulate images.
<hr>
I would use a standard image.Image
interface retrieved (read to memory) by a filename in the imageGET
function and modified according to the queries. You can see also the jpeg golang library from the standard lib.
function ImageGET(w http.ResponseWriter, r *http.Request) {
// GetJPEGImageFromFileSystem must decode os.File content and
// return a golang image.Image interface
img, _ := GetJPEGImageFromFileSystem(r.URL.Query().Get("filename"))
if r.URL.Query().Get("resize") != "" {
// If resizing, write over the image.Image in Memory,
// but not the image File on disk
img, _ = Resize(img, r.URL.Query().GET("resize"))
}
// etc...
}
function Resize(img image.Image, size string) (image.Image, error) {
// the resize stuff -- or, alternatively just call a dependency in the original handler to resize
return resizedImage, nil
}
答案2
得分: 1
现在,我的问题是,我应该如何设计图像处理的函数,以便能够正确高效地处理所有内容?
这取决于你使用的包以及你想要用它做什么。如果你看一下imaging包,你会发现它们总是返回*image.NRGBA类型。
该类型实现了image.Image
接口。
在下一步中,你可以使用Encode函数。
func Encode(w io.Writer, img image.Image, format Format) error
正如你所看到的,该函数使用了一个io.Writer
。
func ImageGET(w http.ResponseWriter, r *http.Request) {
// ...
imaging.Encode(w,img,imaging.PNG)
// ...
因此,你只需要在处理程序中使用该写入器,就可以了。
所以,为了保持你的函数正确,只需返回image.Image
接口即可。
英文:
> Now, my question is, how should I design whatever function does the
> image processing so that it will process everything properly and
> efficiently?
Depends on the package you are using for your stuff and what you want to do with it. If you look for example to the imaging package you see that they return always: *image.NRGBA
That type implements the image.Image
interface.
At the next step you can use the Encode function.
> func Encode(w io.Writer, img image.Image, format Format) error
As you see that function uses a io.Writer.
function ImageGET(w http.ResponseWriter, r *http.Request) {
// ...
imaging.Encode(w,img,imaging.PNG)
// ...
So you just need to use the writer from the your handler there and ready.
So to keep your function correct just return the image.Image
interface.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论