Search code examples
javascriptjqueryjcrop

JCrop big images issue


Is it possible to set some maxWidth and maxHeight in order to crop big images? I found out maxSize in documentation, but it is only for selection area. Also I tried trueSize

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

        // Grab some information about the preview pane
        $preview = $('#preview-pane'),
        $pcnt = $('#preview-pane .preview-container'),
        $pimg = $('#preview-pane .preview-container img'),

        xsize = $pcnt.width(),
        ysize = $pcnt.height();

    //console.log('init',[xsize,ysize]);
    $('#imageEdit').Jcrop({
      onChange: updatePreview,
      onSelect: updatePreview,
      aspectRatio: 220 / 55,


    },  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;

      jcrop_api.animateTo([ 120,120,20,20 ]);
      jcrop_api.trueSize([200, 100])
      // Move the preview into the jcrop container for css positioning
      $preview.appendTo(jcrop_api.ui.holder);

    });

Now my Images are like this one. They are out of bounds of modal: enter image description here


Solution

  • I find the answer. You should use boxHeight and boxWidth options. Like this:

    $('#imageEdit').Jcrop({
          onChange: updatePreview,
          onSelect: updatePreview,
          aspectRatio: 220 / 55,
          boxWidth: 350
    
        },  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;
    
          jcrop_api.animateTo([ 120,120,20,20 ]);
        //  jcrop_api.trueSize([200, 100])
          // Move the preview into the jcrop container for css positioning
          $preview.appendTo(jcrop_api.ui.holder);
    
        });