// ********************** Image Upload preview plug-in ************ ************
// Author: IDDQD)
// Email: iddqd5376@163.com
// Http://blog.sina.com.cn/iddqd
// Version 1.0
// Description: Image Upload preview plug-in
// You can generate proportional scaling charts within the fixed width and height range during upload.
// Parameter settings:
// Width stores the image fixed-size container width
// Height stores the Image height of a fixed-size container
// The JQuery id of the imgDiv page DIV
// ImgType array suffix
// ********************** Image Upload preview plug-in ************ *************
(Function ($ ){
JQuery. fn. extend ({
UploadPreview: function (opts ){
Opts = jQuery. extend ({
Width: 0,
Height: 0,
ImgDiv: "# imgDiv ",
ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
Callback: function () {return false ;}
}, Opts | {});
VaR _ Self = this;
VaR _ this = $ (this );
VaR imgdiv =$ (OPTs. imgdiv );
Imgdiv. Width (OPTs. width );
Imgdiv. Height (OPTs. Height );
Autoscaling = function (){
If ($. browser. version = "7.0" | $. browser. version = "8.0") imgdiv. get (0 ). filters. item ("DXImageTransform. microsoft. alphaimageloader "). sizingmethod = "image ";
VaR img_width = imgdiv. Width ();
VaR img_height = imgdiv. Height ();
If (img_width> 0 & img_height> 0 ){
VaR rate = (OPTs. width/img_width <opts. Height/img_height )? Opts. width/img_width: opts. Height/img_height;
If (rate <= 1 ){
If ($. browser. version = "7.0" | $. browser. version = "8.0") 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 );
}
Var left = (opts. width-imgDiv. width () * 0.5;
Var top = (opts. height-imgDiv. height () * 0.5;
ImgDiv.css ({"margin-left": left, "margin-top": top });
ImgDiv. show ();
}
}
_ This. change (function (){
If (this. value ){
If (! RegExp (". (" + opts. imgType. join ("|") + ") $", "I"). test (this. value. toLowerCase ())){
Alert ("the image type must be" + opts. imgType. join (",") + ");
This. value = "";
Return false;
}
ImgDiv. hide ();
If ($. browser. msie ){
If ($. browser. version = "6.0 "){
Var img = $ (" ");
ImgDiv. replaceWith (img );
ImgDiv = img;
Var image = new Image ();
Image. src = 'file: // '+ this. value;
ImgDiv. attr ('src', image. src );
AutoScaling ();
}
Else {
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 = this. value;
} Catch (e ){
Alert ("invalid image file! ");
Return;
}
SetTimeout ("autoscaling ()", 100 );
}
}
Else {
Var img = $ (" ");
ImgDiv. replaceWith (img );
ImgDiv = img;
ImgDiv. attr ('src', this. files. item (0). getAsDataURL ());
ImgDiv.css ({"vertical-align": "middle "});
SetTimeout ("autoScaling ()", 100 );
}
}
});
}
});
}) (JQuery );
Page:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Meta content = "" name = "Keywords"/>
<Meta content = "" name = "Description"/>
<Meta content = "text/html; charset = UTF-8" http-equiv = "Content-Type"/>
<Script type = "text/javascript" src = "js/jquery. pack. js"> </script>
<SCRIPT type = "text/JavaScript" src = "JS/uploadpreview/jquery. uploadpreview. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
// We recommend that you add an overflow: hidden; CSS style to the parent element of # imgdiv.
$ ("Input "). uploadpreview ({width: 200, height: 200, imgdiv: "# imgdiv", imgtype: ["BMP", "GIF", "PNG", "jpg"]});
});
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Input type = "file"/>
<Br/>
<Div style = "width: 200px; height: 200px; overflow: hidden; border: 1px solid red;">
<Div id = "imgDiv">
</Div>
</Div>
</Form>
</Body>
</Html>