php 获取图像高度和宽度作为整数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2179100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Image Height and Width as integer values?
提问by Mostafa Elkady
I've tried to use the PHP function getimagesize, but I was unable to extract the image width and height as an integer value.
我尝试使用 PHP 函数getimagesize,但无法将图像宽度和高度提取为整数值。
How can I achieve this?
我怎样才能做到这一点?
回答by Sarfraz
Try like this:
像这样尝试:
list($width, $height) = getimagesize('path_to_image');
Make sure that:
确保:
- You specify the correct image path there
- The image has read access
- Chmod image dir to 755
- 您在那里指定正确的图像路径
- 该图像具有读取权限
- chmod 图像目录为 755
Also try to prefix path with $_SERVER["DOCUMENT_ROOT"], this helps sometimes when you are not able to read files.
还尝试使用前缀路径$_SERVER["DOCUMENT_ROOT"],这有时在您无法读取文件时会有所帮助。
回答by davethegr8
list($width, $height) = getimagesize($filename)
Or,
或者,
$data = getimagesize($filename);
$width = $data[0];
$height = $data[1];
回答by Matt
getimagesize() returns an array containing the image properties.
getimagesize() 返回一个包含图像属性的数组。
list($width, $height) = getimagesize("path/to/image.jpg");
to just get the width and height or
只获取宽度和高度或
list($width, $height, $type, $attr)
to get some more information.
获取更多信息。
回答by Julien
Like this :
像这样 :
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;
回答by Sampson
PHP's getimagesize()returns an array of data. The first two items in the array are the two items you're interested in: the width and height. To get these, you would simply request the first two indexes in the returned array:
PHPgetimagesize()返回一个数据数组。数组中的前两项是您感兴趣的两项:宽度和高度。要获得这些,您只需请求返回数组中的前两个索引:
var $imagedata = getimagesize("someimage.jpg");
print "Image width is: " . $imagedata[0];
print "Image height is: " . $imagedata[1];
For further information, see the documentation.
有关更多信息,请参阅文档。
回答by ronforever
getimagesize('image.jpg')function works only if allow_url_fopenis set to 1 or On inside php.ini file on the server,
if it is not enabled, one should use
ini_set('allow_url_fopen',1); on top of the file where getimagesize() function is used.
getimagesize('image.jpg')函数仅allow_url_fopen在服务器上的 php.ini 文件中设置为 1 或 On时才有效,如果未启用,则应在使用
ini_set('allow_url_fopen',1); getimagesize() 函数的文件顶部使用。

