Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php imagepng和imagegif_Php_Gd - Fatal编程技术网

Php imagepng和imagegif

Php imagepng和imagegif,php,gd,Php,Gd,我需要一些关于PHP GD的帮助。这是我的一段代码 header("Content-type: image/gif"); $image = imagecreatetruecolor(550, 20); imagealphablending($image, false); $col=imagecolorallocatealpha($image,255,255,255,127); $black = imagecolorallocate($image, 0,

我需要一些关于PHP GD的帮助。这是我的一段代码

    header("Content-type: image/gif");

    $image = imagecreatetruecolor(550, 20);
    imagealphablending($image, false);

    $col=imagecolorallocatealpha($image,255,255,255,127);
    $black = imagecolorallocate($image, 0, 0, 0);

    imagefilledrectangle($image,0,0,550,20,$col);
    imagealphablending($image, true);

    $font_path = 'font/arial.ttf';
    imagettftext($image, 9, 0, 16, 13, $black, $font_path, $lastlisten);
    imagesavealpha($image, true);

    imagepng($image);
问题是当我使用imagepng时,它可以像这样很好地显示png

但如果我使用imagegif,它将变成这样。

我尝试过使用不同的gif和png标题。imagegif的结果仍然相同。
问题是为了正确地显示GIF版本,我该如何制作?谢谢你

第一个问题:你的角色很难看:那是因为你需要在使用imagecreatetruecolor时设置一个颜色较少的调色板

$image = imagecreatetruecolor(550, 20);
imagetruecolortopalette($image, true, 256);
应该解决这个问题

第二个问题:没有透明度

如你所见

imagesavealpha()设置标志以尝试保存完整的alpha通道 保存PNG时的信息(相对于单色透明度) 图像

此函数不适用于GIF文件

您可以改为使用,但这并不完美,因为字体具有抗锯齿功能,使其边框更甜

这是我的密码:

<?php

$lastlisten = "test test test test test test";

error_reporting(E_ALL);
header("Content-type: image/gif");

$image = imagecreatetruecolor(550, 20);
imagetruecolortopalette($image, true, 256);

$transparent=imagecolorallocatealpha($image,255,255,255,127);
imagecolortransparent( $image, $transparent);
imagefilledrectangle($image,0,0,550,20,$transparent);

$black = imagecolorallocate($image, 0, 0, 0);
$font_path = dirname(__FILE__) . '/font.ttf';
imagettftext($image, 9, 0, 16, 13, $black, $font_path, $lastlisten);

imagegif($image);

GIF图像最多支持256种颜色。最重要的是,它只支持索引透明度:像素可以是100%不透明或100%透明

另一方面,PNG支持真实(数百万)彩色图像,并支持alpha通道透明度。这意味着像素可以是100%不透明、100%透明或介于两者之间的任何东西


您提到的PNG图像的边缘可能部分透明,因此浏览器可以轻松地将这些像素与背景颜色混合,从而产生平滑效果。PNG是一个更好的选择。

Gif不支持透明度,限制为256色。除非您要分配由所需的抗锯齿文本创建的所有可能的灰度,否则您将得到这样的块结果。您的意思是逐像素分配吗?手动?