New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

image-convert

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

image-convert - npm Package Compare versions

Comparing version 0.1.32 to 0.1.33

137

index.js
// Copyright 2016 Arshpreet Wadehra
//
//
const image = require('images');
const request = require('request').defaults({ encoding: null });
const FileAPI = require('file-api')
, File = FileAPI.File
, FileList = FileAPI.FileList
, FileReader = FileAPI.FileReader
;
var image = require('images');
var request = require('request').defaults({
encoding: null
});
var FileAPI = require('file-api'),
File = FileAPI.File,
FileList = FileAPI.FileList,
FileReader = FileAPI.FileReader;
function GetFilename(url) {
if (url)
{
var m = url.toString().match(/.*\/(.+?)\./);
if (m && m.length > 1)
{
return m[1];
}
}
return "";
if (url) {
var m = url.toString().match(/.*\/(.+?)\./);
if (m && m.length > 1) {
return m[1];
}
}
return "";
}
function dataURLtoFile(dataurl, filename,output_format) {
function GetFileExtension(url) {
if (url) {
var index = url.lastIndexOf(".") + 1;
return url.substr(index);
}
}
function dataURLtoFile(dataurl, filename, output_format) {
var file = new File({
name: filename+"."+output_format, // required
type: "image/"+output_format, // optional
buffer: new Buffer(dataurl,'base64')
name: filename + "." + output_format, // required
type: "image/" + output_format, // optional
buffer: new Buffer(dataurl, 'base64')
});

@@ -31,26 +39,67 @@ return file;

module.exports = {
fromURL : (options, callback)=>{
let {url, quality,output_format,size} = options;
if(url == undefined)
callback("No URL FOUND",null);
if(quality == undefined)
quality = 100
if(output_format == undefined)
output_format = "jpg"
let filename = GetFilename(url);
try{
request.get(url, function (err, res, body) {
var img = image(body);
var buffer;
if(size ==undefined || size == "original")//original size
buffer = image(body).encode(output_format,{quality:quality});
else
buffer = image(body).size(size).encode(output_format,{quality:quality});
callback(null,buffer,dataURLtoFile(buffer,filename,output_format));
});
fromURL: function(options, callback) {
var url = options.url;
var quality = options.quality;
var output_format = options.output_format;
var size = options.size;
if (url == undefined) callback("No URL FOUND", null, null);
if (quality == undefined) quality = 100
if (output_format == undefined) output_format = "jpg"
var filename = GetFilename(url);
var extension = GetFileExtension(url);
if ((extension == "jpg" || extension == "gif" || extension == "png") && (output_format == "jpg" || output_format == "png")) {
try {
request.get(url, function(err, res, body) {
if (!res) {
callback(new Error("Network Error"), null, null)
return;
}
if (res.statusCode != 200) {
callback(new Error("Invalid URL"), null, null)
return;
}
var img = image(body);
var buffer;
if (size == undefined || size == "original") //original size
buffer = image(body).encode(output_format, {
quality: quality
});
else buffer = image(body).size(size).encode(output_format, {
quality: quality
});
callback(null, buffer, dataURLtoFile(buffer, filename, output_format));
});
} catch (e) {
callback(e, null, null);
}
} else {
callback(new Error("Unknown File Format"), null, null)
}
},
fromBuffer: function(options, callback) {
var buf = new Buffer(options.buffer.replace(/^data:image\/\w+;base64,/, ""), 'base64');
var quality = options.quality;
var output_format = options.output_format;
var size = options.size;
if (quality == undefined) quality = 100
if (output_format == undefined) output_format = "jpg"
if ((output_format == "jpg" || output_format == "png")) {
try {
var img = image(buf);
var buffer;
if (size == undefined || size == "original") //original size
buffer = img.encode(output_format, {
quality: quality
});
else buffer = img.size(size).encode(output_format, {
quality: quality
});
callback(null, buffer, dataURLtoFile(buffer, new Date().getTime(), output_format));
} catch (e) {
callback(e, null, null);
}
} else {
callback(new Error("Unknown File Format"), null, null)
}
}
catch(e){
callback(e.message,null);
}
}
}
}
{
"name": "image-convert",
"version": "0.1.32",
"version": "0.1.33",
"description": "convert image to different format like gif2jpg jpg2png also manage the quality & size",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},

@@ -9,0 +8,0 @@ "repository": {

# image-convert
convert image to different format like gif2jpg jpg2png also manage the quality & size
supported extensions jpg, png and gif
npm install image-convert --save
```
npm install image-convert --save
```
or
```
yarn add image-convert
```
## Usage
``` js
let imgConvert = require('image-convert').fromURL;
imgConvert({
let imgConvert = require('image-convert');
imgConvert.fromURL({
url:<url>,
quality: <quality[0..100]>,
output_format:<output_format>,
size: <size{integer}>},function(err,buffer,file){//buffer=> base64 encode, file=> file object
size: <size{integer}>},function(err,buffer,file){//buffer=> base64 encode, file=> file object
if(!err)

@@ -21,2 +27,11 @@ console.log(res);//img buffer

imgConvert.fromBuffer({
buffer:<buffer base64>,
quality: <quality[0..100]>,
output_format:<output_format>,
size: <size{integer}>},function(err,buffer,file){//buffer=> base64 encode, file=> file object
if(!err)
console.log(res);//img buffer
})
```

@@ -27,8 +42,8 @@

``` js
let imgConvert = require('image-convert').fromURL;
imgConvert({
let imgConvert = require('image-convert');
imgConvert.fromURL({
url:"https://upload.wikimedia.org/wikipedia/commons/thumb/2/2c/Rotating_earth_(large).gif/200px-Rotating_earth_(large).gif",
quality: 80,//default 100
output_format:"jpg",//default jpg
size: 300//defualt original
size: 300//default original
},function(err,buffer,file){

@@ -42,6 +57,24 @@ if(!err)

imgConvert.fromBuffer({
buffer: data:image/jpeg;base64,/9j...,//replace with buffer
quality: 50, //quality
output_format: "jpg", //jpg
size: "original" //defualt
}, function(err, response, file) {
if (!err) {
console.log(file);
res.end(response);
} else {
res.json({
"Error": err.message
})
}
});
```
jpg2gif and png2gif not available
## License
MIT

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc