1

我正在为我的网站制作附件系统。在基于表单的上传方法中,您可以使用以下方式获取图像大小:

$SizeResult = getimagesize($_FILES['file']['tmp_name']);

但是通过 HTML5 的文件 API 使用拖放上传系统,文件不是通过表单发布上传,而是通过黑魔法和巫术上传。

$File = file_get_contents('php://input');

在这种情况下, $File 不是name,而是文件的实际二进制内容。

当getimagesize()似乎只接受文件时,如何获得所述 $File 的大小?在内部,getimagesize想要从磁盘本身打开文件。它不想接受 $File。

任何人都知道我该如何解决这个问题?我希望避免将文件保存到磁盘。

4

3 回答 3

4

这就是我的意思,请尝试一下:

$File = file_get_contents('php://input');
$image = ImageCreateFromString($File);
echo ImageSX($image); // width
echo ImageSY($image); // height
于 2012-03-02T03:38:08.657 回答
2

我提出了一个我认为最好的解决方案。我必须承认,使用getimagesize()函数的主要原因是获取文件的 MIME 类型(以确保它是图像)。获得大小是一个额外的好处,但我可以很容易地围绕exif_imagetype()函数提出问题。问题是一样的,因为这两个函数都只接受一个文件;不是文件内容。

所以我所做的基本上就是查看 PHP 源代码,看看exif_imagetype()是如何读取 MIME 信息的。事实证明它只读取文件的前 12 个字节。我复制了它的功能,如下所示:

function GetConstMimeArray()
{
    // MIME type markers (taken from PHP sourcecode consts in \ext\standard\image.c starting at line 39).
    return array
    (
        'gif' => array(ord('G'), ord('I'), ord('F')),
        'psd' => array(ord('8'), ord('B'), ord('P'), ord('S')),
        'bmp' => array(ord('B'), ord('M')),
        'swf' => array(ord('F'), ord('W'), ord('S')),
        'swc' => array(ord('C'), ord('W'), ord('S')),
        'jpg' => array(0xff, 0xd8, 0xff),
        'png' => array(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a),
        'tif_ii' => array(ord('I'), ord('I'), 0x2a, 0x00),
        'tif_mm' => array(ord('M'), ord('M'), 0x00, 0x2a),
        'jpc' => array(0xff, 0x4f, 0xff),
        'jp2' => array(0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a),
        'iff' => array(ord('F'), ord('O'), ord('R'), ord('M')),
        'ico' => array(0x00, 0x00, 0x01, 0x00)
    );
}

// Get an array of known MIME headers.
$MimeTypeConsts = GetConstMimeArray();

// Get first 12 bytes of file from stream to do a MIME check.
$File = file_get_contents('php://input', null, null, 0, 12);
if(strlen($File) < 12)
    return;

// Scan first 12 bytes of file for known MIME headers.
$MatchedMime = '';
foreach($MimeTypeConsts as $Type => $Bytes)
{
    $NumMatching = 0;
    $NumBytes = count($Bytes);
    for($i = 0; $i < $NumBytes; $i++)
    {
        if(ord($File[$i]) == $Bytes[$i])
            $NumMatching++;
        else
            break;
    }
    if($NumMatching == $NumBytes)
    {
        $MatchedMime = $Type;
        break;
    }
}

// Check if the file does NOT have one of the known MIME types.
if(strlen($MatchedMime) <= 0)
    return;

// If we fix up TIF_TT and TIF_MM, you can use $MatchedMime in lieu
// of the extension on the file name.
if($MatchedMime == 'tif_ii' || $MatchedMime == 'tif_mm')
    $MatchedMime = 'tif';

// What's the max size allowed to upload?
$MaxSize = min(ReturnBytes(ini_get('post_max_size')), MAX_UPLOAD_SIZE);

// Get full file.
$File = file_get_contents('php://input', null, null, 0, $MaxSize + 8);

// Get file size.
$Size = strlen($File);
if($Size > $MaxSize)
    return;

// Get hash of the file contents.
$Hash = hash('SHA1', $File, true);

file_put_contents(UPLOADS_DIR.'/'.bin2hex($Hash).'.'.$MatchedMime, $File);

该文件现在将保存在 UPLOADS_DIR 中,使用哈希作为其名称,并使用 MIME 类型作为其扩展名。(文件名上的任何扩展名都将被忽略且不使用。)

于 2012-03-03T22:27:59.580 回答
0

希望这可以帮助

<?php
list($width, $height, $type, $attr) = getimagesize("img/flag.jpg");
echo "<img src=\"img/flag.jpg\" $attr alt=\"getimagesize() example\" />";
?>
于 2012-03-02T04:55:06.607 回答