Nodejs crawling network pictures to Base64 encoded pictures

Source: Internet
Author: User
Tags base64

Crawling network images requires HTTP modules to be loaded

Suppose this is a index.js file

var http = require (' http ');

var url = ' Http://p0.meituan.net/tuanpic/3df525af5a3f7fe04077567d2a6caf794904.png '; A picture of the web

Http.get (Url,function (res) {
var chunks = []; Buffer data used to save network requests that are constantly loading transmissions
var size = 0; Save the total length of buffered data

Res.on (' Data ', function (chunk) {
Chunks.push (chunk); When a network request is made, data is continuously received (the data is not acquired once),

Node saves the received fragment of data to the buffer in a segment,

These pieces of data form a buffer object (that is, the buffer object),

The concatenation of the buffer data cannot be spliced like a string (because a Chinese character occupies three bytes),

If a data fragment carries a Chinese two byte, the next piece of data carries the last byte,

Direct string stitching will cause garbled characters, in order to avoid garbled, so will get buffered data pushed into the chunks array,

Use the following node. JS built-in Buffer.concat () method for stitching

         
Size + = Chunk.length; The length of the accumulated buffered data
});

  

Res.on (' End ', function (err) {

var data = Buffer.concat (chunks, size); Buffer.concat the buffered data in the chunks array and returns a new buffer object assigned to data.

Console.log (Buffer.isbuffer (data)); The Buffer.isbuffer () method can be used to determine whether a variable is a buffer object

    

var base64img = data.tostring (' base64 '); Converts a buffer object to a string and displays it in Base64 encoded format

Console.log (BASE64IMG); Enter the terminal terminal, then enter the directory where index.js resides, and input node index.js in the terminal;

Print out the base64 encoded format of the image, as shown below

});

});

After crawling the network picture, convert it to Base64 encoded format as follows:

