//Description: Image upload Preview plugin//when uploading, you can generate equal scale graphs in a fixed width and height range.//parameter settings://Width store picture fixed size container width//height storage picture fixed size container high//Imgdiv The ID of the jquery page Div//maxSize maximum picture size limit (K)//imgtype array suffix name//********************** image upload Preview plugin *************************(function($) {jQuery.fn.extend ({uploadpreview:function(opts) {opts=jquery.extend ({width:0, Height:0, Imgdiv:"#imgDiv", MaxSize:300, Imgtype: ["GIF", "JPEG", "JPG", "BMP", "PNG"], callback:function() {return false; } }, OPTs|| {}); //var _self = this; var_this = $ ( This); varImgdiv =$ (OPTS.IMGDIV); Imgdiv.width (Opts.width); Imgdiv.height (Opts.height); varVersion = parseint ($.browser.version, 10); AutoScaling=function () { if(Version = = 7 | | version = 8 | | version = = 9) imgdiv.get (0). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). Sizingmethod = "image"; varImg_width =imgdiv.width (); varImg_height =imgdiv.height (); if(Img_width > 0 && img_height > 0) { varRate = (Opts.width/img_width < opts.height/img_height)? Opts.width/img_width:opts.height/Img_height; if(Rate <= 1) { if(Version = = 7 | | version = 8 | | version = = 9) imgdiv.get (0). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). Sizingmethod = "scale"; Imgdiv.width (Img_width*Rate ); Imgdiv.height (Img_height*Rate ); } Else{imgdiv.width (img_width); Imgdiv.height (Img_height); } varleft = (Opts.width-imgdiv.width ()) * 0.5; vartop = (Opts.height-imgdiv.height ()) * 0.5; Imgdiv.css ({"Margin-left": Left, "Margin-top": Top}); Imgdiv.show (); }}, Createimg=function() {imgdiv.html (‘‘); varIMG = $ ("); Imgdiv.replacewith (IMG); Imgdiv=img; }, _this.change (function () { if( This. Value) { if(! REGEXP ("\. (" + Opts.imgType.join ("|") + ") $", "I"). Test ( This. Value.tolowercase ())) {Alert ("Picture type must be" + opts.imgType.join (",") + "); This. Value = ""; return false; } imgdiv.hide (); if($.browser.msie && Version < 10) { if(Version = = 6) { varImage =NewImage (); Image.onload=function () { if((image.filesize/1024) >opts.maxsize) {alert (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false; }} image.src= ' file:///' + This. Value; Createimg (); Imgdiv.attr (' SRC ', IMAGE.SRC); AutoScaling (); } Else { This. Select (); varIMG =Document.selection.createRange (). text; varImage =NewImage (); Image.onload=function () { if((image.filesize/1024) >opts.maxsize) {alert (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false; }} image.src=img; Imgdiv.html (‘‘); Imgdiv.css ({filter:"Progid:DXImageTransform.Microsoft.AlphaImageLoader (Sizingmethod=image)" }); Imgdiv.get (0). Filters.item ("DXImageTransform.Microsoft.AlphaImageLoader"). Sizingmethod = "image"; Try{imgdiv.get (0). Filters.item (' DXImageTransform.Microsoft.AlphaImageLoader '). src =img; } Catch(e) {alert ("Invalid picture File! "); return; } setTimeout ("AutoScaling ()", 100); } } Else { Try { varFile =NULL; varSize = 0; if( This. Files && This. files[0]) {file= This. files[0]; Size=file.size; } Else if( This. Files && This. Files.item (0) ) {file= This. Files.item (0); Size=file.filesize; } if((size/1024) >opts.maxsize) {alert (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false; } createimg (); //Firefox is unable to get the full file path directly through Input[file].value due to security issues Try { //Firefox7.0 belowImgdiv.attr (' src ', File.getasdataurl ()); } Catch(e) {//Firefox8.0 or moreImgdiv.attr (' src ', window. Url.createobjecturl (file)); } imgdiv.css ({"Vertical-align": "Middle" }); SetTimeout ("AutoScaling ()", 100); } Catch(e) {//browsers that support HTML5, such as high-version Firefox, Chrome, Ie10 if( This. Files && This. files[0]) { if(( This. files[0].size/1024) >opts.maxsize) {alert (' Picture size cannot exceed ' + opts.maxsize + ' K '); return false; } varReader =NewFileReader (); Reader.onload=function(e) {imgdiv.attr (' SRC ', E.target.result); }; Reader.readasdataurl ( This. files[0]); SetTimeout ("AutoScaling ()", 100); } }; } } }); } });}) (jQuery);
HTML code:
<Divstyle= "width:290px; height:290px; overflow:hidden; border:1px solid red;"> <DivID= "Imgdiv"></Div> </Div> <BR> <inputtype= "File"value= "Upload file"> <Script>$ (document). Ready (function () { $("input"). Uploadpreview ({width:290, Height:290, Imgdiv:"#imgDiv" }); });</Script>
Upload Image preview, support IE6