header('Content-Type: image/jpeg') 做黑站

header('Content-Type: image/jpeg') makes black site

我有一个代码可以像这样做拇指:

 switch (strtolower($imgOrginalsize['mime'])) {
            case 'image/jpeg':
                $img = imagecreatefromjpeg($file);
                $new = imagecreatetruecolor($w, $h);
                imagecopyresampled($new, $img, 0, 0, 0, 0, $w, $h, $imgOrginalsize[0], $imgOrginalsize[1]);
                header('Content-Type: image/jpeg'); // when I comment this everything works good, but with this I have a whole black site. What is wrong? In png it doesn't destroy my site.
                imagepng($new, $pathToSave . $file_name, 9);
                imagedestroy($new);
                break;
            case 'image/png':
                $img = imagecreatefrompng($file);
                $new = imagecreatetruecolor($w, $h);
                imagecopyresampled($new, $img, 0, 0, 0, 0, $w, $h, $imgOrginalsize[0], $imgOrginalsize[1]);
                header('Content-Type: image/png');
                imagepng($new, $pathToSave . $file_name, 9);
                imagedestroy($new);
                break;
            default:
                die();
        }

当我添加到我的代码 header jpeg 时,整个网站都是黑色的。在 png 中,它不会破坏它,并且一切正常。我看不出我的错误在哪里。网站看起来像这样:

这很明显。仔细看这两行 ;-)

header('Content-Type: image/jpeg');
imagepng($new, $pathToSave . $file_name, 9);

改成正确的内容类型!使用 imagejpeg() http://php.net/manual/en/function.imagejpeg.php