Home  >  Article  >  Web Front-end  >  jquery image upload proportional preview plug-in collection_jquery

jquery image upload proportional preview plug-in collection_jquery

WBOY
WBOYOriginal
2016-05-16 18:06:13831browse

The js part is like this:

Copy code The code is as follows:

//**** ******************Picture upload preview plug-in************************
/ /Author: IDDQD(2009-07-01)
//Email: iddqd5376@163.com
//http://blog.sina.com.cn/iddqd
//Version: 1.0

//Description: Image upload preview plug-in
//When uploading, it can generate proportional scaling images within a fixed width and height range

//Parameter settings:
//width The width of the fixed-size container for storing images
//height The height of the fixed-size container for storing images
//imgDiv The JQuery id of the page DIV
//imgType array suffix name
//**** ******************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 one of " opts.imgType.join(", ") " kind");
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 part:
Copy code The code is as follows:
























Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn