node. js Uploading Pictures

Source: Internet
Author: User
Tags imagemagick

Dependent Library Connect,gm,imagemagick,image-size

Connect: Middleware

Gm,imagemagick: Used to process images such as zooming, cutting, adding text watermarks, etc.

Image-szie: Get the picture's width height

Image upload processing service, save Server.js

var http = require ("http");
var connect = require ("Connect");
var url = require ("url");
var fs = require ("FS");

var imagesize = require ("Image-size");
var GM = require ("GM");
var ImageMagick = Gm.subclass ({imagemagick:true});

var oldimgname;
var app = connect (). Use (function (req, res, next) {
var list = [];
var len = 0;
var pathName = Url.parse (req.url). PathName;
PathName = pathname.substring (1, pathname.length);
Oldimgname = Req.headers.imgname;
Req.on ("Error", function (Err) {
Console.log (ERR);
});
Req.on ("Data", function (chunk) {
List.push (chunk);
Len + = Chunk.length;
});
Req.on ("End", function () {
var buf = buffer.concat (list, len);
Req.data = {"path": PathName, "buf": buf};
Next ();
});
}). Use (function (req, res, next) {
if (Req.data.path = = = "Upload" && oldimgname) {
Fs.writefilesync ("./img/new_" +oldimgname, req.data.buf);

Take picture width high
var imageinfo = ImageSize ("./img/new_" +oldimgname);
Console.log (Imageinfo.width, imageinfo.height);

Print watermarks
var imgm = ImageMagick ("./img/new_" +oldimgname);
Imgm.fill ("Blue");
Imgm.fontsize (30);
Imgm.drawtext (+, Date.now (). toString ());
Imgm.write ("./img/new_" +oldimgname, function (err) {
if (err) {
Console.log (ERR);
}
});

Picture Cutting
var imgM2 = ImageMagick ("./img/new_" +oldimgname);
Imgm2.crop (300, 300, 0, 0);//Do Cutting
Imgm2.write ("./img/new_mid_" +oldimgname, function (err) {
if (err) {
Console.log (ERR);
}
});


Fs.writefilesync ("./img/new_min_" +oldimgname, req.data.buf);
var imgm_min = ImageMagick ("./img/new_min_" +oldimgname);
Imgm_min ("Img.png"). Crop (width, height, x, y)//do Cutting
! = Force Scaling
Imgm_min.resize (200, 200, "!"). Autoorient (). Write ("./img/new_min_" +oldimgname, function (err) {
if (err) {
Console.log (ERR);
}
});
}
Next ();
}). Use (function (req, res) {
Res.writehead, {"Content-type": "Text/plain; Charset=utf-8 "});
Res.write ("ending");
Res.end ();
});

Http.createserver (APP). Listen (7650);
Console.log ("Start ..." + 7650);

Client Request Save Code Test.js

var http = require ("http");
var fs = require ("FS");

var Imgpath = "./12306.jpg";

var data = Fs.readfilesync (Imgpath);

var options = {
"hostname": "127.0.0.1",
"Port": 7650,
"Path": "/upload",
"Method": "Post",
"Headers": {
"Content-type": "Application/octet-stream",
"Imgname": "12306.jpg"
}
};
var req = http.request (options, function (res) {
Res.on (' Data ', function (chunk) {
if (res.statuscode! = 200) {
Console.log ("Reqest error");
}else{
Console.log (Chunk.tostring ());
}
});
});
Req.write (data);
Req.end ();

node. js Uploading 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.