[英]Using jcrop on responsive images
由於jcrop現在正在使用觸摸屏,我想做一個使用它的網絡應用程序。 我有一切工作,但如果我嘗試使設計響應,以便用戶可以在裁剪之前看到整個圖像(它的寬度是屏幕的百分比),那么裁剪區域將不會與選擇的相同用戶。 在調整大小后的圖像上進行的選擇坐標與全尺寸圖像上的坐標不匹配。
Jcrop通過使用box大小調整或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);
};
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);
};
});
它使用以下代碼
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] });
您可以使用truesize參數。
如果您使用ajax加載圖像,則可以使用getimagesize('img.png')
獲取圖像寬度/高度,然后您可以在javascript代碼中使用此變量。
trueSize真的對我有用。
我希望即使我的回答也能幫助人們理解這個想法。 讓我們說我們在引導程序中有一個響應式圖像,具有類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代碼,它將根據x , y , w和h的計算得到rx , ry , rw和rh
$(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 , y , w和h 。
(要么)
你可以簡單地覆蓋
RX,RY,RW和相對濕度上的x,y,w和h這樣和使用的x,y,w和h如常。
$('#x').val(responsiveX);
$('#y').val(responsiveY);
$('#w').val(responsiveW);
$('#h').val(responsiveH);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.