我无法在imagecopyresampled后输出新图像

时间:2012-07-16 05:50:29

标签: php image resize

有人可以帮助我..我想检查我的调整大小是否正常,但我无法输出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();

1 个答案:

答案 0 :(得分:1)

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

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