Merge Image using Javascript

后端 未结 6 1482
青春惊慌失措
青春惊慌失措 2020-12-01 01:39

Is it possible to merge pictures using javascript?

For example, if you have 2 rectangle .jpg or .png images files of the same size, is it possible that you can align

6条回答
  •  醉梦人生
    2020-12-01 01:54

    I know this is an old post, but I ran across it searching for a solution to this question, myself. Even moreso, I wanted to be able to upload the images that should be used (without needing to rely on server-side logic).

    I've created a fiddle (http://jsfiddle.net/davidwalton/4pjreyfb/6/ ) that builds on this:

    How to make a simple image upload using Javascript/HTML

    I then added Huỳnh Quốc Phong's logic above (Merge Image using Javascript):

    HTML:

    
    
    


    JS:

    $('.file1, .file2').on('change', function() {
      var reader = new FileReader(),
        imageSelector = $(this).data('image-selector');
    
      if (this.files && this.files[0]) {
        reader.onload = function(e) {
          imageIsLoaded(e, imageSelector)
        };
        reader.readAsDataURL(this.files[0]);
      }
    });
    
    $('.btn-merge').on('click', merge);
    
    function imageIsLoaded(e, imageSelector) {
      $(imageSelector).attr('src', e.target.result);
      $(imageSelector).removeClass('hidden');
    };
    
    function merge() {
      var canvas = document.getElementById('canvas'),
        ctx = canvas.getContext('2d'),
        imageObj1 = new Image(),
        imageObj2 = new Image();
    
      imageObj1.src = $('.image1').attr('src');
      imageObj1.onload = function() {
        ctx.globalAlpha = 1;
        ctx.drawImage(imageObj1, 0, 0, 328, 526);
        imageObj2.src = $('.image2').attr('src');;
        imageObj2.onload = function() {
          ctx.globalAlpha = 0.5;
          ctx.drawImage(imageObj2, 15, 85, 300, 300);
          var img = canvas.toDataURL('image/jpeg');
          $('.merged-image').attr('src', img);
          $('.merged-image').removeClass('hidden');
        }
      };
    }
    

    Additionally, it incorporates a bit of transparency just to allow for two jpegs to be utilized.

    Note that all image positioning & sizing is managed via the ctx.drawImage() functions. The demo will be ugly, but it should prove the concept. :)

    Hopefully this is helpful!

提交回复
热议问题