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文件中(不做处理,直接写到文件)
datasource
Base64 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.