2010-02-01 54 views

回答

161

嘗試這樣的:

list($width, $height) = getimagesize('path_to_image'); 

確保:

  1. 您指定正確的圖像路徑有
  2. 圖像具有讀取權限
  3. CHMOD圖像DIR 755

也嘗試前綴路徑與$_SERVER[DOCUMENT_ROOT],這有時幫助,當你不能夠讀取文件

+0

777在目錄上是不需要的。 – poke 2010-02-01 18:57:21

+0

@poke:你真的100%確定嗎? – Sarfraz 2010-02-18 05:04:15

+3

是的。 777意味着對所有者,團體和所有人都有讀,寫和執行權。你需要閱讀和執行正確的目錄才能訪問,但你不需要寫入權限;而且你也不需要每個人都有這個權利。對於不需要在目錄內創建文件的每個訪問,755應該都可以。 – poke 2010-02-18 18:51:50

50
list($width, $height) = getimagesize($filename) 

或者,

$data = getimagesize($filename); 
$width = $data[0]; 
$height = $data[1]; 
+2

更多信息:http://php.net/manual/en/function.getimagesize.php – davethegr8 2010-02-01 18:42:04

13

和getimagesize()返回包含圖像屬性的數組。

list($width, $height) = getimagesize("path/to/image.jpg"); 

只得到寬度和高度或

list($width, $height, $type, $attr) 

得到一些更多的信息。

+2

感謝您指出我們必須擁有PATH的事實。:) – MEM 2014-07-04 15:31:55

6

PHP的getimagesize()返回一組數據。數組中的前兩項是您感興趣的兩個項目:寬度和高度。要得到這些,你會簡單地要求前兩個指標返回數組中:

var $imagedata = getimagesize("someimage.jpg"); 

print "Image width is: " . $imagedata[0]; 
print "Image height is: " . $imagedata[1]; 

欲瞭解更多信息,請參閱the documentation

6

像這樣:

imageCreateFromPNG($var); 
//I don't know where from you get your image, here it's in the png case 
// and then : 
list($width, $height) = getimagesize($image); 
echo $width; 
echo $height; 
+0

不要工作:'getimagesize()期望參數1是字符串,資源給定',函數'getimagesize'需要filename – lopisan 2015-08-14 07:48:42

3

getimagesize('image.jpg') 功能僅如果allow_url_fopen設置爲1或在服務器裏面的php.ini文件, 如果未啓用它,應該上使用 ini_set('allow_url_fopen',1); 使用getimagesize()函數的文件的頂部。