There is a picture of multicolored squares, transmitted to base64, decoded: $pngString = base64_decode($pngBase64); When saving near the squares appear pixels with different colors, they are not needed. I tried with 2 options, none of them worked as I needed:

  1. In this embodiment, if you set a white background - everything is OK, with black too, but except for the primary colors, intermediate colors appear on the borders of the rectangles (for example, if the rectangle is red, then burgundy pixels appear) ..

     $img = imagecreatefromstring($pngString); $black = imagecolorallocate($img, 0, 0, 0); imagefill($img, 0, 0, $black); imagepng($img, $outputFilePath); imagedestroy($img); 
  2. Overlaying a new image:

     $img = imagecreatefromstring($pngString); $w = imagesx($img); $h = imagesy($img); $img2 = imagecreatetruecolor($w, $h); imagefilledrectangle($img2, 0, 0, $w, $h, imagecolorallocate($img2, 0, 0, 0)); imagecopyresampled($img2, $img, 0, 0, 0, 0, $w, $h, $w, $h); imagepng($img2, $outputFilePath); imagedestroy($img); imagedestroy($img2); 

0