Golang image processing, clipping, base64 data conversion, file storage

Source: Internet
Author: User
Tags builtin
This is a creation in Article, where the information may have evolved or changed.

This article mainly introduces: 1. Read and write picture files. 2. How to convert images to and from Base64 in the Go Cache 3. Picture clipping

In this article, all error judgments are removed for easy viewing

Base64-File

ddd, _ := base64.StdEncoding.DecodeString(datasource) //成图片文件并把文件写入到buffererr2 := ioutil.WriteFile("./output.jpg", ddd, 0666) //buffer输出到jpg文件中(不做处理,直接写到文件)

datasourceBase64 string

Base64, Buffer

ddd, _ := base64.StdEncoding.DecodeString(datasource) //成图片文件并把文件写入到bufferbbb := bytes.NewBuffer(ddd) // 必须加一个buffer 不然没有read方法就会报错

After converting to buffer, there is the reader method. To be decode by the image API

Buffer-> Imagebuff (picture clipping, code connection above)

M,_,_: = Image.Decode(BBB)Image file decoding rgbimg: = M.(*image:= rgbimg subimage (Image.< Span class= "token function" >rect (00200200) ) . (*image//picture clipping x0 y0 x1 y1      

img-File (code above)

f_  : = Os. Create ( "test.jpg" //create file defer f.close//close file Jpeg. Encodenil) //write file             

IMG-Base64 (code above)

Emptybuff: = bytes.Newbuffer(Nil)To open up a new empty buffjpeg.Encode(Emptybuff, subimg,Nil)IMG writes to Buffdist:=Make([]Byte,50000)Open up storage space base64. Stdencoding.Encode(Dist, Emptybuff.Bytes() //buff turn into base64fmt println (string//output picture base64 (type = []byte] _ = Ioutilwritefile (0666) // Buffer output to JPG file (do not process, write directly to file)            

Imgfile-Base64

Ff,_: = Ioutil.ReadFile("Output2.jpg")I still like to use this quick read file Bufstore:=Make([]byte5000000//data cache Base64. Encode (Bufstore//file goto Base64_ = ioutil. Writefile0666//write directly to the file to finish the job.  

The

is probably the code that basically some small sites are enough. Scaling something can be preceded by the front end. A cut on the back end is enough.

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.