Shenlin

Merge branch 'develop' into 'master'

优化图片



See merge request !1
...@@ -73,16 +73,16 @@ ...@@ -73,16 +73,16 @@
73 $editor.summernote({ 73 $editor.summernote({
74 height: 500, // set editor height 74 height: 500, // set editor height
75 minHeight: 300, // set minimum height of editor 75 minHeight: 300, // set minimum height of editor
76 - maxHeight: 760, // set maximum height of editor 76 + //maxHeight: 760, // set maximum height of editor
77 focus: true, // set focus to editable area after initializing summernote 77 focus: true, // set focus to editable area after initializing summernote
78 lang: 'zh-CN', 78 lang: 'zh-CN',
79 placeholder: '写点什么...', 79 placeholder: '写点什么...',
80 toolbar: [ 80 toolbar: [
81 ['font', ['fontname', 'fontsize', 'color']], 81 ['font', ['fontname', 'fontsize', 'color']],
82 ['font-style', ['bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript']], 82 ['font-style', ['bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript']],
83 + ['para', ['style', 'paragraph', 'height']],
83 ['media', ['gallery', 'picture', 'link', 'video']], 84 ['media', ['gallery', 'picture', 'link', 'video']],
84 ['insert', ['table', 'hr', 'ol', 'ul']], 85 ['insert', ['table', 'hr', 'ol', 'ul']],
85 - ['para', ['style', 'paragraph', 'height']],
86 ['misc', ['clear', 'undo', 'redo']], 86 ['misc', ['clear', 'undo', 'redo']],
87 ['send', ['publish']], 87 ['send', ['publish']],
88 ['tools', ['fullscreen', 'codeview', 'help']] 88 ['tools', ['fullscreen', 'codeview', 'help']]
...@@ -116,7 +116,12 @@ ...@@ -116,7 +116,12 @@
116 var file = result.content.files[0]||{}; 116 var file = result.content.files[0]||{};
117 console.log(file); 117 console.log(file);
118 if (file.size) { 118 if (file.size) {
119 - $editor.summernote('insertImage', 'http://domino.chuangcheng.co/appimg/000151418f2f4b82b924649fccd47eb5_1vuy5a.jpg', file.name); 119 + setTimeout(function () {
120 + $editor.summernote('insertImage', file.url, function ($image) {
121 + $image.css('width', $image.width() / 3);
122 + $image.attr('data-filename', file.name);
123 + });
124 + }, 1000); //等待上传处理图片
120 } 125 }
121 } 126 }
122 }); 127 });
......
...@@ -103,7 +103,7 @@ var configure = function () { ...@@ -103,7 +103,7 @@ var configure = function () {
103 //文件上传目录定义 103 //文件上传目录定义
104 upload: { 104 upload: {
105 root: '/opt/domino-img/', 105 root: '/opt/domino-img/',
106 - base_url: BASE_URL, 106 + base_url: null,
107 appimg: 'appimg/', 107 appimg: 'appimg/',
108 qrimg: 'qrimg/', 108 qrimg: 'qrimg/',
109 wximg: 'wximg/' 109 wximg: 'wximg/'
......
...@@ -23,9 +23,11 @@ module.exports = function Uploader(routerPath, db) { ...@@ -23,9 +23,11 @@ module.exports = function Uploader(routerPath, db) {
23 23
24 const 24 const
25 DEST_DIR = path.resolve(UPLOAD_OPTION.root, routerPath), 25 DEST_DIR = path.resolve(UPLOAD_OPTION.root, routerPath),
26 - IMAGE_URL = UPLOAD_OPTION.base_url + routerPath + '/', 26 + IMAGE_URL = UPLOAD_OPTION.base_url||'/' + routerPath + '/',
27 - THUMB_W = 240, 27 + THUMB_WIDTH = 240;
28 - THUMB_H = 240; 28 +
29 + //定义上传文件的访问路径
30 + router.use(`/${routerPath}`, express.static(DEST_DIR));
29 31
30 // var FileInfo = db.import(path.resolve(ENTITIES_PATH, 'file_info')); 32 // var FileInfo = db.import(path.resolve(ENTITIES_PATH, 'file_info'));
31 33
...@@ -41,30 +43,32 @@ module.exports = function Uploader(routerPath, db) { ...@@ -41,30 +43,32 @@ module.exports = function Uploader(routerPath, db) {
41 /** 43 /**
42 * 创建缩略图 44 * 创建缩略图
43 * @param gm_object gm对象,可以在此前对图片做好预处理 45 * @param gm_object gm对象,可以在此前对图片做好预处理
44 - * @param file 处理图片文件 46 + * @param file_orig 原始处理图片文件
45 * @param uploaded_file 上传文件结构 47 * @param uploaded_file 上传文件结构
46 */ 48 */
47 - var create_thumb = function (gm_object, file, uploaded_file) { 49 + var create_thumb = function (gm_object, file_orig, uploaded_file) {
48 - let dest_thumb = uploaded_file.thumb_path, dest_file = uploaded_file.file_path; 50 + let dest_thumb = uploaded_file.thumb_path, //缩略图
51 + dest_file = uploaded_file.file_path, //优化后的图片
52 + dest_orig = uploaded_file.orig_path; //原始图片
53 +
49 gm_object.size(function (err, size) { 54 gm_object.size(function (err, size) {
50 if (err) { 55 if (err) {
51 logger.error(err, uploaded_file); 56 logger.error(err, uploaded_file);
52 return; 57 return;
53 } 58 }
54 - if (size.width > THUMB_W || size.height > THUMB_H) { 59 + this.write(dest_file, function (err) {
55 - this.thumb(THUMB_W, THUMB_H, dest_thumb, 0, 'center', function (err) { 60 + if (err) {
56 - err ? logger.error(err, uploaded_file) : logger.debug('image saved to', uploaded_file); 61 + logger.error(err, uploaded_file);
57 - //move file to dest_path 62 + } else {
58 - fs.rename(file.path, dest_file); 63 + let thumb_w = size.width > size.height ? size.height : size.width; //计算最小边作为缩略图
59 - }); 64 + thumb_w = thumb_w > THUMB_WIDTH ? THUMB_WIDTH : thumb_w;
60 - } else { 65 + gm(dest_file).thumb(thumb_w, thumb_w, dest_thumb, 0, 'center', function (err) {
61 - //小图片直接复制 66 + err ? logger.error(err, uploaded_file) : logger.debug('image saved to', uploaded_file);
62 - this.write(dest_thumb, function (err) { 67 + //move file to dest_orig
63 - err ? logger.error(err, uploaded_file) : logger.debug('image saved to', uploaded_file); 68 + fs.rename(file_orig.path, dest_orig);
64 - //move file to dest_path 69 + });
65 - fs.rename(file.path, dest_file); 70 + }
66 - }) 71 + })
67 - }
68 }); 72 });
69 }; 73 };
70 74
...@@ -83,7 +87,7 @@ module.exports = function Uploader(routerPath, db) { ...@@ -83,7 +87,7 @@ module.exports = function Uploader(routerPath, db) {
83 if (files.hasOwnProperty(key)) { 87 if (files.hasOwnProperty(key)) {
84 let file = files[key]; 88 let file = files[key];
85 if (file && file.size) { 89 if (file && file.size) {
86 - let file_ext = file.type.replace('image/', ''); 90 + let file_ext = file.type.replace('image/', ''), orig_ext = file_ext;
87 switch (file.type) { 91 switch (file.type) {
88 case 'image/gif': 92 case 'image/gif':
89 file_ext = 'gif'; 93 file_ext = 'gif';
...@@ -103,7 +107,9 @@ module.exports = function Uploader(routerPath, db) { ...@@ -103,7 +107,9 @@ module.exports = function Uploader(routerPath, db) {
103 salt = (~~(Math.random() * 1000000000)).toString(36), 107 salt = (~~(Math.random() * 1000000000)).toString(36),
104 file_id = crypto_utils.UUID(), 108 file_id = crypto_utils.UUID(),
105 file_name = `${file_id}_${salt}.${file_ext}`, 109 file_name = `${file_id}_${salt}.${file_ext}`,
110 + orig_file_name = `${file_id}_${salt}_o.${orig_ext}`,
106 dest_file = path.resolve(DEST_DIR, file_name), 111 dest_file = path.resolve(DEST_DIR, file_name),
112 + dest_orig = path.resolve(DEST_DIR, orig_file_name),
107 dest_thumb = dest_file.replace(regexp, '_s.$1'), 113 dest_thumb = dest_file.replace(regexp, '_s.$1'),
108 uploaded_file = { 114 uploaded_file = {
109 id: file_id, 115 id: file_id,
...@@ -112,6 +118,7 @@ module.exports = function Uploader(routerPath, db) { ...@@ -112,6 +118,7 @@ module.exports = function Uploader(routerPath, db) {
112 size: file.size, 118 size: file.size,
113 file_path: dest_file, 119 file_path: dest_file,
114 thumb_path: dest_thumb, 120 thumb_path: dest_thumb,
121 + orig_path: dest_orig,
115 hash: file.hash, 122 hash: file.hash,
116 url: IMAGE_URL + file_name, 123 url: IMAGE_URL + file_name,
117 params: fields, 124 params: fields,
......