【问题标题】:Get image height that is not in content获取不在内容中的图像高度
【发布时间】:2012-03-07 09:48:42
【问题描述】:

我正在构建一个自定义 wordpress 主题,并在首页上有一个幻灯片,显示来自博客文章的图像。

现在,如果图像的高度超过 300 像素,我想强制其宽度并将其垂直居中。所以我的问题是:

有没有办法在 php 或 javascript 中仅使用图像的 URL 来获取图像的高度?

编辑:我试过这个:<?php list($height) = getimagesize(echo catch_that_image()); echo "<p>$height</p>"; ?> 其中catch_that_image() 返回 URL,但不知何故这不起作用(我认为第一个回声会破坏它)。有什么想法吗?

感谢您的帮助。

【问题讨论】:

    标签: php javascript image wordpress height


    【解决方案1】:

    参见 PHP 中的 getimagesize()。这将做你正在寻找的。 http://php.net/manual/en/function.getimagesize.php

    <?php
        $url = "http://www.example.com/image.jpg";
    
        list($width, $height) = getimagesize($url);
        echo "Width: $width<br />Height: $height";
    ?>
    

    【讨论】:

      【解决方案2】:

      image by only using the URL of the image?

      var img = new Image();
      img.onload = function() {
          alert('The size is ' + this.width + 'x' + this.height);
      };
      img.src = 'some image URL';
      

      试试看 http://jsfiddle.net/NPSMN/

      ps:

      我试过这个:...在哪里 catch_that_image() 返回 URL,但不知何故这不起作用(我 认为第一个回声会破坏它)。有什么想法吗?

      您是否仔细查看了您的代码? echo 输出到浏览器,而不是函数的参数。

      <?php 
      list(,$height) = getimagesize(catch_that_image()); 
      echo "<p>$height</p>"; 
      ?>
      

      【讨论】:

      • 会尝试的。我第一次接触 php :) 编辑:它有效!谢谢!
      • @SnippetSpace 但返回数组中的第一个元素是宽度。试试list(,$height) 的高度
      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2012-10-18
      相关资源
      最近更新 更多