PHP 创建的文本没有应有的颜色

标签 php image colors png

使用imagettftext创建的文本确实具有完全不同的颜色,因为它应该具有(主要是插入PNG图形的颜色)。 为什么不取颜色呢? 它一定是函数resizePictureAndPlaceOnSign中的东西,因为如果我把它注释掉,那就完美了。

    public function resizePictureAndPlaceOnSign($filename, $new_width,  $dst_x, $dst_y){

        // create figure in memory
        $figure = imagecreatefrompng($filename);


        /* 
            CALC RATIO
        */
        list($width, $height) = getimagesize($filename);

        $vh = ($height/$width);
        $new_height = $vh*$new_width;
        $rnd_new_height = round($new_height, 0); 
        /*  
        */


        // make the figure smaller
        imagecopyresampled($this->picture, $figure, $dst_x, $dst_y, 0, 0, $new_width, $new_height, $width, $height);

        imagedestroy($figure);

    }
    public function drawPicture()
    { 
       // $zufall = rand(1,999);


        // create picture
        $schild_leere_vorlage = imagecreatefrompng("schild_vorlage.png");
        $this->picture = imagecreate(600,192);


        // copy the image into the other image
        # int ImageCopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
        imagecopy($this->picture, $schild_leere_vorlage, 0, 0, 0, 0, 600, 192);


            $filename = "figures/" . $_GET['figure'] . ".png";

            /*********************/
            $this->resizePictureAndPlaceOnSign($filename, 100, 25, 25);


        // create colors
        $color = 0x00000000FF;
        $color =  imagecolorallocate ($this->picture, 0, 0, 255);

        // create text
        imagettftext($this->picture, $this->fontSize , 0, 125, 100,$color , "cooperm.TTF", $this->name);


        // header("Content-Type: image/jpeg");

        imagepng($this->picture);


        //$this->checkFontSize();

        imagedestroy($this->picture);

    }
}

最佳答案

您正在创建调色板图像。当您在其上复制其他图像时,您将使用所有可用的颜色,因此 imagecolorallocate 失败并将 $color 设置为 false。

要解决此问题,请在真彩色图像上进行合成。

$this->picture = imagecreatetruecolor(600,192);

如果由于某种原因需要对输出进行调色,您可以使用 imagetruecolortopalette()来转换它。

关于PHP 创建的文本没有应有的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12044115/

相关文章:

javascript - 存储高分 JavaScript

r - ggplot 中的十六进制颜色未按预期着色

带有 argparse 参数表单列表的 Python 命令行参数不起作用

php - 如何使用codeigniter中的mysql编码功能插入数据

php - Stripe : Received unknown parameter: receipt_email

python - 使用 python 通过 API 将图像上传到 backblaze

python - 使用 ipython 或 python 的 matplotlib.pyplot 无法显示图像

c - 当您使用负参数调用 glClearColor 时会发生什么?

php - 数组归约在变量中返回 bool 值

php - 我无法使用注册时使用的 mySQL 凭据登录