0

有人可以帮帮我.. 我想检查我的调整大小是否正常工作,但我无法输出 imagecopyresampled 图像。它现在输出一堆奇怪的字符。

//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();
4

1 回答 1

1

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

header('Content-Type: image/png');
于 2012-07-16T05:53:15.293 回答