golang-Achieving picture Scaling

Source: Internet
Author: User
Tags imagejpeg
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT" "Graphics" "Image" "Image/png" "Log" "Net/http" "OS" "StrConv" "Stri NGS ") Func Main () {http. Handlefunc ("/", Doimagehandler) http. Listenandserve ("127.0.0.1:6789", nil)}func Doimagehandler (w http. Responsewriter, R *http. Request) {fmt. Printf ("%q\n", strings. Split (R.url. Path, "/")) URL: = Strings. Split (R.url. Path, "/") If Len (URL)! = 3 {return} NEWDX, Uerr: = StrConv. Atoi (url[1]) if uerr! = Nil {log. Fatal (Uerr)} src, err: = LoadImage (url[2]) bound: = src. Bounds () dx: = bound. Dx () Dy: = bound. Dy () if err! = Nil {log. Fatal (ERR)}//thumbnail size DST: = image. Newrgba (image. Rect (0, 0, NEWDX, NEWDX*DY/DX))//Generate thumbnails, equal scale err = graphics. Scale (DST, SRC) if err! = Nil {log. Fatal (Err)} Header: = W.header () header. ADD ("Content-type", "Image/jpeg") png. Encode (W, DST)}//Load image decodes an image from a file of Image.func LoadImage (Path String) (img Image. Image, Err error) {file, err: = OS. Open (PATH) if err! = nil {return} defer file. Close () img, _, err = image. Decode (file) return}

///////////////////////////////////////////////////////////////////////

Package Masimport ("Code.google.com/p/graphics-go/graphics" "FMT" Z "github.com/nutzam/zgo" "image") func (MA *master) Activeimage (pobj string) error {//file absolute path var path string = pobj//Preserve source image structure var img image. Image//Picture type TYPEF: = Z.filetype (path)//load picture in picture format switch TYPEF {//JPEG case "JPEG"://Imagejpe    G img = z.imagejpeg (path)//JPG case "JPG"://imagejpeg img = z.imagejpeg (path)//PNG case "PNG": Imagepng img = z.imagepng (PATH)}//Determine if the original picture was successfully loaded if img = = Nil {//return error return to FMT. Errorf ("Active image decode exception ...")}//--------------------------------------------------------////Received Number of screens monisize: = ma. Nodeconf.monitorsize (MA)//Get screen resolution width: = ma. NodeConf.Resolution.Width Height: = ma. NodeConf.Resolution.Height//Get footage average widthmoni: = img. Bounds (). Dx ()/monisize.col Heightmoni: = img. Bounds (). Dy ()/monisize.row//--------------------------------------------------------////traverse screen, cut picture for _, Monis: = Range Ma. Nodeconf.layout {//Traversal node screen for _, Moni: = Range Monis {//get picture row: = Moni. Display.row col: = Moni. Display.col//Generate target background backgroundsrc: = Z.imagergba (Widthmoni, Heightmoni)//Generate target diagram Z.imagedra  Wrgba (Backgroundsrc, IMG, (COL-1) *widthmoni, (row-1) *heightmoni)//Generate final background map background: = Z.imagergba (width, Height)//produce the final graphics. Scale (background, BACKGROUNDSRC)//Save picture in picture format switch TYPEF {//JPEG case "JPEG"://I Mageencodejpeg Z.imageencodejpeg (FMT. Sprintf ("%s.pic_result/%d_%d.%s", Path, col, Row, TYPEF), background)//JPG case "JPG"://Imageenco Dejpeg Z.imageencodejpeg (FMT. Sprintf ("%s.pic_result/%d_%d.%s", Path, col, Row, TYPEF), background)//PNG case "PNG"://Imageenco Depng z.imageencodepng (FMT. Sprintf ("%s.pic_result/%d_%d.%s ", Path, col, Row, TYPEF), background)}}//Return nil} 

///////////////////////////////////////////////////////////////

Package Zimport ("image" "Image/draw" "Image/jpeg" "Image/png" "OS")//Read JPEG picture return Image.image object func IMAGEJP EG (ph string) image. Image {//Open picture file F, fileerr: = OS. Open (ph) if fileerr! = nil {return nil}//exit when closing file defer f.close ()//Decode J, Jerr: = jpeg. Decode (f) If Jerr = nil {return nil}//return decoded picture return j}//read PNG picture returns Image.image object func imagepng (ph stri NG) image. Image {//Open picture file F, fileerr: = OS. Open (ph) if fileerr! = nil {return nil}//exit when closing file defer f.close ()//decode P, Perr: = png.  Decode (f) if perr! = nil {return nil}//returns the decoded picture return p}//creates a blank picture object in accordance with the resolution Func Imagergba (width, height int) *image. RGBA {//build image, images. Rect (min x, min y, max x, min y) return image. Newrgba (image. Rect (0, 0, width, height))}//draws the picture to the picture func Imagedrawrgba (img *image. RGBA, Imgcode image. Image, x, y int) {//Draw images//image. The x, y coordinates of point a points, and the axes to the right and down add {0,0}//image. ZP ZP is the zero point//image. PtPt is shorthand for point{x, Y} draw. Draw (IMG, IMG. Bounds (), Imgcode, image. Pt (x, y), draw. OVER)}//JPEG will encode the image//Select the encoding parameter, the quality range from 1 to 100, the higher is better &jpeg. Options{90}func imageencodejpeg (ph string, img image.    Image) Error {//Make sure file parent directory exists fcheckparents (ph)//Open file waiting to write F: = filew (ph)//ensure file normal shutdown defer f.close () Write the file return JPEG. Encode (F, IMG, &jpeg. OPTIONS{100})}//PNG will encode the generated picture func imageencodepng (ph string, img image.    Image) Error {//Make sure file parent directory exists fcheckparents (ph)//Open file waiting to write F: = filew (ph)//ensure file normal shutdown defer f.close () Write the file return PNG. Encode (F, IMG)}
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.