获取图像高度和宽度为整数值?

时间:2010-02-01 18:36:26

标签: php

我尝试使用PHP函数getimagesize,但我无法将图像宽度和高度提取为整数值。

我怎样才能做到这一点?

6 个答案:

答案 0 :(得分:182)

试试这样:

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

确保:

  1. 您指定了正确的图像路径
  2. 图片具有读取权限
  3. Chmod图像目录为755
  4. 还尝试使用$_SERVER[DOCUMENT_ROOT]为路径添加前缀,这有时会在您无法读取文件时有所帮助

答案 1 :(得分:58)

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

或者,

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

答案 2 :(得分:13)

getimagesize()返回包含图像属性的数组。

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

获取宽度和高度或

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

获取更多信息。

答案 3 :(得分:6)

PHP的getimagesize()返回一个数据数组。数组中的前两项是您感兴趣的两个项目:宽度和高度。要获得这些,您只需在返回的数组中请求前两个索引:

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

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

有关详细信息,请参阅the documentation

答案 4 :(得分: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;

答案 5 :(得分:3)

getimagesize('image.jpg') 函数只有在allow_url_fopen设置为1或服务器上的php.ini文件内时才有效, 如果没有启用,应该使用 ini_set('allow_url_fopen',1); 在文件顶部使用getimagesize()函数。

相关问题