2016-11-11 4 views
0

Ich habe eine Methode zur Bildgröße, die Bildzeichenfolge und neue Dimensionen akzeptiert und sollte neue Bildzeichenfolge zurückgeben. Aber es funktioniert nichtPHP-Skript ist nicht in der Größe ändern Bild

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

Im Ergebnis bekomme ich das gleiche unbeeinträchtigte Bild.

Antwort

1

Sie Fehler ist auf dieser Linie:

imagepng($src);

Sie das Quellbild nehmen und nicht das Zielbild:

imagepng($dst);