Vote count:
0
I need to create 4 image sizes from an uploaded photo: large, medium, small, and extra small. It's working for the most part with the code below, but also the large and medium sizes need to have a watermark put in the bottom left, but not the smaller two sizes.
It seems like for the watermarked images, I'll need to duplicate the file stream and save out a separate instance for each one. Speed and efficiency are important here, so I want to make sure I'm doing this the best way.
The GM node module is really lacking in documentation. For more information they link to the GraphicsMagick site, which doesn't help if you're trying to do this with the GM module. It's really frustrating.
So basically, I could use help figuring out how to do the watermarks on the two larger sizes, and then also I just want to know if the code below is as efficient as it could be. It seems a bit slow when creating the 4 sizes on my local machine.
var fileName = req.files.photo.name,
fileBaseName = fileName.substr(0, fileName.lastIndexOf('.')),
uploadRoot = SiteConfig.root + '/upload/',
photosRoot = SiteConfig.root + '/photos/',
publicRoot = SiteConfig.root + '/public/';
require('fs').rename(
req.files.photo.path,
uploadRoot + fileName,
function(error)
{
if (error)
{
res.send({ error: 'upload error' });
return;
}
var ImageSizes = {
large: {
width: 990,
height: 990
},
medium: {
width: 550,
height: 550
},
small: {
width: 145,
height: 145
},
xsmall: {
width: 55,
height: 55
}
};
var GM = require('gm'),
fileStream = require('fs').createReadStream(photosRoot + fileName);
var lgPath = photosRoot + fileBaseName + '_lg.jpg',
mdPath = photosRoot + fileBaseName + '_md.jpg',
smPath = photosRoot + fileBaseName + '_sm.jpg',
xsPath = photosRoot + fileBaseName + '_xs.jpg';
// I'm guessing the second parameter is to set the format
GM(fileStream, 'img.jpg')
.size(
{
bufferStream: true
},
function(err, size)
{
console.log(size.width);
console.log(size.height);
if (size.width > ImageSizes.large.width || size.height > ImageSizes.large.height)
this.resize(ImageSizes.large.width, ImageSizes.large.height);
// Auto-orient based on EXIF data then remove EXIF data
this
.autoOrient()
.noProfile()
.quality(70)
.write(
lgPath,
function (err)
{
if (!err)
{
console.log('write large done');
this
.resize(ImageSizes.medium.width, ImageSizes.medium.height)
// watermark code - i want to continue using the file stream instead of a file path
//.subCommand('composite')
//.gravity('Center')
//.in('-compose', 'Over', watermarkFilePath, baseFilePath)
.quality(70)
.write(
mdPath,
function (err)
{
if (!err)
{
console.log('write medium done');
this
.resize(ImageSizes.small.width, ImageSizes.small.height)
.crop(ImageSizes.small.width, ImageSizes.small.height)
.quality(70)
.write(
smPath,
function (err)
{
if (!err)
{
console.log('write small done');
this
.resize(ImageSizes.xsmall.width, ImageSizes.xsmall.height)
.quality(70)
.write(
xsPath,
function (err)
{
if (!err)
console.log('write xsmall done');
else
console.log('write xsmall error');
}
);
}
else
console.log('write small error');
}
);
}
else
console.log('write medium error');
}
);
}
else
console.log('write large error');
}
);
}
);
}
);
Aucun commentaire:
Enregistrer un commentaire