php 如何将 getimagesize() 与 $_FILES[''] 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9799343/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:38:49  来源:igfitidea点击:

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

phpfileupload

提问by eric01

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

非常感谢

回答by Starx

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;    
}

回答by Khurram Ijaz

from php manual very simple example.

来自php手册非常简单的例子。

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

回答by Andreas Wong

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.

索引 0 和 1 分别包含图像的宽度和高度。

索引 2 是指示图像类型的 IMAGETYPE_XXX 常量之一。

索引 3 是具有正确 height="yyy" width="xxx" 字符串的文本字符串,可以直接在 IMG 标签中使用。

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

所以你可以只做 list() 而不用担心索引,它应该得到你需要的信息:)

回答by Sulung Nugroho

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.

至少它对我有用。