代码之家  ›  专栏  ›  技术社区  ›  wenus

header('Content-Type:image/jpeg')生成黑色站点[关闭]

  •  -1
  • wenus  · 技术社区  · 6 年前

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

     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();
            }
    

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   delboy1978uk    6 年前

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

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

    imagejpeg() http://php.net/manual/en/function.imagejpeg.php