PHP/Wordpress -获取<img>的宽度/高度属性


PHP/Wordpress - Get the width/height attributes of a <img>

我目前正在制作一个基于wordpress的新版本的网站,并且有一个功能我想添加,但不知道如何添加。

所以也许你可以帮我…我希望如此!

我想在the_content上添加一个过滤器,这适用于每个<img ...>。该函数将在变量($thisWidth, $thisHeight)中获得宽度和高度属性(例如<img src="image.jpg" **height="120" width="300**" alt="test" />)。

然后比较width &每个图像的高度,以知道它是一个风景或肖像格式,并添加相应的类。简单:if($width > $height) { ... } else { ... }

我不习惯使用regex等,所以这就是为什么我需要你的帮助。我的技能允许我编写函数的基础,但仅此而已:

function imgClass($content) {
   /* for each '<img ....>' Loop ? */
   $thisWidth = /* get html width attribute */;
   $thisHeight = /* get html height attribute */;
   if($width > $height) { /* add class='landscape' */ } else { /* add class='portrait' */ }
   return $content;
}
add_filter('the_content','imgClass');
我将非常感谢任何人能够帮助我…提前感谢!

这是一个很好的例子,使用javascript

    <!DOCTYPE html>
<html>
<body>
<img id="planets" src="planets.gif" width="145" height="126" usemap="#planetmap">

<p>Click the button to display the coordinates for the "planets.gif" area.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
    var x = document.getElementById("planets").width;
    var y = document.getElementById("planets").height;
}
</script>
</body>
</html>