【问题标题】:Using the PHP GD library to resize and save images is HELL使用 PHP GD 库来调整和保存图像是地狱
【发布时间】:2011-03-10 01:08:03
【问题描述】:

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

但是,我终其一生都无法弄清楚如何让 PHP 检测图像的 MIME 类型,然后将其提供给标题。这是代码,我已经把 cmets 试着让它尽可能清晰:

        $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 时,它会很好地上传到我的图像文件夹,并会生成一个缩略图到我的缩略图文件夹,就像我想要的那样。

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

这段代码对我想要完成的工作有什么好处吗?

提前致谢,

【问题讨论】:

  • 我冒昧地编辑以降低信噪比。非常具体的信息对这个问题没有帮助。我投了赞成票,因为我认为这是一个有趣的问题,祝你好运!
  • 我有几本关于 PHP 的书,并且正在稳步学习。我不“坚持把东西复制粘贴在一起”,这可能有点夸张,当然。你有没有花 5 分钟阅读代码并理解我的问题?它在绞尽脑汁。我喜欢 PHP,但我真的觉得我需要在这方面工作时间比我长得多的人的帮助。
  • @coreyward:我完全不同意没有人必须购买并阅读一整本书才能确定他们甚至想用这种语言编码,他可能只是出于爱好而可能没有真正的现在考虑项目,我完全支持书籍,但只有当你决心掌握一门语言时!毕竟这是一个问答网站,我认为这不是一个微不足道的问题,实际上已经付出了一些努力,并且可能会很有趣。
  • 谢谢你,特鲁法。你说得对,我下次少说话。也许那样我就不会激怒别人了。
  • 没问题,欢迎来到 Stackoverflow!

标签: php thumbnails mime-types gdlib


【解决方案1】:

使用 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()

【讨论】:

  • 谢谢你,Andrew,我会研究一下 Widelmage 的魔法。另外,我确实使用了 move_uploaded_file,只是没有包含它,因为我觉得理解编码问题并不是绝对必要的。另外,你提到了输出?你介意简短地扩展一下吗?最后,我猜有 PHP 函数可以让我在服务器端检测文件信息?我用过getimagesize()之类的吗?
  • @Seraw:没关系,只是重新检查了你的代码......没关系。
  • @AndrewMoore 我尝试使用 WideImage 并使用文件对其进行了测试,并且可以正常工作。但是,我将它应用到我的网站,在那里我从数据库中加载了 url,但我给了我很多乱码。你知道怎么解决吗?
【解决方案2】:

有了更好的解决方案,实际上使用 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');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2012-12-27
    • 2010-11-03
    • 1970-01-01
    相关资源
    最近更新 更多