Shenlin

Merge branch 'develop' into 'master'

优化图片



See merge request !1
......@@ -73,16 +73,16 @@
$editor.summernote({
height: 500, // set editor height
minHeight: 300, // set minimum height of editor
maxHeight: 760, // set maximum height of editor
//maxHeight: 760, // set maximum height of editor
focus: true, // set focus to editable area after initializing summernote
lang: 'zh-CN',
placeholder: '写点什么...',
toolbar: [
['font', ['fontname', 'fontsize', 'color']],
['font-style', ['bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript']],
['para', ['style', 'paragraph', 'height']],
['media', ['gallery', 'picture', 'link', 'video']],
['insert', ['table', 'hr', 'ol', 'ul']],
['para', ['style', 'paragraph', 'height']],
['misc', ['clear', 'undo', 'redo']],
['send', ['publish']],
['tools', ['fullscreen', 'codeview', 'help']]
......@@ -116,7 +116,12 @@
var file = result.content.files[0]||{};
console.log(file);
if (file.size) {
$editor.summernote('insertImage', 'http://domino.chuangcheng.co/appimg/000151418f2f4b82b924649fccd47eb5_1vuy5a.jpg', file.name);
setTimeout(function () {
$editor.summernote('insertImage', file.url, function ($image) {
$image.css('width', $image.width() / 3);
$image.attr('data-filename', file.name);
});
}, 1000); //等待上传处理图片
}
}
});
......
......@@ -103,7 +103,7 @@ var configure = function () {
//文件上传目录定义
upload: {
root: '/opt/domino-img/',
base_url: BASE_URL,
base_url: null,
appimg: 'appimg/',
qrimg: 'qrimg/',
wximg: 'wximg/'
......
......@@ -23,9 +23,11 @@ module.exports = function Uploader(routerPath, db) {
const
DEST_DIR = path.resolve(UPLOAD_OPTION.root, routerPath),
IMAGE_URL = UPLOAD_OPTION.base_url + routerPath + '/',
THUMB_W = 240,
THUMB_H = 240;
IMAGE_URL = UPLOAD_OPTION.base_url||'/' + routerPath + '/',
THUMB_WIDTH = 240;
//定义上传文件的访问路径
router.use(`/${routerPath}`, express.static(DEST_DIR));
// var FileInfo = db.import(path.resolve(ENTITIES_PATH, 'file_info'));
......@@ -41,30 +43,32 @@ module.exports = function Uploader(routerPath, db) {
/**
* 创建缩略图
* @param gm_object gm对象,可以在此前对图片做好预处理
* @param file 处理图片文件
* @param file_orig 原始处理图片文件
* @param uploaded_file 上传文件结构
*/
var create_thumb = function (gm_object, file, uploaded_file) {
let dest_thumb = uploaded_file.thumb_path, dest_file = uploaded_file.file_path;
var create_thumb = function (gm_object, file_orig, uploaded_file) {
let dest_thumb = uploaded_file.thumb_path, //缩略图
dest_file = uploaded_file.file_path, //优化后的图片
dest_orig = uploaded_file.orig_path; //原始图片
gm_object.size(function (err, size) {
if (err) {
logger.error(err, uploaded_file);
return;
}
if (size.width > THUMB_W || size.height > THUMB_H) {
this.thumb(THUMB_W, THUMB_H, dest_thumb, 0, 'center', function (err) {
err ? logger.error(err, uploaded_file) : logger.debug('image saved to', uploaded_file);
//move file to dest_path
fs.rename(file.path, dest_file);
});
this.write(dest_file, function (err) {
if (err) {
logger.error(err, uploaded_file);
} else {
//小图片直接复制
this.write(dest_thumb, function (err) {
let thumb_w = size.width > size.height ? size.height : size.width; //计算最小边作为缩略图
thumb_w = thumb_w > THUMB_WIDTH ? THUMB_WIDTH : thumb_w;
gm(dest_file).thumb(thumb_w, thumb_w, dest_thumb, 0, 'center', function (err) {
err ? logger.error(err, uploaded_file) : logger.debug('image saved to', uploaded_file);
//move file to dest_path
fs.rename(file.path, dest_file);
})
//move file to dest_orig
fs.rename(file_orig.path, dest_orig);
});
}
})
});
};
......@@ -83,7 +87,7 @@ module.exports = function Uploader(routerPath, db) {
if (files.hasOwnProperty(key)) {
let file = files[key];
if (file && file.size) {
let file_ext = file.type.replace('image/', '');
let file_ext = file.type.replace('image/', ''), orig_ext = file_ext;
switch (file.type) {
case 'image/gif':
file_ext = 'gif';
......@@ -103,7 +107,9 @@ module.exports = function Uploader(routerPath, db) {
salt = (~~(Math.random() * 1000000000)).toString(36),
file_id = crypto_utils.UUID(),
file_name = `${file_id}_${salt}.${file_ext}`,
orig_file_name = `${file_id}_${salt}_o.${orig_ext}`,
dest_file = path.resolve(DEST_DIR, file_name),
dest_orig = path.resolve(DEST_DIR, orig_file_name),
dest_thumb = dest_file.replace(regexp, '_s.$1'),
uploaded_file = {
id: file_id,
......@@ -112,6 +118,7 @@ module.exports = function Uploader(routerPath, db) {
size: file.size,
file_path: dest_file,
thumb_path: dest_thumb,
orig_path: dest_orig,
hash: file.hash,
url: IMAGE_URL + file_name,
params: fields,
......