I have a cropping tool in my website that users can upload pictures and crop it.
I am encountering whenever I upload a picture that was captured by mobile phone the images orientation is wrong.
HTML
<div class="modal-body">
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "formAjax", enctype = "multipart/form-data", action = "/ProfilePicture/Crop" }))
{
@Html.AntiForgeryToken()
<label> Upload Photo: </label>
<label class="btn btn-default btn-file">
Browse @Html.TextBoxFor(ModelIndex => ModelIndex.ProfilePic.MyFile, new { id = "file", type = "file", style = "display: none;" })
</label>
<br /><br />
<div id="crop-image-area" style="display:none;">
<table border="0" cellpadding="0" cellspacing="5">
<tr>
<td>
<label>Preview: </label>
<div class="img-circle" style="width:100px;height:100px;overflow:hidden;margin-left:5px;">
<canvas id="preview"></canvas>
</div>
<br />
</td>
</tr>
<tr>
<td>
<label>Crop Here: </label>
<img src="" id="cropbox" style="display: none;">
</td>
</tr>
</table>
<br />
<input type="hidden" id="cropPointX" name="cropPointX" />
<input type="hidden" id="cropPointY" name="cropPointY" />
<input type="hidden" id="imageCropWidth" name="imageCropWidth" />
<input type="hidden" id="imageCropHeight" name="imageCropHeight" />
<span>
<input type="submit" id="btnCrop" class="btn btn-success" value="Crop" />
<label>
<img id="loader" src="~/Content/images/loading.gif" style="display:none;" />
</label>
</span>
</div>
}
</div>
JS
$(function() {
var jcrop_api;
$("#file").change(function() {
var reader = new FileReader();
reader.onload = function(e) {
$('#cropbox').show();
$('#cropbox').attr('src', e.target.result);
$('#cropbox').Jcrop({
onChange: updatePreview,
onSelect: getcroparea,
boxWidth: 400,
boxHeight: 400,
aspectRatio: 1,
bgOpacity: .4,
setSelect: [80, 45, 100, 100]
}, function() {
//first attempt
if (jcrop_api != null) {
jcrop_api.destroy();
}
//second attempt - even this does not work
$(".jcrop-holder").not(":last").remove();
jcrop_api = this;
});
$("#crop-image-area").hide();
$("#crop-image-area").fadeIn("slow");
}
reader.readAsDataURL($(this)[0].files[0]);
if ($('#cropbox').data('Jcrop')) {
$('#cropbox').data('Jcrop').destroy();
$('#cropbox').removeAttr('style');
}
});
function updatePreview(c) {
if (parseInt(c.w) > 0) {
var imageObj = jQuery("#cropbox")[0];
var canvas = jQuery("#preview")[0];
var context = canvas.getContext("2d");
context.beginPath();
context.arc(50, 50, 50, Math.PI * 4, 0, true);
context.clip();
context.closePath();
context.drawImage(imageObj, c.x, c.y, c.w, c.h, 0, 0, 100, 100);
}
};
function getcroparea(c) {
imageCropWidth = c.w;
imageCropHeight = c.h;
cropPointX = c.x;
cropPointY = c.y;
$('#cropPointX').val(Math.round(cropPointX))
$('#cropPointY').val(Math.round(cropPointY))
$('#imageCropWidth').val(Math.round(imageCropWidth))
$('#imageCropHeight').val(Math.round(imageCropHeight))
}
function destroyCrop() {
var jcropApi = $('#cropbox').data('Jcrop');
if (jcropApi !== undefined) {
jcropApi.destroy();
$('#cropbox').attr('style', "").attr("src", "");
}
}
function destroyPreview() {
$('#preview').removeAttr('src');
}
function cropImage() {
$("#loader").show();
$('#formAjax').ajaxSubmit({
type: 'POST',
success: function(result) {
$("#loader").hide();
$('#myProfilePicModal').modal('hide');
$("#crop-image-area").fadeOut("slow");
destroyCrop();
destroyPreview();
$("#alert-success").show();
$('#newImage').attr('src', 'data:image;base64,' + result.Photo);
$('.img-avatar').attr('src', 'data:image;base64,' + result.Avatar);
setTimeout(function() {
$("#alert-success").hide();
}, 5000);
},
error: function() {
$("#loader").hide();
$('#myProfilePicModal').modal('hide');
$("#crop-image-area").fadeOut("slow");
destroyCrop();
destroyPreview();
$("#alert-fail").show();
setTimeout(function() {
$("#alert-fail").hide();
}, 5000);
}
});
}
$("#btnCrop").on("click", function(e) {
e.preventDefault();
cropImage();
});
});
I am not familiar with EXIF orientation please help me in implementing it on my code.