|
|
#1 |
|
Resizing an image before uploading
Hello, this issue is frustrating the hell out of me
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;
}
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);
}
PHP Code: PHP Code:
Thank you
|
|
|
|
0
|
|
|
#2 |
|
Maybe this:
Code:
c.width = width;
c.height = height;
ctx.drawImage(img, 0, 0, width, height);
Code:
c.width = width;
c.height = height;
ctx.drawImage(img, 0, 0, c.width, c.height);
As you also mentioned PHP resizing and doing it server side like that is my preferred method. Here is how I might handle it noting this supports various image formats and proportionate scaling: PHP Code:
__________________
Jim Goldbloom Sr. Web Developer, owner GoldTechPro, LLC http://www.GoldTechPro.com
|
|
|
|
0
|
|
|
#3 | |
|
Quote:
|
||
|
|
0
|
|
|
#4 |
|
|
0
|
|
|
#5 |
|
what i did
i hope i didnt miss it but i don't see you using windows.File or windows.FileReader to do this. i wrote code that takes an image file selected by the user, resamples it then uploads to a php script using windows.File and windows.FileReader and also the canvas element. it sends the new image in base64 using ajax then a php script writes it as a jpeg onto the server side storage.
you have to use these to do what you are doing i believe. they are html5 elements. at the time safari didn't support the File object but testing it now looks like safari 6 does finally. i used a tutorial online but it took a bit of trial and error myself since i am just a hobbyist and no professional programmer. hope this helps. ---------- 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. encoded_data = encodeURIComponent(STRING); sendrequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); and it looks like you are using those html5 elements. my bad. Last edited by weex23; Oct 8, 2012 at 11:17 AM. Reason: forgot a line of code |
|
|
|
0
|
|
|
#6 |
|
Hi guys, after not seeing any reply for after a day I thought no one was going to reply. Glad people helped.
I'm sort of at work now so I'll try out the suggestions when I get home. As for why I don't resize the images first, the web app is supposed to allow users to upload their own images to server, and for convenience, I don't want to trouble the users to resize before uploading I'll do it for them.
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 03:02 AM.







Jim Goldbloom
Linear Mode