ivborw0kggoaaaansuheugaaakqaaabscayaaadkdhmyaaaabmjlr0qa/wd/ap+ gvaetaaaqquleqvr42u2dd5gv1rmh34uvwiriggjgkkjoai2ixqixyw9oohgtxstg2gskkurjn/ aw2gnqy0edqferc7flvcjfrlgaxurauzosmz9+z5zh7tw7m/dou7vnfz777n07z2bozpzmlo875zs1g8apoqlaadob/ kw2fwdca1wezkvkrgxqatgfoaryv0ia+ccgwocp581shfyv7r8uolhe9l7asgaqsfgk17uombk4m+ Jibdgdk8zcuakgar4ghghi09okwz2fa6ofhgqgbkr7bbgxhfw4nnpp8ccoqqkcdcwjsnmlodorr70qxwcbbqrzcrwhnksqh0se4hlke8afidkdaaxo8hp6aoe gsdcamjrgpixlepcgas4bzgwkqqwuawosup5lcg4wfagmsuj8lgqju5blgzncpnso+ Hkc17itchcidkcjuvpysjycbhgegbci3wxe28gpba4luornuynjiujcggr1cbyhpombtdjxctzbbdovqmozmofrtsreeokctbgoznhey5vsczwvit1nwmsjxk 8lrmo8nprgrdntzawbfbkhvb2/iajnogow7/iwco3vamyvoh2pbfaq+wexcgdwmuiyoudvugmsaenalgvjs0ilhz2ayqfphgq+ jfvevpawp9ydbghim4mebgmrbeuusik05apvlv2fcv5wr0b1il4c+ Mscvycflpjxcnjbxnkj69jrzefelqlhq2xlrrcctoqkk0hlrrcctoskuobxvi5o6zxr3pkaduhhbmiwc1a0c5hmhjrliiifoyhrobcemnzsbuixdsycwdtkuj di/g5bzvtyhrwkjvdyqvpyhrwkjvdyqvpyhrwkjveumvukaxcd2rcxhkj3jzrx15yzt7obdeqbaylue3beekrbvnyomu8+ Ao5m6vz9kc8ifwnwxnyzsvv2y2zl4awcy2mmhhvky2u/yckwvtyz8vjlfn3wxcjkl12mtnvacxaccdnqouumfgjlyjzfk+ak4cpykeawnzqwrb1wg7b/ 1hkphrvky6abmtnslxvggrccbp6sbzxq/uye24zs3mwfveg0mwyag92wkm2x/ vgbss7cpu8aewdds8q0lsgbjycby4gmxkmora1assnraqloo5rfn9bivnhcxp3urytybc1+lti2hgw+ 1ah3ev2mvwhdyyeyiv9tyf7otq3ivdibefatrixmoltg2gcl8dskynyqj0h2adylks72mirvzga0wqd/hh2wogwbx2ad+ ulyjmhuidplomrjcduglxi7rtjncxqd96wsm+9hntqq3w+zzq5vlyydtxbnjloarcmpgcffggy7inkcrdoaa0aiybfrjug/ Gmt4dtazl0weblljyhqtwzctgavrao0o5rp/atsb87k+gccqctzkxeatf9ky43ya7ir4dyrclgd+ t5ws8zinto2lnlcstywnachuv2wd8shyqvypufzli5hnzvzwmy6kfwtzyy6wd/ dfrdnsdlaqzyu30gidd7poslnkqzdtqyzn0gidqhujwee2f8yau6cif1wnfwt1cylweohck+ eevsiolsxn0ziuyrlqnspryiog4fhky4zbljwoq4vkqlnzlgp8x8gwwghgbnvn5c+xb1cp+o+q6c8ddg+ztjxh3bixk4ughylvqzwcdhwa/ dgd60mbt5gb/oui+/ql3nc+zem7dfkjnfyqtcj0ywi/ Youovbi0z95aaysiilgqsfoquwkpar0j7hmx8bvcv8wnivdzlsoevmgzapzje1lnxpbtegdnq5a1abbcw2gj4rbmqixefcafefy7zpwryjjbaua08rmk7ljge Hihtlqqvvgpeaxr2qvz0qpqy83/pwn3rnh/n2aa4rvylmjcitpwszouilsh+9hbygabhsvonlr33gmjcbjwzitj9esmpbfusrirkuft4nskdh6nigtqg/tm1om9bdkho9cazblp+wxbjnqxt0c85k9qz+ae1aazlm+ jwpykonc3urnpdeo5d0db03tvckzlyhzzyik0i4e90bcqkcvvlxcg+cxca3zvrkjtcdwntk1g/4xa81tebgzsbnymgj8m6khrgr1r+ 2wp4hsvzhjgesrjwlbjbzax/oakzzsveai9bk+q7kjqietbc19dynvusq5wklgyeri38ozsbt6bea9zztoqywdg2gc58szmbm6m/ kbhxuzzgji4t0tnhafjomznm6mvccubygsvkosj9xzbo5lxrzb9acbgvcimxc3itlo4gmpszy6zzsx5+wkvmq+ hqv1n4h3ge8pzuqbtsxy4jezryaot0bc4gwes1wwapyyl/+/7wdhixhghaynr7pps/heg6r0v4v/ Ylujnr4i0xydfi5vihsqnrocpuzv2mnixhndbhvcukzyrjbeilueubp6cmglnk0w4lezusg4argvwphrtlvj4iglbvgicgcxbp5horpv5ahjbibrbtigfc1ri 4fekbdkxua71xrca/o6q6rr5czgulzhxex7ukcubgogb4xlkb2iuzq6og3emmdnrkzs8iwz39ab+ gulx2eo8f9wnvfrul3srstu9jijwzcj6aikyfgokvzd6idpxckyw0xhsl7s4u4afa1+heeqszicegpayeal4nebtnmuldp/ bijfo5qg0dysvzgwmw2mdctw1s5db9xnkaj6jsr03gnlusacimlmgbr8+4/mto+oury8i0/zgltjuqcduonbcbloohxviz08c/0tese+orbzi+ /auymfm81slb/ kqpmermvisegvcnybjq1bymto719haazzcyqvpyrxvimipqfkrzu6npuqpbkgw4grkc70x64xy4qhazrybajujgyawzka1chjpnguhc3+ 4jwwqqzafzp0bs3oue2qffagrhzqh+z+fnapr6lu1aa71y4rzJ7ybfoi8h0urn6hazv+thzw4hdbe+c+rszxkppm+wa+qt+dt5iqtzaqhl4fapzqlzqf6bmhxhlwnuu2dsxsfowcadscks2zlr+tle5mmhvb+sf /tzxkmiy/zkobxvzhb8dq0yi7xrzsvjzr5hricbsawrjz6irrl81crjlzfe8xpouli68lm30k++ 001mb8tztlstdpfkzbyxmc0nhelfidkccnsyud8lxa0mlqxrs/2a4bz8f/+bjea6frnmsa7wqh0nvl+gg3fkwxd+ lu59qwu76tybu2ikpy30ohcdkdqfbpshfanc/8lvulfoa7jefi/nhpq8zggbws4btipvscbzkbdaj9fbtjrulu/pua8pwl/8pay+ 6hlx3zgil6aw+jtjkakbo18vluamz8fn9zjomh5yb+wb6sde9amws7m+n59h5ghtmezdyrjtgluxrvjyd7bm5t5wyljdei+ w0emc3efcecuvovkcvi3avemtv6+rq99z/si7+iz75pm2+m4ylyew+j3rseon6x0jlqukmb5/shyswd35eydit5tgqbn7olj41lp/ jcbw7ycvndcubpntaphyxudslf9ziwd1qtewxyohr3dodfdbwhxg4htkmdpqimuuqlsywqa5d0w2rrxdngac81f7yjbgr/n+e2imh+ Sh32ai8z3kwjetlut9w5oxuphzvvrfj9xfsaq7i8w19ff3othylclkqucp4kwsdturm0pksdqppf2kct8dsdtrbp5rgyf8nkck44hnkwfr0dusdjz5ieggqpy ltmknuxoneetn3m4xwzvgl7ilkjgxc1ndfqiojxdsk8g2ynx/bvxuxobw3flyg7al5handf8g+ apaiq25qk87iocr4heci6ux3epuvfdzxhwqiitxvi0bvmj3a7p157f4lzp9cdu5m1atzrhcntuk9bg31hm/1pggylh6wns/qmndagveg/ Hxsnpitqwubgyiwqrkghh4pbcs4abcauitt3padqm2c58h2uu7zxp9skoancj5v+dgx2l5kmd0sazji+yv0fjbulvyo7bhbgv4tlu1hjc/l+jv5dpodemki/ rihcad9bcjou0ovkzodfei3ndjqnoe35mq20lkaiilhrj0q2nopekvxdv6qrf8v0hxamgt/ b87xshb5vjlm6xu3yv11txrbgd53enjlmchopxiw0w6ryr/7biod5fwireg7bvg4ovc+dop5nxkbmozmfmb6n4mf/6/nygdzagejkz5/ ydadphyoqrqcfelkxfk+cqemfxlqyfejfyipii7ud5xioezgpu+tag2g3u7njefh8i1ytf+aj32eyav2xmsyopkxo08yausav5br6t9+ cl1jhxgqhrp6/2w13ter9kb1ov83wfjbrjeamxelkgoop1a9szc+kjbpiy2pxdrrqwnkcs2bf/ mcxojqeqvt2yqrxtsp2levs62gll2kramn7n830l8wld3gfrkqenauftizo5hcwgoexievpqhdpzxkkp6pea3t4pkf4zz/ d6mgoynpiw5elp96civ5p9iugcy2aiaj86d3aacks3cxnzp6ba4nkm8lzt8dck3uzdmgacn85nirhfhziw5mee7/ cc1yd8vnipnutwnbljihrbcrxpmn4z5dmxvhrhoiolli4mtqnqr3llxd4hzuvne9bzuxzjdz+bj1sqbhkqtjmkyzec+krssfxzyj/f+ uoaya7ex4zgtphtxng3rkzwu+g/6fk4y0rn4pyzdikyoebvj1s5ime1rb9au5oqlwnrb/ fu4pvn453hukzka1hvxih3wfrsbaa9y7tt0fbaruo5a/ri2oadmzwnuca+bsgd6noorvftgaqvkg1boogfuqjqynu+ axoixzbowg3ugea9ilylt/d5vm/2zsib4ncow+j5vj3+defxlr3ili33mffdbxdyznsebtk0fuqzf1tk322feun3ebzdjwt7va1/g+ H6yi4jahjcffp1nsgn4wczucszm5evdwzywr+bqpvb6kf2r3aqkbh+ha/hmqhjmrnvr+ey319arv1eyf/bhfnkg9gonenwly7zzsffc5cz49filfoaak+ ui0jq74fehqf0hyyexnta/mvyb3liwh8q8lhibfohsrzbkb33k+ Q1ohlx1hyxqxr3p5nncgys41tzxbxz2w5mf8fdeabrzuursws85nmo4zasmu4u8umzgi2kmrlhuooq0nrghpyehivibnrtx5k0r9c0ejsf2qklbbksrvg7h70 4h5qpl3fr6jbnkw/6efq9bpvsfz1uzn8hmrgnmj9crqnnumhzqpqchyr3kk0lhg/n/bwwjqtqvdyhwwehuutjirev+ ij714v4dxedvbvlkkd8gpe+jto9bzhpuioyz71lbuiiisfujwtjd+65qi36fsoj30bvmbd99bbjtz3qmdchr9lzaktm9wzbo5+ 8zzlhr0fve0fkansv+fwpr6xopbsbpgf1fpx284bmgbmpxrq0rob2gsbf73u2ofma4xnddu8gws1gl0u9qge+m+lprgtxfwuxt0s+ 7lbmgjuhl3g2+f1x+ Lulmfmejdqmxaarripggrxcqzqkd93mfzjxf9bgjs2lwnkyaaaagxrfwhrtb2z0d2fyzqbbzg9izsbjbwfnzvjlywr5ccllpaaaaabjru5erkjggg ==

Question:

1: What is the use of converting a picture to base64 encoding?

2: How does the converted Base64 code work?

Answer:

1. Convert the image to Base64 encoding can realize the Web page picture in the bad speed of the time before the content load and reduce the number of HTTP requests to reduce the burden on the server;

2. How to use (PNG for example), above a large lump of code abbreviated to ABC

Background:url ("Data:image/png;base64,abc") no-repeat;

If the conversion is the other format of the picture (such as GIF) is rewritten into Background:url ("data:image/gif;base64,abc") no-repeat;

Nodejs crawling network pictures to Base64 encoded pictures

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.