PHP脚本没有调整图像大小

时间:2016-11-11 13:28:24

标签: php base64

我有一个调整图像大小的方法,它接受图像字符串和新尺寸,并应返回新的图像字符串。但它不起作用

   public function putBase64ThumbToFile($base64String){
    $fs = new Filesystem();
    $TEMP_FOLDER_NAME = "tmpimageupload";

    $pos  = strpos($base64String, ';');
    $type = explode('/', substr($base64String, 0, $pos))[1];

    $base64String = str_replace('data:image/'. $type .';base64,', '', $base64String);
    $base64String = str_replace(' ', '+', $base64String);
    $data = base64_decode($base64String);

    $resizedImage = $this->resizeAndCompressImage($data, 100, 100);

    $fs->mkdir($TEMP_FOLDER_NAME, 0700);
    $tempFilePath = $TEMP_FOLDER_NAME.'/'.uniqid() . '.' . $type;

    file_put_contents($tempFilePath, $resizedImage);
}

public function resizeAndCompressImage($data, $w, $h) {
    list($width, $height) = getimagesizefromstring($data);
    $r = $width / $height;

    if ($w/$h > $r) {
        $newwidth = $h*$r;
        $newheight = $h;
    } else {
        $newheight = $w/$r;
        $newwidth = $w;
    }

    $src = imagecreatefromstring($data);
    $dst = imagecreatetruecolor($newwidth, $newheight);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    ob_start();
    imagepng($src);
    $image = ob_get_clean();

    return $image;
}

在结果中,我得到了相同的未图像。

1 个答案:

答案 0 :(得分:1)

你错了就在这一行:

imagepng($src);

您拍摄源图像而不是目标图像:

imagepng($dst);