Skip to content
This repository has been archived by the owner on May 4, 2020. It is now read-only.
/ scenic Public archive

Commit

Permalink
Create thumbnail and minified image for original.
Browse files Browse the repository at this point in the history
With gm, create a thumbnail and a minified image.
The thumbnail'll be used to show pictures in the channel,
and the minified image will be provided as a default image
cause of traffic issue.
  • Loading branch information
Hyeonje Alex Jun committed Nov 14, 2012
1 parent 883189d commit 776a077
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
65 changes: 54 additions & 11 deletions server/handlers.js
@@ -1,6 +1,7 @@
var settings = require('../settings'),
path = require('path'),
fs = require('fs'),
gm = require('gm'),
mongoose = require('mongoose');

// mongoose models
Expand Down Expand Up @@ -40,17 +41,19 @@ function Handlers(web) {
}

// create new picture
picture = new PictureModel({channel: channel});
var picture = new PictureModel({channel: channel}),
picture_dir = path.join(upload_dir, ''+picture._id);

var picture_dir = path.join(upload_dir, ''+picture._id);
fs.mkdir(picture_dir, '0775', function(err) {
if(err) {
response('mkdir_error:'+err);
return;
}

var picture_file = path.join(picture_dir, image.name);
fs.writeFile(picture_file, data, function(err) {
var ext = path.extname(image.name.toLowerCase()),
original = path.join(picture_dir, "original"+ext);

fs.writeFile(original, data, function(err) {
if(err) {
response('write_error:'+err);
return;
Expand All @@ -63,14 +66,54 @@ function Handlers(web) {
return;
}

picture.original = picture_file;
picture.uploaded = Date.now();
picture.save(function(err) {
if(err) {
response('save_error:'+err);
}
picture.original = original;

// get size
gm(original).size(function(err, size) {
// create thumbnail
var thumbnail_width = size.width >= size.height ? 300 : size.width * 300 / size.height,
thumbnail_height = size.width <= size.height ? 300 : size.height * 300 / size.width,
thumbnail = path.join(picture_dir, "thumbnail.jpg");

gm(original).resize(thumbnail_width, thumbnail_height).quality(90).write(thumbnail, function(err) {
if(err) {
response('thumbnail_error:'+err);
return;
}

picture.thumbnail = thumbnail;

var save = function() {
picture.upload = Date.now();
picture.save(function(err) {
if(err) {
response('save_err:'+err);
}

response('success');
});
};

// if the image is larger than 1mb or 1920x1080, create minified image.
if(image.size > 1024 * 1024 || size.width > 1920 || size.height > 1080) {
var minified_width = (size.width / size.height) >= (16 / 9) ? 1920 : 1080 * size.width / size.height,
minified_height = (size.width / size.height) <= (16 / 9) ? 1080 : 1920 * size.height / size.width,
minified = path.join(picture_dir, "minified.jpg");

gm(original).resize(minified_width, minified_height).quality(90).write(minified, function(err) {
if(err) {
response('minified_error:'+err);
return;
}

response('success');
picture.minified = minified;
save();
});
}
else {
save();
}
});
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions server/models.js
Expand Up @@ -4,6 +4,8 @@ var mongoose = require('mongoose'),
module.exports = function() {
var Pictures = new Schema({
original: String,
thumbnail: String,
minified: String,
channel: String,
width: Number,
height: Number,
Expand Down

0 comments on commit 776a077

Please sign in to comment.