HTML5 Large Image Preview Fast

Hi, I am making a mobile web application that requires the user to upload multiple images to the server. As soon as they select their images, I want to show a preview of their images before they want to upload to the server. I tried both FileReader and createObjectURL (using the canvas), and both methods take the age for the images to appear. This is usually not a problem, but most of the images on your mobile phone are of high quality and at least 2-3 MB.

The time taken to create the thumbnails is almost the same as loading the image. Is there a way to show instant viewing of low-resolution images before downloading?

Below are two ways that I used to preview images:

createObjectURL and Canvas

<input type="file" id="browseImages" multiple="multiple" accept="image/*">
<output id="list"></output>

document.getElementById('browseImages').addEventListener('change', handleFileSelect, true);

  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {
      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

    var canvas = document.createElement('canvas');
    canvas.width  = 110;
    canvas.height = 100;
    var ctx = canvas.getContext("2d");
    var img = new Image;
    img.onload = function() {
    ctx.drawImage(img, 0, 0, 100, 100);
}
    img.src = window.URL.createObjectURL(f);
    document.getElementById('list').appendChild(canvas);
    window.URL.revokeObjectURL(f);
    }
  }

The above method reads the images that the user has selected, and creates a canvas with the image for each selected image.

Path filereader

<input type="file" id="browseImages" multiple="multiple" accept="image/*">
<output id="list"></output>

document.getElementById('browseImages').addEventListener('change', handleFileSelect, true);

  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          var span = document.createElement('span');
          span.innerHTML = ['<img style="height:75px;" src="', e.target.result,
                            '" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

The above method uses FileReader to read and then process preview images.

Both of these methods require time passing through each image, reading, and then processing the thumbnail. Speed ​​it up as if I'm uploading images. Is there a way to generate instant thumbnails of selected images? It doesn’t matter if it’s for a mobile browser, so it won’t have much memory, such as a PC.

+4

Source: https://habr.com/ru/post/1612608/


All Articles