使用 Jcrop 调整图像大小
我使用 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,我就无法调整图像大小。如何同时使用这两个功能?
I use the Jquery Jcrop for cropping my images. Now I'm implementing a slider for resizing the image. I want the cropping and resizing to happend on the same page.
I do it like this:
$(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);
}
});
});
And my simple resizeImage function:
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);
}
The problem is that, as soon I turn on Jcrop I can't resize the image. How can I use both these functions at the same time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我最终在调整大小时破坏了 jCrop,并在调整大小后将其放回原处。不管怎样,谢谢。代码:
I ended up destroying the jCrop while resizing and putting it back on after resize. Thanks anyway. Code:
我有同样的任务要完成:使用应用了 jCrop 的滑块调整图像大小。您还必须调整 jCrop 创建的其他一些元素的大小,而不仅仅是图像。我最终修补了 jCrop 插件,这是最新 jCrop-0.9.10 的补丁。
修补你的 jCrop。如果您不知道如何应用补丁,只需将 resizeImage 函数放在 jCrop 的第 1578 行(当然是统一版本):
获取 jCrop API:
计算新的高度和宽度。如果您使用滑块执行此操作,请让滑块返回图像的新宽度,然后根据图像的纵横比计算新的高度:
还有其他一些需要注意的点。每次调整大小后,您应该看到裁剪区域仍然在图像的视口中。如果您需要,我可以发布完整的源代码,我是如何为我做到这一点的:jCrop + jquery ui slider to resize the image。
问候
I had the same task to accomplish: resize the image with a slider where jCrop is applied. There are some more elements you have to resize also that jCrop created, not only the image. I ended up patching the jCrop plugin and here is the patch for latest jCrop-0.9.10.
Patch your jCrop. If you don't know how to apply the patch, just put the resizeImage function to line 1578 of jCrop (unimified version ofcourse):
Get the jCrop API:
Calc new height and width. If your are doing it with a slider, let the slider say return the new width of the image and you calculate new height with aspect ratio of the image:
There are some other points to keep an eye on. After each resize your should look that crop area is still in the viewport of the image. If you need i could post the complete source how i've done it for me: jCrop + jquery ui slider to resize the image.
Regards
您还可以做的是利用 Jcrop 的 setImage 函数,当滑块发生变化时,使用 Jcrop api 调用 setImage 并设置新的宽度和高度值,如下所示:
我不确定这种技术是否是最佳解决方案,因为每次调用 setImage 函数时, jcrop 都会创建一个新的 Image 对象。
What you can also do is make use of the setImage function of Jcrop, when the slider changes, call the setImage with the Jcrop api and set new width and height values like this:
What I am not sure about this technique is if it is the best solution because everytime you call the setImage function, jcrop creates a new Image object.
如果您想要的是调整大小应该成比例,我认为您不需要滑块(因为它似乎与jCrop不兼容)。您可以使用jCrop并在onChange事件中确保比例(即实现resizeImage函数,修改)。
我就是这么认为的。
If what you want is that the resizing should be proportional, I don't think you need the slider (since it seems to be incompatible with jCrop). You could use jCrop and in the onChange event, ensure the proportionality (that is, implement the resizeImage function, modified).
That's what I think.
作为 Hermann Bier 答案的扩展,我添加了 jquery 动画。
当它是动画时,调整大小看起来更好:)
在 Jcrop 版本中实现:jquery.Jcrop.js v0.9.12
找到代码:
在 jquery.Jcrop.js 中第 1573 行附近
并将其替换为:
调用该函数将为调整大小设置动画。
请随意删除 /* */ 之间的代码 - 我只是将其保留作为参考
Happy Coding :)
As an extension to Hermann Bier answer, i have added the jquery animation.
The resizing looks way better when it's animated :)
Implemented in Jcrop version: jquery.Jcrop.js v0.9.12
Locate the code:
in jquery.Jcrop.js around line 1573
and replace it with:
Call to the function will animate the resize.
Feel free to delete the code between /* */ - I just kept it as an reference
Happy Coding :)