2
我想知道如何使imgareaselect选择照片文件时自动选择?所以用户知道他们可以裁剪到目前为止我在照片中使用什么:如何自动显示图像上的裁剪选择
我想实现的是当我选择一个图像文件我试图让imgareaselect选择一部分照片自动。
imgareaselect文档:http://odyniec.net/projects/imgareaselect/usage.html
jQuery代码:
// set info for cropping image using hidden fields
function setInfo(i, e) {
// Get on screen image
var screenImage = $("#uploadPreview");
// Create new offscreen image to test
var theImage = new Image();
theImage.src = screenImage.attr("src");
// Get accurate measurements from that.
var imageWidth = theImage.width;
//Get width of resized image
var scaledimagewidth = document.getElementById("uploadPreview").width;
if (imageWidth > scaledimagewidth){ var ar = imageWidth/scaledimagewidth;}
else { var ar = 1;}
$('#x').val(e.x1*ar);
$('#y').val(e.y1*ar);
$('#w').val(e.width*ar);
$('#h').val(e.height*ar);
}
$(document).ready(function() {
var p = $("#uploadPreview");
// prepare instant preview
$("#uploadImage").change(function() {
// fadeOut or hide preview
p.fadeOut();
// prepare HTML5 FileReader
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
oFReader.onload = function (oFREvent) {
var image = new Image();
image.src = oFREvent.target.result;
image.onload = function() {
if ((this.width >= 4500) || (this.height >= 4500)) {
alert("Picture Has to Be Lower Than 4500 by 4500, choose another file");
}
else {
p.attr('src', oFREvent.target.result).fadeIn();
}
// access image size here & do further implementation
};
};
});
// implement imgAreaSelect plug in (http://odyniec.net/projects/imgareaselect/)
$('img#uploadPreview').imgAreaSelect({
aspectRatio: '1.33:1',
onSelectEnd: setInfo
});
});
HTML标记:
<div class="wrap">
<!-- image preview area-->
<img id="uploadPreview" style="display:none;"/>
<!-- image uploading form -->
<form action="upload.php" method="post" enctype="multipart/form-data">
<input id="uploadImage" type="file" accept="image/jpeg" name="image" />
<input type="submit" value="Upload">
<!-- hidden inputs -->
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
</form>
</div>