Go + Appengine Blobstore,调整图像上传大小

huangapple go评论77阅读模式
英文:

Go + Appengine Blobstore, resize image uploads

问题

我正在努力理解Go + Appengine。我一直在使用Blobstore来存储用户的图像上传。有没有办法从BlobStore下载图像并对其进行操作?

或者还有其他解决方案吗?

image lib有点有限。我还想将其他格式转换为jpeg。

英文:

I'm trying to get my head around Go + Appengine. I have been using Blobstore to store users image uploads. Is there anyway I can download the image from the BlobStore and manipulate it?

Or is there another solution?

The image lib is a bit limited. I would also like to convert others formats to jpeg.

答案1

得分: 7

是的,您可以从blobstore中提取文件,并利用Go 1中包含的图像包,包括image/drawimage/jpeg。请参阅Go图像包的概述和image/draw博客文章。假设您已经有了要操作的图像的BlobInfo,您可以通过将嵌入的BlobKey传递给blobstore.NewReader来获取其原始数据,从而获得一个io.Reader对象。将其传递给例如png.Decode以获取一个image.Image。进行任何您喜欢的操作,然后要将其保存回blobstore,请从blobstore.Create获取一个Writer,并将其交给jpeg.Encode。最后调用blobstore.Close和.Key以获得一个新的BlobKey以保存在数据存储中。

如果您想进行更复杂的操作,还有几个纯Go库可以与您的应用程序一起上传并与image.Image一起使用。例如,请参阅graphics-goGo编写的库中的“图像和图形”部分。

英文:

Yes, you can pull a file out of the blobstore and take advantage of the image packages that are included with Go 1, including image/draw and image/jpeg. See The Go image package for an overview and the image/draw blog post. Assuming you already have the BlobInfo for an image you want to manipulate, you can get its raw data by passing the embedded BlobKey to blobstore.NewReader, giving you an io.Reader object. Pass that to e.g. png.Decode to get an image.Image. Do whatever manipulations you like, then to save it back to blobstore, get a Writer from blobstore.Create and hand it to jpeg.Encode. Finally call blobstore.Close and .Key to get a new BlobKey to save in the datastore.

If you want to do even more complex manipulations, there are several pure-Go libraries that you can upload with your app and use with image.Image. See for instance graphics-go and the "Images and Graphics" section of Libraries Written in Go.

答案2

得分: 0

我想要一个能自动优化blobstore图像的包。结果可以在GitHub上找到:https://github.com/TomiHiltunen/GAE-Go-image-optimizer

算法本身与Adam Thomason描述的相似。这个包只是隐藏了一些丑陋的部分并自动化了整个过程。

你可以像使用blobstore.ParseUploads()一样使用它。它会遍历上传的blob并优化图像,同时保留其他类型的blob不变。它还会返回从blobstore.ParseUploads()传递的所有其他值。

英文:

I wanted to have a package that will automatically do the magic of optimizing blobstore images. The result can be found at GitHub: https://github.com/TomiHiltunen/GAE-Go-image-optimizer

The algorithm itself is pretty much like Adam Thomason described. This package just hides the ugly bits and automates the process.

You can use it like you would use blobstore.ParseUploads(). What it does is that it runs through the uploaded blobs and optimizes images leaving all other type of blobs untouched. It will also return all the other values passed from blobstore.ParseUploads().

huangapple
  • 本文由 发表于 2012年9月29日 04:47:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/12647194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定