由于jcrop现在正在使用触摸屏,我想做一个使用它的网络应用程序。我有一切工作,但如果我尝试使设计响应,以便用户可以在裁剪之前看到整个图像(它的宽度是屏幕的百分比),那么裁剪区域将不会与选择的相同用户。在已调整大小的图像上进行的选择坐标与全尺寸图像上的坐标不匹配。
Jcrop通过使用框大小调整或truesize方法包含类似问题的解决方案(当处理大图像时),但如果图像的宽度基于百分比而不是给定的像素宽度,则它们都不起作用。 / p>
我能想到的唯一解决方案是使用媒体查询调整图像大小,根据屏幕宽度制作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);
};
答案 0 :(得分:13)
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);
};
});
答案 1 :(得分:0)
您可以使用truesize参数。
答案 2 :(得分:0)
如果您使用ajax加载图片,则可以使用getimagesize('img.png')
获取图片宽度/高度,然后您可以在javascript代码中使用此变量。
trueSize对我来说真的很有用。
答案 3 :(得分:0)
使用以下代码
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]
});
&#13;
答案 4 :(得分:0)
我希望我的答案能帮助人们理解这个想法。假设我们在bootstrap中有一个响应式图像,其中 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代码,它将根据计算得出 rx , ry , rw 和 rh x , y , w 和 h
$(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方面采取 rx , ry , rw 和 rh 而不是 x < / strong>, y , w 和 h 。
(或)
您可以简单地覆盖
rx , ry , rw 和 rh
上
x , y , w 和 h 就像这样使用 x , y , w 和 h 照常。
$('#x').val(responsiveX);
$('#y').val(responsiveY);
$('#w').val(responsiveW);
$('#h').val(responsiveH);