繁体   English   中英

使用 PHP GD 库来调整大小和保存图像是地狱

[英]Using the PHP GD library to resize and save images is HELL

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

但是,我终生无法弄清楚如何让 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);

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


几点提示:

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

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

有更好的解决方案,使用Thumbnailer类实际上可以很有趣地处理图像。

function callb(& $image) {
   $image->thumbFixed(300,200)->save('/some/location/'.$image->filename);
}

// myFile refers to $_FILES['myFile']
// upload image and handle it
Thumbnailer::upload('myFile', 'callb');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM