利用Go语言上传图像并生成缩略图
2013-04-12 14:56
369 查看
承前文:Go语言中对图像进行缩放
//利用Go语言上传图像并生成缩略图
func upload(w http.ResponseWriter, req *http.Request, link string) {
// Upload of a new image.
// Copied from Moustachio demo.
f, _, err := req.FormFile("image")
if err != nil {
fmt.Fprintf(w, "You need to select an image to upload.\n")
return
}
defer f.Close()
i, _, err := image.Decode(f)
if err != nil {
panic(err)
}
// Convert image to 128x128 gray+alpha.
b := i.Bounds()
const max = 128
// If it's gigantic, it's more efficient to downsample first
// and then resize; resizing will smooth out the roughness.
var i1 *image.RGBA
if b.Dx() > 4*max || b.Dy() > 4*max {
w, h := 2*max, 2*max
if b.Dx() > b.Dy() {
h = b.Dy() * h / b.Dx()
} else {
w = b.Dx() * w / b.Dy()
}
i1 = resize.Resample(i, b, w, h)
} else {
// "Resample" to same size, just to convert to RGBA.
i1 = resize.Resample(i, b, b.Dx(), b.Dy())
}
b = i1.Bounds()
// Encode to PNG.
dx, dy := 128, 128
if b.Dx() > b.Dy() {
dy = b.Dy() * dx / b.Dx()
} else {
dx = b.Dx() * dy / b.Dy()
}
i128 := resize.ResizeRGBA(i1, i1.Bounds(), dx, dy)
var buf bytes.Buffer
if err := png.Encode(&buf, i128); err != nil {
panic(err)
}
h := md5.New()
h.Write(buf.Bytes())
tag := fmt.Sprintf("%x", h.Sum(nil))[:32]
ctxt := fs.NewContext(req)
if err := ctxt.Write("qr/upload/"+tag+".png", buf.Bytes()); err != nil {
panic(err)
}
// Redirect with new image tag.
// Redirect to draw with new image tag.
http.Redirect(w, req, req.URL.Path+"?"+url.Values{"i": {tag}, "url": {link}}.Encode(), 302)
}
//利用Go语言上传图像并生成缩略图
func upload(w http.ResponseWriter, req *http.Request, link string) {
// Upload of a new image.
// Copied from Moustachio demo.
f, _, err := req.FormFile("image")
if err != nil {
fmt.Fprintf(w, "You need to select an image to upload.\n")
return
}
defer f.Close()
i, _, err := image.Decode(f)
if err != nil {
panic(err)
}
// Convert image to 128x128 gray+alpha.
b := i.Bounds()
const max = 128
// If it's gigantic, it's more efficient to downsample first
// and then resize; resizing will smooth out the roughness.
var i1 *image.RGBA
if b.Dx() > 4*max || b.Dy() > 4*max {
w, h := 2*max, 2*max
if b.Dx() > b.Dy() {
h = b.Dy() * h / b.Dx()
} else {
w = b.Dx() * w / b.Dy()
}
i1 = resize.Resample(i, b, w, h)
} else {
// "Resample" to same size, just to convert to RGBA.
i1 = resize.Resample(i, b, b.Dx(), b.Dy())
}
b = i1.Bounds()
// Encode to PNG.
dx, dy := 128, 128
if b.Dx() > b.Dy() {
dy = b.Dy() * dx / b.Dx()
} else {
dx = b.Dx() * dy / b.Dy()
}
i128 := resize.ResizeRGBA(i1, i1.Bounds(), dx, dy)
var buf bytes.Buffer
if err := png.Encode(&buf, i128); err != nil {
panic(err)
}
h := md5.New()
h.Write(buf.Bytes())
tag := fmt.Sprintf("%x", h.Sum(nil))[:32]
ctxt := fs.NewContext(req)
if err := ctxt.Write("qr/upload/"+tag+".png", buf.Bytes()); err != nil {
panic(err)
}
// Redirect with new image tag.
// Redirect to draw with new image tag.
http.Redirect(w, req, req.URL.Path+"?"+url.Values{"i": {tag}, "url": {link}}.Encode(), 302)
}
相关文章推荐
- Go语言图片处理和生成缩略图的方法
- 利用ASPUPLOAD,ASPJPEG实现图片上传自动生成缩略图及加上水印
- 利用ASPUPLOAD,ASPJPEG实现图片上传自动生成缩略图及加上水印
- php实现图片上传并利用ImageMagick生成缩略图
- C# 上传图片,生成缩略图,生成文字或图像水印图
- PHP实现图像上传并生成缩略图
- php实现图片上传并利用ImageMagick生成缩略图
- js生成缩略图后上传(利用canvas重绘)
- js生成缩略图后上传并利用canvas重绘
- js生成缩略图后上传并利用canvas重绘
- VS2005中使用FileUpload控件上传图片并自动生成缩略图
- 上传图片+生成缩略图 ashx代码
- php裁剪上传图片生成缩略图
- [网络收集]asp.net上传图片并生成缩略图
- php图片上传并生成缩略图
- .Net 上传图片并生成高清晰缩略图
- 在ASP.NET中上传图片并生成缩略图
- 【JSP上传图片并自动生成缩略图】
- swfupload+ffmpeg实现视频上传并生成缩略图