简体   繁体   English

获取图像的宽度和高度并将其写入本地文件

[英]Get image width and height and write them to local files

I have a set of HTML files with images that do not have width and height attributes set. 我有一组HTML文件,这些文件的图像没有设置width和height属性。

In the HTML file I have something like: <img src="image1.png" alt="image one"></img> 在HTML文件中,我有类似以下内容: <img src="image1.png" alt="image one"></img>

I would like to know if there is a way to automatically get image height and width and write to the local HTML files the image width and height using the image name in the source document as the condition for inserting the two attributes. 我想知道是否有一种方法可以自动获取图像的高度和宽度,并使用源文档中的图像名称作为插入两个属性的条件,将图像的宽度和高度写入本地HTML文件。

Basically what I need is to get the <img /> tag and insert the actual width and height values of the referenced image. 基本上,我需要获取<img />标记并插入参考图像的实际宽度和高度值。 All the images are saved in the same local directory as the source HTML files. 所有图像都与源HTML文件保存在同一本地目录中。 So my final should look like: <img src="image1.png" height="100px" width="230px" alt="image one"></img> 所以我的决赛应该像这样: <img src="image1.png" height="100px" width="230px" alt="image one"></img>

Thanks in advance for your help. 在此先感谢您的帮助。

Given that you have the picture is loaded correctly you can use try this. 如果您已正确加载图片,则可以尝试使用此功能。

$(document).ready(function(){
   var imgHeight = $('img').height();
    var imgWidth = $('img').width();

    $('img').attr('height',imgHeight+'px').attr('width',imgWidth+'px');

});

check full fiddle 检查完整的小提琴

When the picture is loaded, you check the length and width with jquery. 加载图片后,您可以使用jquery检查长度和宽度。 Then you add them as attributes. 然后,将它们添加为属性。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM