Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:12

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 3000
CMD [ "npm start" ]
59 changes: 56 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,65 @@ Performs operations
- Rotate left
- Rotate right

## API usuage
## API usage

http://localhost:3000/image?resize=400,300&greyscale=1&rotate=60

http://localhost:3000/image?thumbail=&rotate=70&resize=200,400&greyscale

### Swagger

## sharp
API docs - http://localhost:3000/api-docs

https://sharp.pixelplumbing.com/

### CURL

```shell
curl -X POST -F "image=@/pathtoimagefifle" --output -i "localhost:3000/image?resize=400,300&greyscale=1&rotate=60"
```
Example
```shell
curl -X POST -F "image=@/home/altanai/Desktop/Altanai-Bisht1.jpg" --output -i "localhost:3000/image?resize=400,300&greyscale=1&rotate=60"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 147k 100 30661 100 117k 1575k 6163k --:--:-- --:--:-- --:--:-- 7352k
```



## Dockerizing

Make image
```shell
docker build -t altanai/imageprocessor-sharp-api .
```

Run Image
```shell
docker run -p 3000:3000 -d altanai/imageprocessor-sharp-api
```

Other operations on docker
```shell
# Get container ID
$ docker ps

# Print app output
$ docker logs <container id>

# Enter the container
$ docker exec -it <container id> /bin/bash
```

## References

### sharp
https://sharp.pixelplumbing.com/

### Nodejs Streams


### Swagger
https://swagger.io/docs/

### Docker
Binary file not shown.
Binary file removed image/Screenshot from 2021-03-04 22-08-09.png
Binary file not shown.
37 changes: 24 additions & 13 deletions lib/processImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,46 @@ var lib = {
const sharpStream = sharp({
failOnError: true
});
console.log("Oplist ", oplist);
const transformer = sharp(imgfile)
.pipe(sharpStream);
oplist.reduce((_,op)=>{
oplist.reduce((_, op) => {
console.log("Op ", op.opname, op.options);
switch (op.opname) {
case 'greyscale':
console.log("greyscale");
// console.log("greyscale");
transformer.pipe(sharpStream.greyscale());
break;
case 'flip':
break;
case 'thumbnail':
console.log("thumbnail");
transformer.pipe(sharpStream.resize(200,200, {fit: sharp.fit.cover}));
// console.log("thumbnail");
transformer.pipe(sharpStream.resize(200, 200, {fit: sharp.fit.cover}));
break;
case 'rotate':
console.log("rotate");
transformer.pipe(sharpStream.rotate(parseInt(op.options||0)));
// console.log("rotate", op.options);
transformer.pipe(sharpStream.rotate(parseInt(op.options || 0)));
break;
case 'rotateleft':
console.log("rotateleft");
// console.log("rotateleft");
transformer.pipe(sharpStream.rotate(90));
break;
case 'rotateright':
console.log("rotateright");
// console.log("rotateright");
transformer.pipe(sharpStream.rotate(270));
break;
case 'resize':
console.log("resize");
transformer.pipe(sharpStream.resize(op.options));
// console.log("resize ", op.options);
let opt = op.options;
if (opt && opt.includes(",")) {
let size = opt.split(",");
transformer.pipe(sharpStream.resize({
height: parseInt(size[0]),
width: parseInt(size[1])
}))
.on('error', (err)=>{ return err });
} else {
transformer.pipe(sharpStream.resize(parseInt(opt)));
}

break;
case 'end':
transformer.pipe(
Expand All @@ -50,7 +61,7 @@ var lib = {
console.log("Unmatched / Default case ")
break;
}
});
}, {});

return transformer;
}
Expand Down
Loading