1

如何在不丢失所有颜色的情况下创建图像?它正在创建带有斑点的图像,并且更改颜色编号没有任何区别。我开始使用的 PNG 图像(通过 PHP 上传)要清晰得多。

//Create an image from the source
$srcImage = imagecreatefromstring( file_get_contents( $sourcePath ) );

//Get the source width and height
list( $width, $height ) = getimagesize( $sourcePath );

//Create image to paint on
$canvas = imagecreatetruecolor( $width, $height );

//Add alpha for transparency
$bga = imagecolorallocatealpha( $canvas, 0, 0, 0, 127 );
imagecolortransparent( $canvas, $bga );
imagefill( $canvas, 0, 0, $bga );

//Convert to palette (ignores the number of colors)
imagetruecolortopalette( $canvas, true, 1000 );

//Retain transparency
imagesavealpha( $canvas, true );

//Save as PNG to file
imagepng( $canvas, $destPath );

//Remove temporary data
imagedestroy( $canvas );

如何创建具有更多颜色的调色板图像 png?

4

0 回答 0