Hello, this issue is frustrating the hell out of me
I can't seem to resize my file before uploading it.
Relevant code
Problem is I am getting (I think) the correct dataurl file (I did a console.log and had this longish string), but I can't convert it to ajax readable format.
If I just had (without the resize function)
Everything works well, just that the image is not resized!
PHP Code:
So basically I need to resize the image before uploading. Solution preferably not to much changes. Is there just some "var newFile = resize(oldFile, 800,600) function I could use without changing the format of the file?
Thank you
I can't seem to resize my file before uploading it.
Relevant code
Code:
function submitFile(){
var file = resize(document.getElementById('askForm').files[0]);
console.log(file);
var formData = new FormData();
formData.append('file',file);
var xhr = new XMLHttpRequest();
xhr.open('POST',"../server/upload.php",true);
xhr.onload = function(e){
alert(this.response);
addImage(this.response); // this leads to another function that is working already
};
xhr.send(formData);
}
function resize(MYFILE){
var file = MYFILE;
var c = document.createElement('canvas');
var ctx = c.getContext('2d'),
reader = new FileReader;
reader.onload = function(event) {
var img = new Image;
img.src = event.target.result;
var MAX_WIDTH = 800;
var MAX_HEIGHT = 600;
var width = img.width;
var height = img.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
c.width = width;
c.height = height;
ctx.drawImage(img, 0, 0, width, height);
};
reader.readAsDataURL(file);
var dataurl = c.toDataURI("image/png");
return dataurl;
}
Problem is I am getting (I think) the correct dataurl file (I did a console.log and had this longish string), but I can't convert it to ajax readable format.
If I just had (without the resize function)
Code:
function submitFile(){
var file = document.getElementById('askForm').files[0];
console.log(file);
var formData = new FormData();
formData.append('file',file);
var xhr = new XMLHttpRequest();
xhr.open('POST',"../server/upload.php",true);
xhr.onload = function(e){
alert(this.response);
addImage(this.response);
};
xhr.send(formData);
}
Everything works well, just that the image is not resized!
PHP Code:
PHP:
<?php
$name = time();
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("../images/questions/" . $name))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"../images/questions/" . $name);
echo $name;
}
}
?>
So basically I need to resize the image before uploading. Solution preferably not to much changes. Is there just some "var newFile = resize(oldFile, 800,600) function I could use without changing the format of the file?
Thank you