How can I use getimagesize() with $_FILES['']?

雨燕双飞 提交于 2019-12-12 07:49:32

问题


I am doing an image upload handler and I would like it to detect the dimensions of the image that's been uploaded by the user.

So I start with:

if (isset($_FILES['image'])) etc....

and I have

list($width, $height) = getimagesize(...);

How am i supposed to use them together?

Thanks a lot


回答1:


You can do this as such

$filename = $_FILES['image']['tmp_name'];
$size = getimagesize($filename);

// or

list($width, $height) = getimagesize($filename);
// USAGE:  echo $width; echo $height;

Using the condition combined, here is an example

if (isset($_FILES['image'])) {
    $filename = $_FILES['image']['tmp_name'];
    list($width, $height) = getimagesize($filename);
    echo $width; 
    echo $height;    
}



回答2:


from php manual very simple example.

list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";



回答3:


list($w, $h) = getimagesize($_FILES['image']['tmp_name']);

From the docs:

Index 0 and 1 contains respectively the width and the height of the image.

Index 2 is one of the IMAGETYPE_XXX constants indicating the type of the image.

Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.

So you can just do list() and don't worry about indexes, it should get the info you need :)




回答4:


Try this for multi images :

for($i=0; $i < count($filenames); $i++){  

    $image_info = getimagesize($images['tmp_name'][$i]);
    $image_width  = $image_info[0];
    $image_height = $image_info[1];
}

Try this for single image :

$image_info = getimagesize($images['tmp_name']);
$image_width  = $image_info[0];
$image_height = $image_info[1];

at least it works for me.



来源:https://stackoverflow.com/questions/9799343/how-can-i-use-getimagesize-with-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!