3

I want to avoid uploading big/heavy image files.

I prefer HTML5 FileAPI library for this job.

All other functionality is added (upload, re-order, etc.), so I only need the image-resize function.

CASE:

On the page there is an input for multiple files.

On input change event (when adding files), resize the entered images/files and append them to FormData(), then send the FormData() to PHP script via ajax.

EXAMPLE:

$('input').on('change',function(){
   var formData = resizeImages(this.files[0]);   
   uploadResizedImages(formData);
});

function resizeImages(files){
   formData = new FormData();

   //For each image, resize it and append to formData
   //resize file part missing....
   formData.append('files[]',this);//Appending to formData (this = currently iterated file)

   return formData;//Return the formData with resized images
}

Anyone?

Thanks

1 Answer 1

9

In my experience you cannot manipulate the image on the client side then upload the manipulated image in tact via a file input in a form.

The way I have done what you are trying to do in the past involves a few steps.

  1. Select image using a file input
  2. Read the file as a dataURL
  3. Use canvas to manipulate the image as needed
  4. Export the new image as a dataUrl
  5. Use ajax to upload the image to the server as a dataURL
  6. Use server side functions to convert the dataUrl to an image and store

https://jsfiddle.net/0hmhumL1/

function resizeInCanvas(img){
  /////////  3-3 manipulate image
    var perferedWidth = 2700;
  var ratio = perferedWidth / img.width;
  var canvas = $("<canvas>")[0];
  canvas.width = img.width * ratio;
  canvas.height = img.height * ratio;
  var ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0,0,canvas.width, canvas.height);
  //////////4. export as dataUrl
  return canvas.toDataURL();
}

When uploading as a dataUrl you increase the size (bandwidth required) of the manipulated image by about 20% so you may not see the savings you are looking for unless you are changing the image size considerably.

Sign up to request clarification or add additional context in comments.

5 Comments

I want the image size max 1920x1080. When I shoot photos with my camera, the resolution is 6000x4000 and the size is from 8Mb to 20 Mb. By resizing it, it would be around ~2 Mb (hopefully). Also, I think I can add jpg quality reduction to get even lower sizes. Btw. will I receive these dataURL's as $_FILES in PHP or as $_POST? Thank you for your reply.
What about reverting the DataURL to Blob object, append to FormData and then send?
Based on your previous question. iff you change your preferredWidth to 1920 the solution would re size the images correctly. I am not a php programmer so I do not know the answer, but my guess would be $_POST since it is not longer actually a file and just data being transfered
I have tried this... however the file size ends up so much higher with canvas...original being 37kb and uploaded version afterwards being 387kb... what gives?
Thanks, it solved my needs! @Carine, for small files sometimes that happens, but if you use a Mb file, is really shrinks. I have created an Ember component for this, if anyone needs it, send me a msg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.