kindeditor 限制上传图片大小及宽高

进入:/kindeditor/plugins/image/image.js,替换其中的 self.plugin.imageDialog = function (options)方法,代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
self.plugin.imageDialog = function (options) {
        var imageUrl = options.imageUrl,
            imageWidth = K.undef(options.imageWidth, ''),
            imageHeight = K.undef(options.imageHeight, ''),
            imageTitle = K.undef(options.imageTitle, ''),
            imageAlign = K.undef(options.imageAlign, ''),
                   imageSize = options.imageSize*1024,
            showRemote = K.undef(options.showRemote, true),
            showLocal = K.undef(options.showLocal, true),
            tabIndex = K.undef(options.tabIndex, 0),
            clickFn = options.clickFn;
        var target = 'kindeditor_upload_iframe_' + new Date().getTime();
        var hiddenElements = [];
        for (var k in extraParams) {
            hiddenElements.push('<input type="hidden" name="' + k + '" value="' + extraParams[k] + '" />');
        }
        var html = [
            '<div style="padding:20px;">',
            //tabs
            '<div class="tabs"></div>',
            //remote image - start
            '<div class="tab1" style="display:none;">',
            //url
            '<div class="ke-dialog-row">',
            '<label for="remoteUrl" style="width:60px;">' + lang.remoteUrl + '</label>',
            '<input type="text" id="remoteUrl" class="ke-input-text" name="url" value="" style="width:200px;" />  ',
            '<span class="ke-button-common ke-button-outer">',
            '<input type="button" class="ke-button-common ke-button" name="viewServer" value="' + lang.viewServer + '" />',
            '</span>',
            '</div>',
            //size
            '<div class="ke-dialog-row">',
            '<label for="remoteWidth" style="width:60px;">' + lang.size + '</label>',
            lang.width + ' <input type="text" id="remoteWidth" class="ke-input-text ke-input-number" name="width" value="" maxlength="4" /> ',
            lang.height + ' <input type="text" class="ke-input-text ke-input-number" name="height" value="" maxlength="4" /> ',
            '<img class="ke-refresh-btn" src="' + imgPath + 'refresh.png" width="16" height="16" alt="" style="cursor:pointer;" title="' + lang.resetSize + '" />',
            '</div>',
            //align
            '<div class="ke-dialog-row">',
            '<label style="width:60px;">' + lang.align + '</label>',
            '<input type="radio" name="align" class="ke-inline-block" value="" checked="checked" /> <img name="defaultImg" src="' + imgPath + 'align_top.gif" width="23" height="25" alt="" />',
            ' <input type="radio" name="align" class="ke-inline-block" value="left" /> <img name="leftImg" src="' + imgPath + 'align_left.gif" width="23" height="25" alt="" />',
            ' <input type="radio" name="align" class="ke-inline-block" value="right" /> <img name="rightImg" src="' + imgPath + 'align_right.gif" width="23" height="25" alt="" />',
            '</div>',
            //title
            '<div class="ke-dialog-row">',
            '<label for="remoteTitle" style="width:60px;">' + lang.imgTitle + '</label>',
            '<input type="text" id="remoteTitle" class="ke-input-text" name="title" value="" style="width:200px;" />',
            '</div>',
            '</div>',
            //remote image - end
            //local upload - start
            '<div class="tab2" style="display:none;">',
            '<iframe name="' + target + '" style="display:none;"></iframe>',
            '<form class="ke-upload-area ke-form" method="post" enctype="multipart/form-data" target="' + target + '" action="' + K.addParam(uploadJson, 'dir=image') + '">',
            //file
            '<div class="ke-dialog-row">',
            hiddenElements.join(''),
            '<label style="width:60px;">' + lang.localUrl + '</label>',
            '<input type="text" name="localUrl" class="ke-input-text" tabindex="-1" style="width:200px;" readonly="true" />  ',
            '<input type="button" class="ke-upload-button" value="' + lang.upload + '" />',
            '</div>',
            '</form>',
            '</div>',
            //local upload - end
            '</div>'
        ].join('');
        var dialogWidth = showLocal || allowFileManager ? 450 : 400,
            dialogHeight = showLocal && showRemote ? 300 : 250;
        var dialog = self.createDialog({
            name: name,
            width: dialogWidth,
            height: dialogHeight,
            title: self.lang(name),
            body: html,
            yesBtn: {
                name: self.lang('yes'),
                click: function (e) {
                    // Bugfix: http://code.google.com/p/kindeditor/issues/detail?id=319
                    if (dialog.isLoading) {
                        return;
                    }
 
                    var f = uploadbutton.fileBox[0].files[0];
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var data = e.target.result;
                        //加载图片获取图片真实宽度和高度
                        var image = new Image();
                        image.onload = function () {
                            var width = image.width;
                            var height = image.height;
                            if (f.size > imageSize) {
                                alert("图片大小超过限制");
                                return;
 
                            }
 
                            if (width != imageWidth || height != imageHeight) {
                                alert("图片尺寸不对");
                                return;
                            }
                             
 
                            // insert local image
                            if (showLocal && showRemote && tabs && tabs.selectedIndex === 1 || !showRemote) {
                                if (uploadbutton.fileBox.val() == '') {
                                    alert(self.lang('pleaseSelectFile'));
                                    return;
                                }
                                dialog.showLoading(self.lang('uploadLoading'));
                                uploadbutton.submit();
                                localUrlBox.val('');
                                return;
                            }
                            // insert remote image
                            var url = K.trim(urlBox.val()),
                                width = widthBox.val(),
                                height = heightBox.val(),
                                title = titleBox.val(),
                                align = '';
                            alignBox.each(function () {
                                if (this.checked) {
                                    align = this.value;
                                    return false;
                                }
                            });
 
                            if (url == 'http://' || K.invalidUrl(url)) {
                                alert(self.lang('invalidUrl'));
                                urlBox[0].focus();
                                return;
                            }
                            if (!/^\d*$/.test(width)) {
                                alert(self.lang('invalidWidth'));
                                widthBox[0].focus();
                                return;
                            }
                            if (!/^\d*$/.test(height)) {
                                alert(self.lang('invalidHeight'));
                                heightBox[0].focus();
                                return;
                            }
                            clickFn.call(self, url, title, width, height, 0, align);
 
 
                        };
                        image.src = data;
                    };
                    reader.readAsDataURL(f);
 
                }
            },
            beforeRemove: function () {
                viewServerBtn.unbind();
                widthBox.unbind();
                heightBox.unbind();
                refreshBtn.unbind();
            }
        }),
        div = dialog.div;
 
        var urlBox = K('[name="url"]', div),
            localUrlBox = K('[name="localUrl"]', div),
            viewServerBtn = K('[name="viewServer"]', div),
            widthBox = K('.tab1 [name="width"]', div),
            heightBox = K('.tab1 [name="height"]', div),
            refreshBtn = K('.ke-refresh-btn', div),
            titleBox = K('.tab1 [name="title"]', div),
            alignBox = K('.tab1 [name="align"]', div);
 
        var tabs;
        if (showRemote && showLocal) {
            tabs = K.tabs({
                src: K('.tabs', div),
                afterSelect: function (i) { }
            });
            tabs.add({
                title: lang.remoteImage,
                panel: K('.tab1', div)
            });
            tabs.add({
                title: lang.localImage,
                panel: K('.tab2', div)
            });
            tabs.select(tabIndex);
        } else if (showRemote) {
            K('.tab1', div).show();
        } else if (showLocal) {
            K('.tab2', div).show();
        }
 
        var uploadbutton = K.uploadbutton({
            button: K('.ke-upload-button', div)[0],
            fieldName: filePostName,
            form: K('.ke-form', div),
            target: target,
            width: 60,
            afterUpload: function (data) {
                dialog.hideLoading();
                if (data.error === 0) {
                    var url = data.url;
                    if (formatUploadUrl) {
                        url = K.formatUrl(url, 'absolute');
                    }
                    if (self.afterUpload) {
                        self.afterUpload.call(self, url, data, name);
                    }
                    if (!fillDescAfterUploadImage) {
                        clickFn.call(self, url, data.title, data.width, data.height, data.border, data.align);
                    } else {
                        K(".ke-dialog-row #remoteUrl", div).val(url);
                        K(".ke-tabs-li", div)[0].click();
                        K(".ke-refresh-btn", div).click();
                    }
                } else {
                    alert(data.message);
                }
            },
            afterError: function (html) {
                dialog.hideLoading();
                self.errorDialog(html);
            }
        });
        uploadbutton.fileBox.change(function (e) {
            localUrlBox.val(uploadbutton.fileBox.val());
        });
        if (allowFileManager) {
            viewServerBtn.click(function (e) {
                self.loadPlugin('filemanager', function () {
                    self.plugin.filemanagerDialog({
                        viewType: 'VIEW',
                        dirName: 'image',
                        clickFn: function (url, title) {
                            if (self.dialogs.length > 1) {
                                K('[name="url"]', div).val(url);
                                if (self.afterSelectFile) {
                                    self.afterSelectFile.call(self, url);
                                }
                                self.hideDialog();
                            }
                        }
                    });
                });
            });
        } else {
            viewServerBtn.hide();
        }
        var originalWidth = 0, originalHeight = 0;
        function setSize(width, height) {
            widthBox.val(width);
            heightBox.val(height);
            originalWidth = width;
            originalHeight = height;
        }
        refreshBtn.click(function (e) {
            var tempImg = K('<img src="' + urlBox.val() + '" />', document).css({
                position: 'absolute',
                visibility: 'hidden',
                top: 0,
                left: '-1000px'
            });
            tempImg.bind('load', function () {
                setSize(tempImg.width(), tempImg.height());
                tempImg.remove();
            });
            K(document.body).append(tempImg);
        });
        widthBox.change(function (e) {
            if (originalWidth > 0) {
                heightBox.val(Math.round(originalHeight / originalWidth * parseInt(this.value, 10)));
            }
        });
        heightBox.change(function (e) {
            if (originalHeight > 0) {
                widthBox.val(Math.round(originalWidth / originalHeight * parseInt(this.value, 10)));
            }
        });
        urlBox.val(options.imageUrl);
        setSize(options.imageWidth, options.imageHeight);
        titleBox.val(options.imageTitle);
        alignBox.each(function () {
            if (this.value === options.imageAlign) {
                this.checked = true;
                return false;
            }
        });
        if (showRemote && tabIndex === 0) {
            urlBox[0].focus();
            urlBox[0].select();
        }
        return dialog;
    };

  

posted on   落叶子  阅读(926)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示