inline images as base64 in html files
189553
function(htmlFile, config, cb) {
        var html = fs.readFileSync(htmlFile, 'utf-8'),
            processedImages = 0,
            $ = cheerio.load(html);

        // grab all <img/> elements from the document
        $('img').each(function (idx, elm) {
            var src = $(elm).attr('src'),
                imgPath = null,
                img = null,
                mimetype = null,
                inlineImgPath = null;

            // check if the image src is already a data attribute
            if (src.substr(0, 5) !== 'data:') {
                // figure out the image path and load it
                inlineImgPath = imgPath = path.join(path.dirname(htmlFile), src);
                img = fs.readFileSync(imgPath, 'base64');

                mimetype = mime.lookup(inlineImgPath);

                // check file size and ie8 compat mode
                if (img.length > 32768 && config.ie8 === true) {
                    // i hate to write this, but can´t wrap my head around
                    // how to do this better: DO NOTHING
                } else {
                    $(elm).attr('src', 'data:' + mimetype + ';base64,' + img);
                    processedImages++;
                }
            }

        });
        html = $.html();

        // check if a callback is given
        if (_.isFunction(cb)) {
            grunt.log.ok('Inlined: ' + processedImages + ' Images in file: ' + htmlFile);
            cb(htmlFile, html);
        }
    }
339600
function(canvas) {
            var base64 = canvas.toDataURL('image/jpeg', options.jpegQuality)
                .replace(/^data:image\/(png|jpeg|jpg|gif);base64,/, '');
            imageData = base64toBlob(base64, 'image/jpeg');       // Byte array
        }
1190896
function dataURItoBlob(dataURI) {
  // convert base64/URLEncoded data component to raw binary data held in a string
  var byteString;
  if (dataURI.split(',')[0].indexOf('base64') >= 0)
    byteString = atob(dataURI.split(',')[1]);
  else
    byteString = unescape(dataURI.split(',')[1]);

  // separate out the mime component
  var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];

  // write the bytes of the string to a typed array
  var ia = new Uint8Array(byteString.length);
  for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
  }

  return new Blob([ia], {
    type: mimeString
  });
}
734290
function convertInlinePNG(source, dest) {
    if (!/^data\:image\/png/.test(source)) return Promise.reject(new Error('Source is not a PNG data-uri'));

    var base64data = source.split('data:image/png;base64,')[1];
    var buf = new Buffer(base64data, 'base64');

    return fs.writeFile(dest, buf)
    .then(function() {
        if (fs.existsSync(dest)) return;

        throw new Error('Error converting '+source+' into '+dest);
    });
}
765831
private static byte[] base64Decode(String data) {
        int len = data.length();

        byte[] b64 = new byte[len * 3 / 4];
        byte[] buf = new byte[4];
        boolean done = false;

        for (int i = 0, j, k = 0; i < len && !done;) {
            buf[0] = buf[1] = buf[2] = buf[3] = 0;
            for (j = 0; j < 4 && i < len; ++j) {
                char c = data.charAt(i);
                if (c >= 'A' && c <= 'Z') {
                    c -= 'A';
                } else if (c >= 'a' && c <= 'z') {
                    c = (char) (c - 'a' + 26);
                } else if (c >= '0' && c <= '9') {
                    c = (char) (c - '0' + 52);
                } else if (c == '+') {
                    c = 62;
                } else if (c == '/') {
                    c = 63;
                } else if (c == '=' || c == '-') {
                    done = true;
                    break;
                } else {
                    ++i;
                    --j;
                    continue;
                }
                buf[j] = (byte) c;
                ++i;
            }

            if (k < b64.length && j >= 1) {
                b64[k++] = (byte) ((buf[0] << 2) | ((buf[1] & 0x30) >> 4));
            }
            if (k < b64.length && j >= 3) {
                b64[k++] = (byte) (((buf[1] & 0x0f) << 4) | ((buf[2] & 0x3c) >> 2));
            }
            if (k < b64.length && j >= 4) {
                b64[k++] = (byte) (((buf[2] & 0x03) << 6) | (buf[3] & 0x3f));
            }
        }
        return b64;
    }
713611
def raw_encode()
      return Base64.encode64(File.read @file_path).delete("\n") if RUBY_VERSION < "1.9.0"
      Base64.strict_encode64(File.read @file_path)
    end
