Skip to content
This repository was archived by the owner on Aug 22, 2022. It is now read-only.

Commit 0e43505

Browse files
committed
add ie6/7 image preview feature
1 parent 9d32a15 commit 0e43505

5 files changed

Lines changed: 91 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/node_modules
55
/doc
66
server/*
7+
!server/preview.php
78
!server/fileupload.php
89
!server/fileupload2.php
910
!server/crossdomain.xml

examples/upload.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
i = 0,
151151
// 修改js类型
152152
unAllowed = 'text/plain;application/javascript ';
153-
153+
154154
for ( ; i < len; i++ ) {
155155
// 如果在列表里面
156156
if ( ~unAllowed.indexOf( items[ i ].type ) ) {
@@ -222,16 +222,30 @@
222222
// @todo lazyload
223223
$wrap.text( '预览中' );
224224
uploader.makeThumb( file, function( error, src ) {
225+
var img;
226+
225227
if ( error ) {
226228
$wrap.text( '不能预览' );
227229
return;
228230
}
229-
if( !isSupportBase64 ) {
230-
// 针对不支持base64的浏览器单独处理
231-
// src = 'http://f9.topit.me/9/dd/6d/11206448174286ddd9l.jpg';
231+
232+
if( isSupportBase64 ) {
233+
img = $('<img src="'+src+'">');
234+
$wrap.empty().append( img );
235+
} else {
236+
$.ajax('../server/preview.php', {
237+
method: 'POST',
238+
data: src,
239+
dataType:'json'
240+
}).done(function( response ) {
241+
if (response.result) {
242+
img = $('<img src="'+response.result+'">');
243+
$wrap.empty().append( img );
244+
} else {
245+
$wrap.text("预览出错");
246+
}
247+
});
232248
}
233-
var img = $('<img src="'+src+'">');
234-
$wrap.empty().append( img );
235249
}, thumbnailWidth, thumbnailHeight );
236250

237251
percentages[ file.id ] = [ file.size, 0 ];

server/fileupload.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* Contributing: http://www.plupload.com/contributing
1010
*/
1111

12+
#!! 注意
13+
#!! 此文件只是个示例,不要用于真正的产品之中。
14+
#!! 不保证代码安全性。
15+
1216
#!! IMPORTANT:
1317
#!! this file is just an example, it doesn't incorporate any security checks and
1418
#!! is not recommended to be used in production environment as it is. Be sure to
@@ -47,7 +51,7 @@
4751
@set_time_limit(5 * 60);
4852

4953
// Uncomment this one to fake upload time
50-
usleep(5000);
54+
// usleep(5000);
5155

5256
// Settings
5357
// $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
@@ -77,13 +81,6 @@
7781
$fileName = uniqid("file_");
7882
}
7983

80-
$md5File = @file('md5list.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
81-
$md5File = $md5File ? $md5File : array();
82-
83-
if (isset($_REQUEST["md5"]) && array_search($_REQUEST["md5"], $md5File ) !== FALSE ) {
84-
die('{"jsonrpc" : "2.0", "result" : null, "id" : "id", "exist": 1}');
85-
}
86-
8784
$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
8885
$uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $fileName;
8986

server/preview.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* 此页面用来协助 IE6/7 预览图片,因为 IE 6/7 不支持 base64
4+
*/
5+
6+
$DIR = 'preview';
7+
// Create target dir
8+
if (!file_exists($DIR)) {
9+
@mkdir($DIR);
10+
}
11+
12+
$cleanupTargetDir = true; // Remove old files
13+
$maxFileAge = 5 * 3600; // Temp file age in seconds
14+
15+
if ($cleanupTargetDir) {
16+
if (!is_dir($DIR) || !$dir = opendir($DIR)) {
17+
die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
18+
}
19+
20+
while (($file = readdir($dir)) !== false) {
21+
$tmpfilePath = $DIR . DIRECTORY_SEPARATOR . $file;
22+
23+
// Remove temp file if it is older than the max age and is not the current file
24+
if (@filemtime($tmpfilePath) < time() - $maxFileAge) {
25+
@unlink($tmpfilePath);
26+
}
27+
}
28+
closedir($dir);
29+
}
30+
31+
$src = file_get_contents('php://input');
32+
33+
if (preg_match("#^data:image/(\w+);base64,(.*)$#", $src, $matches)) {
34+
35+
$previewUrl = sprintf(
36+
"%s://%s%s",
37+
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
38+
$_SERVER['HTTP_HOST'],
39+
$_SERVER['REQUEST_URI']
40+
);
41+
$previewUrl = str_replace("preview.php", "", $previewUrl);
42+
43+
44+
$base64 = $matches[2];
45+
$type = $matches[1];
46+
if ($type === 'jpeg') {
47+
$type = 'jpg';
48+
}
49+
50+
$filename = md5($base64).".$type";
51+
$filePath = $DIR.DIRECTORY_SEPARATOR.$filename;
52+
53+
if (file_exists($filePath)) {
54+
die('{"jsonrpc" : "2.0", "result" : "'.$previewUrl.'preview/'.$filename.'", "id" : "id"}');
55+
} else {
56+
$data = base64_decode($base64);
57+
file_put_contents($filePath, $data);
58+
die('{"jsonrpc" : "2.0", "result" : "'.$previewUrl.'preview/'.$filename.'", "id" : "id"}');
59+
}
60+
61+
} else {
62+
die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "un recoginized source"}}');
63+
}

src/widgets/image.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ define([
7878

7979
// 为空的话则保留原有图片格式。
8080
// 否则强制转换成指定的类型。
81+
// IE 8下面 base64 大小不能超过 32K 否则预览失败,而非 jpeg 编码的图片很可
82+
// 能会超过 32k, 所以这里设置成预览的时候都是 image/jpeg
8183
type: 'image/jpeg'
8284
},
8385

0 commit comments

Comments
 (0)