使用PHP GD库调整大小并保存图像是HELL

时间:2011-03-10 01:08:03

标签: php thumbnails mime-types gdlib

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

但是,我不能为我的生活弄清楚如何让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,就会失败。我一定错过了一些明显的东西吗?

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

提前致谢,

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

获得更好的解决方案,使用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');