1183856
private String base64(String value)
    {
        StringBuffer cb = new StringBuffer();

        int i = 0;
        for (i = 0; i + 2 < value.length(); i += 3) {
            long chunk = (int) value.charAt(i);
            chunk = (chunk << 8) + (int) value.charAt(i + 1);
            chunk = (chunk << 8) + (int) value.charAt(i + 2);

            cb.append(encode(chunk >> 18));
            cb.append(encode(chunk >> 12));
            cb.append(encode(chunk >> 6));
            cb.append(encode(chunk));
        }

        if (i + 1 < value.length()) {
            long chunk = (int) value.charAt(i);
            chunk = (chunk << 8) + (int) value.charAt(i + 1);
            chunk <<= 8;

            cb.append(encode(chunk >> 18));
            cb.append(encode(chunk >> 12));
            cb.append(encode(chunk >> 6));
            cb.append('=');
        }
        else if (i < value.length()) {
            long chunk = (int) value.charAt(i);
            chunk <<= 16;

            cb.append(encode(chunk >> 18));
            cb.append(encode(chunk >> 12));
            cb.append('=');
            cb.append('=');
        }

        return cb.toString();
    }
1077365
function encodeToBase64(originalString) {
    const bytes = te.encode(originalString);
    const byteString = Array.from(bytes, byte => String.fromCharCode(byte)).join("");
    const encoded = btoa(byteString);
    return encoded;
  }
161318
function urlToBase64 (url) {
  return new Promise((resolve, reject) => {
    request.get(url, (err, response, body) => {
      err && reject(err)
      resolve(body.toString('base64'))
    })
  })
}
182720
def get_imageblock(filename, format=''):
    
    _, extension = os.path.splitext(filename)

    with open(filename, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')

    return '\n\n.. raw:: html\n\n\t<img src="data:image/' + (format if format else extension[1:]) + ';base64,' + encoded_string +'">\n'
1213465
function getSrc(file) {
  const encodedContents = new Buffer(file.contents).toString('base64')
  return `src:url(data:${mime.lookup(file.path)};charset=utf-8;base64,${encodedContents});`
}
881462
function base64(url, obj, _, fn){
  if (3 == arguments.length) fn = _;
  var prefix = exports.prefix;
  obj = encode(JSON.stringify(obj));
  obj = encodeURIComponent(obj);
  url += '?' + prefix + '=' + obj;
  jsonp(url, { param: exports.callback }, function(err, obj){
    if (err) return fn(err);
    fn(null, {
      url: url,
      body: obj
    });
  });
}
1067321
function base64ToBlob(base64Image, contentType) {
  // http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript

  // Remove leading 'data:image/jpeg;base64,' or 'data:image/png;base64,'
  base64Image = base64Image.split(',')[1];

  let byteCharacters = atob(base64Image);

  // http://stackoverflow.com/questions/6259515/javascript-elegant-way-to-split-string-into-segments-n-characters-long
  let byteArrays = byteCharacters.match(/[\s\S]{1,512}/g).map( slice => {

    let byteNumbers = slice.split('').map( x => x.charCodeAt(0) );

    return new Uint8Array(byteNumbers);
  });

  let blob = new Blob(byteArrays, { type: contentType || 'image/jpg' /* or 'image/png' */ });

  return blob;
}
1250279
def embedded_image_tag(model, options = {})
      model.load_image
      format  = options[:format] || :jpg
      mime    = Mime::Type.lookup_by_extension(format.to_s).to_s
      image   = model.output_image(:format => format)
      data    = Base64.encode64(image)
      
      options = { :alt => model.class.to_s }.merge(options)
      
      result = image_tag("data:#{mime};base64,#{data}", options)
      result.gsub(%r{src=".*/images/data:}, 'src="data:')
      
    rescue Fleximage::Model::MasterImageNotFound => e
      nil
    end
758394
function dataURLToBlob(dataURL) {
                    var BASE64_MARKER = ';base64,',
                        contentType,
                        parts,
                        raw;
                    if (dataURL.indexOf(BASE64_MARKER) === -1) {
                        parts = dataURL.split(',');
                        contentType = parts[0].split(':')[1];
                        raw = parts[1];

                        return new Blob([raw], {type: contentType});
                    }

                    parts = dataURL.split(BASE64_MARKER);
                    contentType = parts[0].split(':')[1];
                    raw = window.atob(parts[1]);
                    var rawLength = raw.length;
                    var uInt8Array = new Uint8Array(rawLength);

                    for (var i = 0; i < rawLength; ++i) {
                        uInt8Array[i] = raw.charCodeAt(i);
                    }
                    return new Blob([uInt8Array.buffer], {type: contentType});
                }
198944
public static String toBase64(File image) throws IOException {
        return "data:" + MimeType.getByFilename(image.getName()) + ";base64," + BaseEncoding.base64().encode(IOUtils.toByteArray(image));
    }
604826
function bytes2base64(buffer) {
            var str = '';
            var length;
            var i;
            // ArrayBuffer
            if (buffer instanceof ArrayBuffer) {
                length = buffer.byteLength;
                var view = new DataView(buffer, 0, length);
                for (i = 0; i < length; i++) {
                    str += String.fromCharCode(view.getUint8(i, false));
                }
            }
            // Array
            else if (buffer.length) {
                length = buffer.length;
                for (i = 0; i < length; i++) {
                    str += String.fromCharCode(buffer[i]);
                }
            }

            return btoa(str);
        }