【问题标题】:Using jcrop on responsive images在响应式图像上使用 jcrop
【发布时间】:2012-11-18 20:20:34
【问题描述】:

由于 jcrop 现在在触摸屏上工作,我想做一个使用它的网络应用程序。我已经完成了所有工作,但是如果我尝试使设计具有响应性,以便用户可以在裁剪之前看到整个图像(它的宽度是屏幕的百分比),那么裁剪区域将与所选区域不同用户。在调整大小的图像顶部所做的选择的坐标与全尺寸图像上的坐标不匹配。

Jcrop 包含一个类似问题的解决方案(在处理大图像时),方法是使用 box sizing 或 truesize 方法,但如果图像的宽度基于百分比而不是给定的像素宽度,它们都不起作用。

我能想到的唯一解决方案是使用媒体查询调整图像大小,并根据屏幕宽度制作 3 或 4 个版本,但我宁愿坚持基于百分比的调整大小,因为它看起来更好。

这是我的 jcrop 调用:

      var jcrop_api, boundx, boundy;
    $('#target').Jcrop({
        onChange: updatePreview,
        onSelect: updatePreview,
        aspectRatio: 0.75
    },function(){
        // Use the API to get the real image size
        var bounds = this.getBounds();
        boundx = bounds[0];
        boundy = bounds[1];
        trueSize: [900,600],
        // Store the API in the jcrop_api variable
        jcrop_api = this;
    });
            function updatePreview(c){
        if (parseInt(c.w) > 0){
            var rx = <?echo $width;?> / c.w;
            var ry = <?echo $height;?> / c.h;

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

        $('#x').val(c.x);
        $('#y').val(c.y);
        $('#w').val(c.w);
        $('#h').val(c.h);

    };

【问题讨论】:

  • 我无法弄清楚如何让图像响应。一旦我打电话给Jcrop,它就会停止响应我。

标签: jquery html css responsive-design jcrop


【解决方案1】:

TrueSize 最终成功了,我没有正确使用它:

jQuery(function($){

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

    $('#target').Jcrop({
        onChange: updatePreview,
        onSelect: updatePreview,
        aspectRatio: 0.75,
        trueSize: [<?echo $width2;?>,<?echo $height2;?>]
    },function(){
        // Use the API to get the real image size
        var bounds = this.getBounds();
        boundx = bounds[0];
        boundy = bounds[0.75];
        //trueSize: [ancho,alto],
        // Store the API in the jcrop_api variable
        jcrop_api = this;
    });

    function updatePreview(c){
        if (parseInt(c.w) > 0){
            var rx = <?echo $width;?> / c.w;
            var ry = <?echo $height;?> / c.h;

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

        $('#x').val(c.x);
        $('#y').val(c.y);
        $('#w').val(c.w);
        $('#h').val(c.h);

    };


});

【讨论】:

  • $width2 和 $height2 是从哪里来的?
  • 我设法通过将 truesize 设置为以下值来使其工作。 trueSize: [oImage.naturalWidth,oImage.naturalHeight],
  • $width2 和 $height2 是从哪里来的?
  • @pratik 是 PHP 代码。 Elaine 可能用 PHP 测量了图像的宽度和高度,并在 JavaScript 代码中打印出来。
  • 我收到错误:“ReferenceError: boundx is not defined”。
【解决方案2】:

它使用以下代码

var width2  = jQuery('#cropbox').prop('naturalWidth');
var height2 = jQuery('#cropbox').prop('naturalHeight');

jQuery('#cropbox').Jcrop({
  aspectRatio: 1,
  onSelect: updateCoords,
  onChange: updateCoords,
  setSelec: [0,0,110,110],
  trueSize: [width2,height2]
});

【讨论】:

    【解决方案3】:

    我希望即使是我的回答也能帮助人们理解这个想法。假设我们在引导程序中有一个响应式图像,其类 img-responsive

    这是带有图片的html表单

    <form method="POST" action="#" enctype="multipart/form-data">
          <img  class="img-responsive" id="get-profile-img" src="image.jpg"/>
          <input id="x" name="x" type="hidden" value="">        
          <input id="y" name="y" type="hidden" value="">        
          <input id="w" name="w" type="hidden" value="">        
          <input id="h" name="h" type="hidden" value="">        
    
          <input id="rx" name="rx" type="hidden" value="">        
          <input id="ry" name="ry" type="hidden" value="">        
          <input id="rw" name="rw" type="hidden" value="">        
          <input id="rh" name="rh" type="hidden" value="">        
    </form>
    

    这里是 JCrop 代码,它将根据计算得到 rxryrwrh xywh

    $(function() {
        $('#get-profile-img').Jcrop({
                onSelect: updateCoords,
                aspectRatio: 1,
                setSelect  : [50, 0, 300,300],
                allowResize: true
            });
        });
        function updateCoords(c) {
            $('#x').val(c.x);
            $('#y').val(c.y);
            $('#w').val(c.w);
            $('#h').val(c.h);
           responsiveCoords(c, '#get-profile-img');
        };
    
        function responsiveCoords(c, imgSelector) {
    
            var imgOrignalWidth     = $(imgSelector).prop('naturalWidth');
            var imgOriginalHeight   = $(imgSelector).prop('naturalHeight');
    
            var imgResponsiveWidth  = parseInt($(imgSelector).css('width'));
            var imgResponsiveHeight = parseInt($(imgSelector).css('height'));                
    
            var responsiveX         = Math.ceil((c.x/imgResponsiveWidth) * imgOrignalWidth);
            var responsiveY         = Math.ceil((c.y/imgResponsiveHeight) * imgOriginalHeight);
    
            var responsiveW         = Math.ceil((c.w/imgResponsiveWidth) * imgOrignalWidth);
            var responsiveH         = Math.ceil((c.h/imgResponsiveHeight) * imgOriginalHeight);
    
            $('#rx').val(responsiveX);
            $('#ry').val(responsiveY);
            $('#rw').val(responsiveW);
            $('#rh').val(responsiveH);
    
    };
    

    最后在 PHP 端使用 rxryrwrh 代替 x、ywh

    (或)

    您可以简单地覆盖
    rxryrwrhxywh 像这样使用 xywh 像往常一样。

    $('#x').val(responsiveX);
    $('#y').val(responsiveY);
    $('#w').val(responsiveW);
    $('#h').val(responsiveH);
    

    【讨论】:

      【解决方案4】:

      您可以为此使用 truesize 参数。

      【讨论】:

      • 我真的不明白我应该怎么做?响应式图像宽度是我不知道的,因为它在 css 上是 width:100%。
      • 您可以随时使用 javascript 询问图像的宽度/高度。
      • Jcrop 自己在css中寻找宽度是不行的。
      【解决方案5】:

      如果您使用 ajax 加载图像,您可以使用 getimagesize('img.png') 获取图像宽度/高度,然后您可以在 javascript 代码中使用此变量。

      trueSize 真的对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 2016-03-18
        • 2014-05-22
        • 2017-05-27
        • 2014-07-06
        相关资源
        最近更新 更多