github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/conv/conv_img_str.go (about)

     1  // Package converts image objects to base64 strings to byte slices.
     2  package conv
     3  
     4  import (
     5  	"encoding/base64"
     6  	"fmt"
     7  	"image"
     8  	"io"
     9  	// "image/jpeg"
    10  	"bytes"
    11  	"image/png"
    12  	"strings"
    13  
    14  	"github.com/pbberlin/tools/net/http/loghttp"
    15  )
    16  
    17  func Base64_str_to_img(base64_img string) (img image.Image, format string) {
    18  
    19  	lg, b := loghttp.BuffLoggerUniversal(nil, nil)
    20  	_ = b
    21  
    22  	pos := base64HeaderPosition(base64_img)
    23  
    24  	reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64_img[pos:]))
    25  	img, format, err := image.Decode(reader)
    26  	lg(err)
    27  	return
    28  }
    29  
    30  // convert image to string, prepend mime header
    31  //		inspiration_1 http://stackoverflow.com/questions/22945486/golang-converting-image-image-to-byte
    32  //		inspiration_2 https://github.com/polds/imgbase64/blob/master/images.go
    33  //
    34  // mime type is always image/png
    35  // because we only accept *image.RGBA and use image/png Encoder
    36  func Rgba_img_to_base64_str(img *image.RGBA) string {
    37  
    38  	lg, b := loghttp.BuffLoggerUniversal(nil, nil)
    39  	_ = b
    40  
    41  	// img to bytes
    42  	buf := new(bytes.Buffer)
    43  	err := png.Encode(buf, img)
    44  	lg(err)
    45  	imgBytes := buf.Bytes()
    46  
    47  	// binary bytes to base64 bytes
    48  	e64 := base64.StdEncoding
    49  	maxEncLen := e64.EncodedLen(len(imgBytes))
    50  	imgEnc := make([]byte, maxEncLen)
    51  	e64.Encode(imgEnc, imgBytes)
    52  
    53  	// base64 bytes to string
    54  	mimeType := "image/png"
    55  	return fmt.Sprintf("data:%s;base64,%s", mimeType, imgEnc)
    56  
    57  }
    58  
    59  func f____________________________() {}
    60  
    61  // we want to find exact position of comma in
    62  //     "data:image/png;base64,"
    63  //   (if the header is present at all)
    64  // string s could be large but according to
    65  //   https://groups.google.com/forum/#!topic/golang-nuts/AdO_d4E_x6k
    66  // the runtime makes it a pointer on its own
    67  func base64HeaderPosition(s string) (pos int) {
    68  
    69  	test_for_header := "data:image/"
    70  	headerLen := len(test_for_header)
    71  
    72  	if s[:headerLen] == test_for_header { // header is present
    73  		pos = strings.Index(s, ",") // get comma pos
    74  		pos++
    75  	}
    76  
    77  	return
    78  
    79  }
    80  
    81  // we want to extract the 'image/png' from
    82  // 	"data:image/png;base64,..."
    83  //func MimeFromBase64(b *bytes.Buffer)(mime string){
    84  func MimeFromBase64(b io.Reader) (mime string) {
    85  
    86  	lg, b := loghttp.BuffLoggerUniversal(nil, nil)
    87  	_ = b
    88  
    89  	// to avoid huge string allocation
    90  	//   we read the first 100 bytes
    91  	b1 := make([]byte, 100)
    92  	_, err := b.Read(b1)
    93  	lg(err)
    94  
    95  	s := string(b1)
    96  
    97  	pos := base64HeaderPosition(s)
    98  
    99  	if pos > 0 {
   100  		tmp1 := s[:pos]
   101  		tmp2 := strings.Split(tmp1, ";")
   102  		tmp3 := strings.Split(tmp2[0], ":")
   103  		mime = tmp3[1]
   104  	}
   105  
   106  	return
   107  
   108  }