packageresizeimport("github.com/nfnt/resize""image/png""io""os")funcResizePng(rio.Reader,filepathstring){// decode png into image.Imageimg,err:=png.Decode(r)iferr!=nil{panic(err)}// resize to width 60 using Lanczos resampling// and preserve aspect ratiom:=resize.Resize(60,0,img,resize.Lanczos3)out,err:=os.Create(filepath)iferr!=nil{panic(err)}deferout.Close()// write new image to filepng.Encode(out,m)}
packageresizeimport("net/http""testing")funcTestResizePng(t*testing.T){// open image on webresp,err:=http.Get("https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png")iferr!=nil{t.Error(err)return}deferresp.Body.Close()ResizePng(resp.Body,"test_resized.png")}
For more usage of github.com/nfnt/resize, visit its GitHub page. If your image
is of JPEG format, import image/jpeg and modify the code correspondingly.
Source code tested on: Ubuntu Linux 16.10, Go 1.8.