Golang picture cropping and thumbnail generation

Source: Internet
Author: User
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}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.