1

I developed a small JCrop file upload app; here is my code:

 function createCropImage(event)
   {     
   //alert(event.target.result); 

   document.getElementById("Imgpreview").src = event.target.result;
   var img2 = document.getElementById("Imgpreview1").src = event.target.result;

   // Create variables (in this scope) to hold the API and image size
   var jcrop_api, boundx, boundy;

   $('#Imgpreview1').Jcrop({
        onChange: updatePreview,
        onSelect: updatePreview,
        aspectRatio: 1
      },function(){
        // Use the API to get the real image size

   var bounds = this.getBounds();
       boundx = bounds[0];
       boundy = bounds[1];
       // Store the API in the jcrop_api variable
       jcrop_api = this;


    });

    function updatePreview(c)
      {
      $('#Xcoardinate').val( Math.round(c.x));
      $('#Ycoardinate').val( Math.round(c.y));

      $('#width').val( Math.round(c.w));
      $('#height').val( Math.round(c.h));
        if (parseInt(c.w) > 0)
          {
          var rx = 100 / c.w;
          var ry = 100 / c.h;

          $('#Imgpreview').css({
            width: Math.round(rx * boundx) + 'px',
            height: Math.round(ry * boundy) + 'px',
            marginLeft: '-' + Math.round(rx * c.x) + 'px',
            marginTop: '-' + Math.round(ry * c.y) + 'px'
          });
        }
      };

}

Here Imgpreview is the preview image and Imgpreview1 is the source image. I first select an image through the browse button:

<input type="file" size="45" id="photoUploadElmt" name="upload" onchange="previewImg()" style="width:430px;"/>

The original image (Imgpreview1) and preview image (Imgpreview) are showing fine, but if I select another image, the preview image is correct but in place of Imgpreview1 I see the older image.

If I put following code in comments, then images are displayed properly but I lose the JCrop instance:

 $('#Imgpreview1').Jcrop({
            onChange: updatePreview,
            onSelect: updatePreview,
            aspectRatio: 1
          },function(){
            // Use the API to get the real image size

            var bounds = this.getBounds();
            boundx = bounds[0];
            boundy = bounds[1];
            // Store the API in the jcrop_api variable
            jcrop_api = this;


          });
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
AnilHoney
  • 259
  • 8
  • 20

1 Answers1

0

The destroy method is unreliable, so create a custom one as in this similar question

Community
  • 1
  • 1
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265