0

Jcrop を使用して、ユーザーが画像を次のフォームにロードする前にトリミングできるようにしています。DB から JS 配列に画像のスタックをロードして、画像をすばやくスクロールできるようにします。

新しい画像を JCrop にロードすると、アスペクト比が台無しになります。以下は私のjsです

<script type="text/javascript">

    jQuery(function($){

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

      $('#fbimage').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;
      });

        $('#nextPhoto').click(function(e) {
            if ($('.prevPhoto:hidden'))
            {
                $('#prevPhoto').show();
            }
            count= parseFloat($('#profilePicCOUNT').val());
            count = count+1;
            $('#profilePicCOUNT').val(count);
            //$('.fbimage').attr("src",images[count-1]);
            $('.previewpic').attr("src",images[count-1]);
            jcrop_api.setImage(images[count-1]);
            jcrop_api.setOptions({ bgOpacity: .6 });


            $('#profilePicURL').val(images[count-1]);
            if (count==images.length )
            {
                $('#nextPhoto').hide();
            }
              return false;
        });

        $('#prevPhoto').click(function(e) {
            if ($('#profilePicCOUNT').val()==2)
            {
                $('#prevPhoto').hide();
            }
            if (count<(images.length+1) )
            {
                $('#nextPhoto').show();
            }
            count= parseFloat($('#profilePicCOUNT').val());
            count = count-1;
            $('#profilePicCOUNT').val(count);
            //$('.fbimage').attr("src",images[count-1]);
            $('.previewpic').attr("src",images[count-1]);
            jcrop_api.setImage(images[count-1]);
            jcrop_api.setOptions({ bgOpacity: .6 });
            $('#profilePicURL').val(images[count-1]);
            count= parseFloat($('#profilePicCOUNT').val());
            count = count-1;
            return false;
        });

      function updatePreview(c)
      {
        if (parseInt(c.w) > 0)
        {
          var rx = 100 / c.w;
          var ry = 100 / c.h;

          $('#previewpic').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'
          });
        }
      };

    });

</script>
4

2 に答える 2

1

数時間検索して試した後、答えは簡単でした。このコード行は、プレビューの比率をリセットして適切に機能させます。

var newUrl = 'https://..../image.jpg'
var jcrop_api = $('#Image1').data("Jcrop");
jcrop_api.setImage(newUrl , function() {
    jcrop_api = this;
    var bounds = this.getBounds();
});
于 2012-09-11T07:42:24.927 に答える