This is a creation in Article, where the information may have evolved or changed.
Auth:philo Version:2
This article mainly introduces:
- Read and write picture files.
- How images are converted to and from Base64 in the go cache
- 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) // 图片文件解码rgbImg := m.(*image.YCbCr)subImg := rgbImg.SubImage(image.Rect(0, 0, 200, 200)).(*image.YCbCr) //图片裁剪x0 y0 x1 y1
IMG-File (code above)
f, _ := os.Create("test.jpg") //创建文件defer f.Close() //关闭文件jpeg.Encode(f, subImg, nil) //写入文件
IMG-Base64 (code above)
emptyBuff := bytes.NewBuffer(nil) //开辟一个新的空buffjpeg.Encode(emptyBuff, subImg, nil) //img写入到buffdist := make([]byte, 50000) //开辟存储空间base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) //buff转成base64fmt.Println(string(dist)) //输出图片base64(type = []byte)_ = ioutil.WriteFile("./base64pic.txt", dist, 0666) //buffer输出到jpg文件中(不做处理,直接写到文件)
Imgfile-Base64
ff, _ := ioutil.ReadFile("output2.jpg") //我还是喜欢用这个快速读文件bufstore := make([]byte, 5000000) //数据缓存base64.StdEncoding.Encode(bufstore, ff) // 文件转base64_ = ioutil.WriteFile("./output2.jpg.txt", dist, 0666) //直接写入到文件就ok完活了。
That's probably the code. Basically some small sites are enough.
Scaling something can be preceded by the front end. A cut on the back end is enough.