1

我有一个$this->_tempFile保存上传图像文件的临时路径。

现在我希望缩放图像,它会调整它的大小,但它不能正确缩放。输出是一个大的黑色方块,位于图像的高度和一半(宽度)的底部。我尝试了其他图像,结果都是这些尺寸:293px × 453px,为什么我不知道?

这是我的功能scaleImage();

scaleImage(900, 582);

public function scaleImage($width, $height){
    $rel_difference = array('width'=>0, 'height'=>0);
    if($width > 604) $rel_difference['width'] = ($width-604)/604;
    if($height > 453) $rel_difference['height'] = ($height-453)/453;

    asort($rel_difference);
    $tmpname = $this->_tempFile;
    $newwidth = $width/(1+end($rel_difference));
    $newheight = $height/(1+end($rel_difference)); 
    $newwidth = round($newwidth);
    $newheight = round($newheight);
    $jpeg_quality = 90;

    switch(exif_imagetype($tmpname)) {
        case IMAGETYPE_GIF:
            $img_r = imagecreatefromgif($tmpname);
            break;
        case IMAGETYPE_JPEG:
            $img_r = imagecreatefromjpeg($tmpname);
            break;
        case IMAGETYPE_PNG:
            $img_r = imagecreatefrompng($tmpname);
            break;
        default:
            echo json_encode(array('error' => 'Not an image!'));
            exit(0);
            break;
    }
    $dst_r = ImageCreateTrueColor( $newwidth, $newheight );

    imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $newwidth , $newheight, $width, $height);

    imagejpeg($dst_r,$tmpname,$jpeg_quality);

    }

这里有什么问题?

4

1 回答 1

1

我怀疑您在传递的参数中不小心翻转了高度和宽度。也一样

scaleImage(582, 900);
于 2011-01-28T18:46:03.233 回答