This is a creation in Article, where the information may have evolved or changed.
The code is directly attached.
Package Mainimport ("Errors" "FMT" "Image" "Image/gif" "Image/jpeg" "image/png" "IO" "OS" "Str Ings "" Golang.org/x/image/bmp "" Github.com/nfnt/resize ") func main () {src: =" data/1.gif "DST: = Strings. Replace (SRC, ".", "_small.", 1) fmt. Println ("src=", SRC, "dst=", DST) FIn, _: = OS. Open (SRC) defer fin.close () FOut, _: = OS. Create (DST) defer fout.close ()//ERR: = Clip (fIn, fOut, 0, 0, max, max)//If Err! = Nil {//Panic (ERR) } ERR: = Scale (FIn, FOut, Max, max) if err! = Nil {panic (err)}}/** Picture Clipping * Entry: * rule: If precision is 0, precision remains Unchanged * * Return: Error */func clip (in IO. Reader, out IO. Writer, x0, y0, x1, y1, quality int) Error {origin, FM, err: = image. Decode (IN) if err! = Nil {return ERR} Switch FM {case "JPEG": img: = Origin. ( *image. YCBCR) subimg: = img. Subimage (image. Rect (x0, y0, x1, y1)). (*image. YCBCR) return JPEG. Encode (out, subimg, &jpeg. Options{quality}) Case "PNG": Switch canvas. (type) {Case *image. NRGBA:IMG: = Canvas. (*image. NRGBA) subimg: = img. Subimage (image. Rect (x0, y0, x1, y1)). (*image. NRGBA) return PNG. Encode (out, subimg) case *image. RGBA:IMG: = Canvas. (*image. RGBA) subimg: = img. Subimage (image. Rect (x0, y0, x1, y1)). (*image. RGBA) return PNG. Encode (out, subimg)} case "GIF": img: = origin. (*image. paletted) subimg: = img. Subimage (image. Rect (x0, y0, x1, y1)). (*image. paletted) return GIF. Encode (out, subimg, &gif. options{}) Case "BMP": img: = origin. (*image. RGBA) subimg: = img. Subimage (image. Rect (x0, y0, x1, y1)). (*image. RGBA) return BMP. Encode (out, subimg) Default:return errors. New ("ERROR FORMAT")} return nil}/** thumbnail generation * Entry: * rule: if width or hight has one of 0, then the size is unchanged if the precision is 0 the precision remains the same * the starting point of the rectangular coordinate system is upper left * back: Erro R */func Scale (in IO. Reader, out IO. Writer, width, height, quality int) errOr {origin, FM, err: = image. Decode (IN) if err! = Nil {return err} if width = = 0 | | Height = = 0 {width = origin. Bounds (). max.x Height = origin. Bounds (). MAX.Y} If quality = = 0 {quality = +} canvas: = resize. Thumbnail (UINT (width), uint (height), origin, resize. LANCZOS3)//return jpeg. Encode (out, canvas, &jpeg. OPTIONS{QUALITY}) Switch FM {case "JPEG": return JPEG. Encode (out, canvas, &jpeg. Options{quality}) Case "PNG": return PNG. Encode (out, canvas) case "GIF": return GIF. Encode (out, canvas, &gif. options{}) Case "BMP": Return BMP. Encode (out, canvas) Default:return errors. New ("ERROR FORMAT")} return nil}