我正在编写一个脚本,该脚本将从用户输入上传文件,将其调整为缩略图并将两个新文件名添加到数据库中。

但是,我终生无法弄清楚如何让 PHP 检测图像的 MIME 类型,然后将其提供给标题。这是代码,我已经添加了注释以尝试使其尽可能清晰:

        $picture = $_FILES['picture']['name'];

        /*original file location*/
        $file = 'picture/'.$picture.'';
        /*save thumbnail location*/
        $save = 'thumb/tn-'.$picture.'';
        /*append thumbnail filename with tn-*/
        $thumb = 'tn-'.$picture.'';
        /*get original file size*/
        list($width, $height) = getimagesize($file);
        /*get image MIME type*/
        $size = getimagesize($file);
        $fp = fopen($file, "r");
        if ($size && $fp)
        {
        header("Content-type:".$size['mime']);
        fpassthru($fp);
        exit;
        }
        else
        {
        echo 'Error getting filetype.';
        }
        /*define thumbnail dimensions*/
        $modwidth = 300;
        $modheight = 200;
        /*check to see if original file is not too small*/
        if (($width > 301) || ($height > 201))
        {
        /*create shell for the thumbnail*/
        $tn = imagecreatetruecolor($modwidth, $modheight);

        /*HERE IS WHERE I HAVE TROUBLE*/

        /*if MIME type is PNG*/
        if ($size['mime'] == "image/png")
            {
        /*try to create PNG thumbnail*/
        $image = imagecreatefrompng($file);
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
        imagepng($tn, $save, 100);
            }

        /*if MIME type is JPEG*/

        if ($size['mime'] == "image/jpeg")
            {
        $image = imagecreatefromjpeg($file);
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
        imagejpeg($tn, $save, 100);
            }

        /*if MIME type is GIF*/

        if ($size['mime'] == "image/gif")
            {
        $image = imagecreatefromgif($file);
        imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
        imagegif($tn, $save, 100);
            }
        }
        else { echo 'Your file is too small.'; }

所以这是我不明白的部分:当我上传 .jpeg 时,代码可以正常工作,但是如果它是 PNG 或 GIF,它会显示一个页面,上面写着“图像 127.0.0.1:8888” '无法显示,因为它包含错误。'我假设它不能有正确的 Header MIME 类型,但它可能是别的东西吗?

当我选择 .jpeg 时,它会很好地上传到我的图像文件夹,并像我想要的那样为我的 Thumbnail 文件夹生成一个缩略图。

但是一旦我尝试使用 PNG 和 GIF,它就会失败。我一定错过了一些明显的东西吗?

这段代码对我想要完成的事情有好处吗?

提前致谢,

最佳答案

使用 PHP 的 GD 扩展真的很痛苦。就个人而言,我建议采用像 WideImage 这样的抽象库。这将大大简化您的代码:

$picture = $_FILES['picture']['name'];

/*original file location*/
$file = 'picture/'.$picture;
move_uploaded_file($_FILES["userfile"]["tmp_name"], $picture);

/*save thumbnail location*/
$save = 'thumb/tn-'.$picture;

// Magic Begins Here ......

require "path-to/WideImage.php";

$image = WideImage::load($picture);
$thumb = $image->resizeDown(300, 200, 'inside');

$thumb->saveToFile($save);

是的,上面的所有代码都减少到了四个 channel 。

几点提示:
  • 永远不要依赖 $_FILES[]['mime'] 。这是由用户代理发送的,不可信。
  • 出于同样的原因,我不会将我的文件名基于 $_FILES[]['name']

  • 此外,您的原始代码不起作用,因为您从不 move_uploaded_file()

    关于php - 使用 PHP GD 库来调整大小和保存图像是 hell ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5254325/

    10-09 20:03
    查看更多