2016-11-11 60 views
0

我有一個方法來調整圖像的大小,它接受圖像字符串和新維度,並應返回新的圖像字符串。但它不起作用PHP腳本不調整圖像

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; 
} 

在結果我得到相同的unresized圖像。

回答

1

你的錯誤是在這條線:

imagepng($src);

你把源圖像,而不是目標圖像:

imagepng($dst);