我可以';t在imagecopy重新采样后输出新图像


I can't output the new image after imagecopyresampled

有人能帮我吗。。我想检查我的调整大小是否正常,但我无法输出imagecopyresoampled图像。它现在输出了一堆奇怪的角色。

//uploading image
    $image_file = $_FILES['image']['tmp_name'];
    $image_size = @getimagesize($image_file);
    $image_mime = $image_size['mime'];
    $image_width = $image_size['0'];
    $image_height  = $image_size['1'];
    $image_ratio = $image_width/$image_height;
    //not an image
    if ($image_size === false) {
        error('1');
    } 
    //check to see if valid image type
    switch ($image_mime){
        case 'image/jpg':
        case 'image/jpeg':
        case 'image/pjpeg':
            $image_file = @imagecreatefromjpeg($image_file);
            break;
        case 'image/png':
            $image_file = @imagecreatefrompng($image_file);
            break;
        default: 
            error('2');
            break;
    }
    if ($image_width < 450 || $image_height < 350) {
        //image too small
        error('3');
    } 
    if ($image_ratio >= 1){
        //resize by height
        $height = 350;
        $ratio = $height/$image_height;
        $width = $image_width * $ratio;
    } else {
        //resize by width
        $width = 450;
        $ratio = $width/$image_width;
        $height = $image_height * $ratio;
    }
    //get the center axis
    $x_center = ($image_width - $width)/2;
    $y_center = ($image_height - $height)/2;
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $image_file, 0, 0, $x_center, $y_center, $width, $height, $image_width, $image_height);
    imagepng($new_image);

    exit();

那些奇怪的字符二进制输出的图像。你的浏览器不知道这应该是一张图像,因为你没有告诉它。在图像输出之前添加一个标题,以通知浏览器它正在处理什么样的数据:

header('Content-Type: image/png');