在 PHP 中,使用 imagecreatefrompng 函數創建圖像時,可以通過以下方法提高圖像質量:
$source = imagecreatefrompng('source.png');
$destination = imagecreatetruecolor(800, 600);
imagecopyresampled($destination, $source, 0, 0, 0, 0, 800, 600, imagesx($source), imagesy($source));
imagepng($destination, 'destination.png', 9); // 9 是 PNG 格式的壓縮質量,范圍為 0(最差質量,最小文件大小)到 9(最佳質量,最大文件大小)
imagedestroy($source);
imagedestroy($destination);
$source = imagecreatefrompng('source.png');
$destination = imagecreatetruecolor(800, 600);
imagealphablending($destination, false);
imagesavealpha($destination, true);
$transparent = imagecolorallocatealpha($destination, 255, 255, 255, 127);
imagefilledrectangle($destination, 0, 0, 800, 600, $transparent);
imagecopyresampled($destination, $source, 0, 0, 0, 0, 800, 600, imagesx($source), imagesy($source));
header('Content-type: image/png');
imagepng($destination);
imagedestroy($source);
imagedestroy($destination);
$source = imagecreatefrompng('source.png');
$destination = imagecreatetruecolor(800, 600);
imagecopyresampled($destination, $source, 0, 0, 0, 0, 800, 600, imagesx($source), imagesy($source));
imagepng($destination, 'destination.png', 9); // 9 是 PNG 格式的壓縮質量,范圍為 0(最差質量,最小文件大小)到 9(最佳質量,最大文件大小)
imagedestroy($source);
imagedestroy($destination);
通過以上方法,可以在使用 imagecreatefrompng 函數時提高圖像質量。