imagecopyresampled 后显示的特殊字符

Special characters displayed after imagecopyresampled

我制作了一个脚本来在我的网站上上传头像,它按预期工作(图像已调整大小并上传)但我不明白为什么这些特殊字符在页面重新加载后显示。

脚本:

//Session for test purpose
session_start();
$_SESSION['user']['id'] = 1337;
/*************************
    AVATAR UPLOAD
**************************/

$msg='';

if(isset($_POST['upload'])){


    $avatar = $_FILES['avatar']['name'];
    $avatar_tmp = $_FILES['avatar']['tmp_name'];

    if(!empty($avatar_tmp)){
        //Extension test
        $image = explode('.', $avatar);
        $image_ext = end($image);


        if(!in_array(strtolower($image_ext), array('png', 'gif', 'jpeg','jpg'))){
            $msg .= '<div class="error">Choosen file is not an image</div>';
        }
        //Mime test and image create
        else{

            $image_size = getimagesize($avatar_tmp);

            if($image_size['mime'] == 'image/jpeg'){
                $image_src = imagecreatefromjpeg($avatar_tmp);
            }
            elseif($image_size['mime'] == 'image/png'){
                $image_src = imagecreatefrompng($avatar_tmp);
            }
            elseif($image_size['mime'] == 'image/gif'){
                $image_src = imagecreatefromgif($avatar_tmp);
            }
            else{

                $msg .= '<div class="error">Choosen file is not an image</div>';

            }

        }

        //No error -> resize and upload
        if(empty($msg)){

            $image_width = 150;

            if($image_size[0] <= $image_width){
                $image_finale = $image_src;
            }
            else{
                $new_width[0] = $image_width;
                $new_height[1] = ($image_size[1] / $image_size[0]) * $image_width;

                $image_finale = imagecreatetruecolor($new_width[0], $new_height[1]);
                imagecopyresampled($image_finale, $image_src, 0, 0, 0, 0, $new_width[0], $new_height[1], $image_size[0], $image_size[1]);

            }

            imagejpeg($image_finale, 'img/' . $_SESSION['user']['id'] . '.jpg');
            imagejpeg($image_finale);

            $msg .= '<div class="success">Avatar uploaded</div>';
        }
    }
}

显示字符:

ÿØÿàJFIFÿþ>CREATOR: gd-jpeg v1.0 (using IJG JPEG v90), default quality ÿÛC     $.' ",#(7),01444'9=82<.342ÿÛC ...    

在此先感谢您的帮助。

你打电话给

imagejpeg($image_finale);

最后第二次,我确定你的意思是

imagedestroy($image_finale);

因为imagejpeg没有第二个参数(路径)输出到浏览器,没有header之前就给了垃圾。