【问题标题】:Resize image which uses Jcrop使用 Jcrop 调整图像大小
【发布时间】:2011-09-07 22:36:36
【问题描述】:

我使用 Jquery Jcrop 来裁剪我的图像。现在我正在实现一个用于调整图像大小的滑块。我希望在同一页面上进行裁剪和调整大小。

我是这样做的:

$(document).ready(function() {


    var img = $('#cropbox')[0]; // Get my img elem
    var orgwidth, orgheight;
    $("<img/>") // Make in memory copy of image to avoid css issues
        .attr("src", $(img).attr("src"))
        .load(function() {
            orgwidth = this.width;   // Note: $(this).width() will not
            orgheight = this.height; // work for in memory images.
        });

    $('#cropbox').Jcrop({
        onSelect: updateCoords
    });

    $("#imageslider").slider({
        value: 100,
        max: 100,
        min: 1,
        slide: function(event, ui) {
            //$('ul#grid li').css('font-size',ui.value+"px");
            resizeImage(orgwidth, orgheight);
        }
    });

});

还有我简单的 resizeImage 函数:

function resizeImage(orgwidth, orgheight) {
    var value = $('#imageslider').slider('option', 'value');
    var width = orgwidth * (value / 100);
    var height = orgheight * (value / 100);
    $('#cropbox').width(width);
    $('#cropbox').height(height);
    $('#tester').val("org: "+orgwidth+" now: "+width);
}

问题是,一旦我打开 Jcrop,我就无法调整图像大小。如何同时使用这两个功能?

【问题讨论】:

    标签: javascript jquery resize crop jcrop


    【解决方案1】:

    如果您想要调整大小应该成比例,我认为您不需要滑块(因为它似乎与 jCrop 不兼容)。您可以使用 jCrop 并在 onChange 事件中确保比例性(即实现 resizeImage 函数,已修改)。
    我就是这么想的。

    【讨论】:

      【解决方案2】:

      我最终在调整大小时破坏了 jCrop,并在调整大小后将其重新打开。不管怎么说,还是要谢谢你。代码:

              function resizeImage(orgwidth, orgheight) {
              jcrop_api.destroy();
      
              var value = $('#imageslider').slider('option', 'value');
              var width = orgwidth * (value / 100);
              var height = orgheight * (value / 100);
              $('#cropbox').width(width);
              $('#cropbox').height(height);
              $('#rw').val(width);
              $('#rh').val(height);
      
              initJcrop();
      
          }
      

      【讨论】:

        【解决方案3】:

        我有同样的任务要完成:使用应用了 jCrop 的滑块调整图像大小。您还需要调整 jCrop 创建的其他一些元素的大小,而不仅仅是图像。我最终修补了 jCrop 插件,这是最新的 jCrop-0.9.10 的补丁。

        修补您的 jCrop。如果你不知道怎么打补丁,把resizeImage函数放到jCrop的第1578行(当然是统一版):

        --- /home/dr0bz/Desktop/jquery.Jcrop.js
        +++ /home/dr0bz/workspace/profile_tuning/js/lib/jquery.Jcrop.js
        @@ -1573,6 +1573,15 @@
               ui: {
                 holder: $div,
                 selection: $sel
        +      },
        +      
        +      resizeImage: function(width, height) {
        +        boundx = width;
        +        boundy = height;
        +        $([$img2, $img, $div, $trk]).each(function(index, element)
        +        {
        +          element.width(width).height(height);
        +        });
               }
             };
        

        获取 jCrop API:

        var jCropApi;
        $('#photo').Jcrop({}, function()
        {
          jCropApi = this;
        });
        

        计算新的高度和宽度。如果您使用滑块执行此操作,请让滑块说返回图像的新宽度,然后使用图像的纵横比计算新高度:

        var aspectRatio = width / height;
        // newWidth returned by slider
        var newHeight = Math.round(width / aspectRatio);
        jCropApi.resizeImage(newWidth, newHeight);
        

        还有其他一些需要注意的地方。每次调整大小后,您应该看到裁剪区域仍在图像的视口中。如果您需要,我可以发布完整的源代码:jCrop + jquery ui slider to resize image.

        问候

        【讨论】:

        【解决方案4】:

        你还可以利用Jcrop的setImage函数,当滑块改变时,通过Jcrop api调用setImage,设置新的宽高值如下:

        var jcrop_api;
        
        $('#cropbox').Jcrop({
            onSelect: updateCoords
        }, function() {
            jcrop_api = this;
        });
        
        $("#imageslider").slider({
            value: 100,
            max: 100,
            min: 1,
            slide: function(event, ui) {
                var value = $('#imageslider').slider('option', 'value');
                var width = orgwidth * (value / 100);
                var height = orgheight * (value / 100);
                jcrop_api.setImage($(img).attr("src"), function() {
                    this.setOptions({
                        boxWidth: width,
                        boxHeight: height
                    });
                });
                $('#tester').val("org: "+orgwidth+" now: "+width);
            }
        });
        

        我不确定这种技术是否是最佳解决方案,因为每次调用 setImage 函数时,jcrop 都会创建一个新的 Image 对象。

        【讨论】:

          【解决方案5】:

          作为 Hermann Bier 答案的扩展,我添加了 jquery 动画。 动画时调整大小看起来更好:)

          在Jcrop版本中实现:jquery.Jcrop.js v0.9.12

          找到代码:

          ui: {
              holder: $div,
              selection: $sel
            }
          

          在 jquery.Jcrop.js 第 1573 行附近 并将其替换为:

          ui: {
              holder: $div,
              selection: $sel
            },
            resizeImage: function(width, height) {
              animationsTid = 500;
              boundx = width;
              boundy = height;
          
              $($img2).animate({
                  width: width, 
                  height: height,
              }, { duration: animationsTid, queue: false });
          
              $($img).animate({
                  width: width, 
                  height: height,
              }, { duration: animationsTid, queue: false });
          
              $($div).animate({
                  width: width, 
                  height: height,
              }, { duration: animationsTid, queue: false });
          
              $($trk).animate({
                  width: width, 
                  height: height,
              }, { duration: animationsTid, queue: false });
          
              /*
              //Old way of resizing, but without animation
              $([$img2, $img, $div, $trk]).each(function(index, element){
                element.width(width).height(height);
              });
              */
            }
          

          调用该函数将为调整大小设置动画。 随意删除 /* */ 之间的代码 - 我只是把它作为参考

          快乐编码:)

          【讨论】:

            猜你喜欢
            • 2013-04-08
            • 2012-05-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-02
            • 2021-06-25
            相关资源
            最近更新 更多