I am using the webpage below to draw an photo selected with an iPhone to the canvas so it can later be uploaded to a webpage over ajax. The code would also have downsampling but I have omitted that to make it simpler.
This code works fine on Mac OSx in Firefox and Safari but when used with the iPhone it goes wrong when you select a photo from your iPhone library or take a photo. The photo in the canvas has the wrong aspect ratio and is upside down. BUT if you import a photo from the iPhone to your Mac and then put it back on the phone with iTunes that photo when selected comes out perfectly! Why would this be, I think it might have to do with something with the image being rotated once it has been imported to the Mac.
Basically I am trying to write a script so a user can take a photo with the iPhone, the photo gets downsampled using canvas and javascript and then gets uploaded via ajax. But the problem is the photo in the canvas doesn't come out right when taking a fresh photo.
First image = correct Second image = incorrect
Canvas Gone Wrong
<style type="text/css">
#theImage {
display:block;
}
#cv {
background-color:#F00;
}
</style>
</head>
<body>
<form id="pageform" method="post" enctype="multipart/form-data" action="">
<input type="file" name="fileinput" id="fileinput" accept="image/*"/>
</form>
<canvas id='cv' width="918" height="689"></canvas>
<img id="theImage" />
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#theImage').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
function another() {
var cv = document.getElementById('cv');
var context = cv.getContext('2d');
context.ImageSmoothingEnabled = true;
context.webkitImageSmoothingEnabled = true;
context.mozImageSmoothingEnabled = true;
var img = document.getElementById('theImage');
context.drawImage(img,0, 0, 918,689 );
}
$(document).ready(function () {
$('#fileinput').change(function() {
readURL(this);
setTimeout(another,2000);
});
});
</script>
</body>
</html>