获取原始图像进行裁剪,忽略宽度属性?
我的用户可以上传非常大的图像,出于裁剪和显示的目的,我添加了width
属性,以便它能够很好地适合浏览器窗口。实际图像尺寸可以是 - 比如 1920 x 1080 像素。
<!-- width added for display purpose -->
<img class="croppable" src="images/fhd.jpg" width="640" />
为了计算真实选择框尺寸(如果x
坐标是20px,那么在原始全高清图片中将是60px)我需要获取完整图像尺寸在应用width
属性之前。
问题是,考虑到宽度属性,这将返回 640 作为值:
// Important: Use load event to avoid problems with webkit browser like safari
// when using cached images
$(window).load(function(){
$('img.croppable').each(function(){
alert(this.width);
});
});
请不要将其标记为重复,因为我所要求的与简单图像宽度完全不同/高度检索(实际上有效)。
编辑:Chris G.解决方案似乎不起作用:
$(window).load(function(){
$('img.croppable').each(function(){
console.log(this.src);
var original = new Image(this.src);
console.log(original);
$("#original_w").text(original.width); // Temp, more images to be added
$("#original_h").text(original.height); // Temp, more images to be added
});
});
控制台输出:
http://localhost/DigitLifeAdminExtension/images/pillars-of-creation.jpg
<img width="0">
My user can upload really big images, and for cropping and display purposes i'm adding width
attribute so it will fit well in the browser window. Real image size can be - say 1920 x 1080 px.
<!-- width added for display purpose -->
<img class="croppable" src="images/fhd.jpg" width="640" />
In order to calculate real selection box dimension (if the x
coordinate is 20px then would be 60px in the original full hd picture) i need to get the full image size before apply the width
attribute.
The problem is that this will return 640 as value, taking into account the width attribute:
// Important: Use load event to avoid problems with webkit browser like safari
// when using cached images
$(window).load(function(){
$('img.croppable').each(function(){
alert(this.width);
});
});
Please don't flag this as duplicate since what i'm asking is completly different from simple image width/height retrival (which works, actually).
EDIT: Chris G. solution seems not working:
$(window).load(function(){
$('img.croppable').each(function(){
console.log(this.src);
var original = new Image(this.src);
console.log(original);
$("#original_w").text(original.width); // Temp, more images to be added
$("#original_h").text(original.height); // Temp, more images to be added
});
});
Console output:
http://localhost/DigitLifeAdminExtension/images/pillars-of-creation.jpg
<img width="0">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
获取图像本身的宽度/高度,而不是它所包含的 div。
Get the width/height of the image itself, not the div it is contained within.
您可以删除属性,获取宽度并再次将属性放在适当的位置:
但我认为 Chris G. 的答案也很好,只需确保当您尝试获取宽度时它将被加载:
You can remove the attributes, get the width and put the attributes in place again:
But I think Chris G.'s answer works well too, just making sure it will be loaded when you try to get the width:
适用于大多数最新浏览器和 IE9。
Works in most up-to-date browsers and IE9.
工作解决方案是:
The working solution would be: