获取原始图像进行裁剪,忽略宽度属性?

发布于 2024-12-21 09:48:28 字数 1310 浏览 1 评论 0原文

我的用户可以上传非常大的图像,出于裁剪显示的目的,我添加了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风透绣罗衣 2024-12-28 09:48:28

获取图像本身的宽度/高度,而不是它所包含的 div。

$(window).load(function(){
    $('img.croppable').each(function(){
        var img = new Image();
        img.src =  $(this).src;
        alert(img.width);
    });
});

Get the width/height of the image itself, not the div it is contained within.

$(window).load(function(){
    $('img.croppable').each(function(){
        var img = new Image();
        img.src =  $(this).src;
        alert(img.width);
    });
});
甜柠檬 2024-12-28 09:48:28

您可以删除属性,获取宽度并再次将属性放在适当的位置:

var $img = $(img);
var oldWidth = $img.attr("width");
var imgWidth = $img.removeAttr("width").width();

$img.width(oldWidth);

但我认为 Chris G. 的答案也很好,只需确保当您尝试获取宽度时它将被加载:

img.onload = function() {
    if (!img.complete) return; // IMG not loaded
    width = img.width;
   imgManipulationGoesHere();
}

You can remove the attributes, get the width and put the attributes in place again:

var $img = $(img);
var oldWidth = $img.attr("width");
var imgWidth = $img.removeAttr("width").width();

$img.width(oldWidth);

But I think Chris G.'s answer works well too, just making sure it will be loaded when you try to get the width:

img.onload = function() {
    if (!img.complete) return; // IMG not loaded
    width = img.width;
   imgManipulationGoesHere();
}
静若繁花 2024-12-28 09:48:28

适用于大多数最新浏览器和 IE9。

$(window).load(function(){
    $('img.croppable').each(function(){
        alert(this.naturalHeight);
    });
});

Works in most up-to-date browsers and IE9.

$(window).load(function(){
    $('img.croppable').each(function(){
        alert(this.naturalHeight);
    });
});
鸢与 2024-12-28 09:48:28

工作解决方案是:

$(function(){
    $('img.croppable').each(function () {

        var original = new Image(this.src);
        original.onload = function () {
            alert(original.src + ': ' + original.width + 'x' +original.height);
        };
    });
});

The working solution would be:

$(function(){
    $('img.croppable').each(function () {

        var original = new Image(this.src);
        original.onload = function () {
            alert(original.src + ': ' + original.width + 'x' +original.height);
        };
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文