【问题标题】:use php to generate image, the background is always in wrong color用php生成图片,背景总是颜色不对
【发布时间】:2011-11-28 13:57:17
【问题描述】:

功能是当用户上传图片时,程序会生成一个新的白色背景正方形图片,并将用户的图片放在图片的中心。

但问题是,我将背景设置为白色,它总是显示黑色。

代码是

$capture = imagecreatetruecolor($width, $height);
$rgb = explode(",", $this->background); 
$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]); 
imagefill($capture, 0, 0, $white); 

控制颜色的代码是 protected $background = "255,255,255";

我已尝试将$white = imagecolorallocate($capture, $rgb[0], $rgb[1], $rgb[2]); 更改为$white = imagecolorallocate($capture, 255, 255, 255);。但背景仍然显示为黑色。

感谢您的任何回答

【问题讨论】:

  • “不工作”是什么意思?背景颜色是什么?另外,你是如何渲染图像的?
  • @Zach Rattner 它仍然显示为黑色而不是白色。我使用 imagejpeg 来渲染图像
  • header ('Content-Type: image/jpeg'); $im = @imagecreatetruecolor(120, 20) or die('Cannot Initialize new GD image stream'); $rgb = explode(",", "255,255,255"); $white = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); imagefill($im, 0, 0, $white); imagejpeg($im); 为我工作,你确定以后不会覆盖它吗?
  • @Deebster 谢谢。我发现我没有更改 $rgb = explode.但是还是需要imagefilledrectangle()函数

标签: php image gd


【解决方案1】:

来自手册imagecreatetruecolor() returns an image identifier representing a black image of the specified size. 第一次调用 imagecolorallocate 设置基于调色板的图像的背景,但不是真彩色图像。

我在真彩色图像上设置背景颜色的方法是用一个实心矩形填充它。

imagefilledrectangle($capture, 0, 0, $width, $height, $white);

【讨论】:

  • 矩形有效,但我还需要将$rgb = explode(",", $this->background); 更改为$rgb = explode(",", "255,255,255");
【解决方案2】:

imagefill($im, x, y, $color) 呢?

$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);

这将用红色填充整个图像,x,y 参数是矩形填充的起点。

【讨论】:

    猜你喜欢
    • 2012-10-24
    • 2018-06-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2013-04-09
    相关资源
    最近更新 更多