2012-02-16 42 views
1

我正在構建自定義WordPress主題,並在顯示博客帖子圖片的首頁上放映幻燈片。獲取不在內容中的圖片高度

現在,如果圖片高度超過300px,我想強制它的widthand垂直居中。所以我的問題是:

有沒有一種方法,在PHP或JavaScript來獲取圖像的高度只使用圖像的URL?

編輯:我想這樣的:<?php list($height) = getimagesize(echo catch_that_image()); echo "<p>$height</p>"; ?>其中catch_that_image()返回URL,但不知何故,這並不工作(我認爲第一回聲打破它)。有任何想法嗎?

謝謝你的幫助。

回答

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>"; 
?> 
+0

會嘗試。有史以來第一次我感動的PHP :)編輯:它的作品!謝謝! – cmplieger 2012-02-16 00:38:47

+0

@SnippetSpace,但返回數組中的第一個元素是寬度。嘗試'列表(,$高度)'的高度 – Cheery 2012-02-16 00:43:32

0

請參閱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"; 
?>