Is it possible to set a maximum width and height for an image before uploading it? I want the user to select a file, see a preview of the image, crop it, and then upload it to be saved in a directory. How can I enforce a max width and height limit before allowing the user to preview the selected image?
If the image exceeds the specified max height and width, display a message asking for a smaller picture.
HTML CODE:
<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>
JQUERY CODE:
function setInfo(i, e) {
$('#x').val(e.x1);
$('#y').val(e.y1);
$('#w').val(e.width);
$('#h').val(e.height);
}
$(document).ready(function() {
var p = $("#uploadPreview");
// Prepare instant preview
$("#uploadImage").change(function(){
// Fade out or hide preview
p.fadeOut();
// Prepare HTML5 FileReader
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
oFReader.onload = function (oFREvent) {
p.attr('src', oFREvent.target.result).fadeIn();
};
});
// Implement imgAreaSelect plugin (http://odyniec.net/projects/imgareaselect/)
$('img#uploadPreview').imgAreaSelect({
// Set crop ratio (optional)
aspectRatio: '1:1',
onSelectEnd: setInfo
});
});