【问题标题】:How to pick a color to make transparent in images?如何选择颜色以使图像透明?
【发布时间】:2010-11-06 03:08:56
【问题描述】:

第一个问题,请温柔;-)

我编写了一个图像类,它使简单的事情(矩形、文本)变得更容易一些,基本上是 PHP 图像函数的一堆包装方法。
我现在要做的是允许用户定义一个选择,并且让以下图像操作只影响所选区域。我想我会通过将图像复制到 imgTwo 并从中删除所选区域来做到这一点,像往常一样对原始图像执行以下图像操作,然后在调用 $img->deselect() 时,将 imgTwo 复制回原始图像,并销毁副本。

  • 这是最好的方法吗?显然,在选定区域内定义取消选定的区域会很棘手,但我现在可以忍受 :)

然后,我从副本中删除选择的方式是绘制一个透明颜色的矩形,这是可行的 - 但我不知道如何选择该颜色,而确保它不会出现在图像的其余部分。此应用程序中的输入图像是真彩色 PNG,因此没有带有颜色索引的调色板(我认为?)。

  • 必须有更好的方法,而不是收集每个单独像素的颜色,然后找到 $existing_colours 数组中没有出现的颜色.. 对吗?

【问题讨论】:

    标签: php colors transparency gd


    【解决方案1】:

    PNG 透明度与 GIF 透明度的工作方式不同 - 您无需将特定颜色定义为透明。 只需使用imagecolorallocatealpha() 并确保已将imagealphablending() 设置为false

    // "0, 0, 0" can be anything; 127 = completely transparent
    $c = imagecolorallocatealpha($img, 0, 0, 0, 127);
    
    // Set this to be false to overwrite the rectangle instead of drawing on top of it
    imagealphablending($img, false);
    
    imagefilledrectangle($img, $x, $y, $width - 1, $height - 1, $c);
    

    【讨论】:

    • 干杯,这似乎有效,但我必须添加 imagecolortransparent($img, $c);还有,听起来对吗?
    • 嗯,不适合 PNG... 有点奇怪。我不认为我之前通过 GD 创建了一个托盘 PNG,所以也许你需要它?我会感到惊讶,但话又说回来,一切皆有可能!
    • 尝试使用imagesavealpha($img, true);而是。
    • 我只是在网页中显示它们(它们是带有实时数据的图表)。它可能与源图像或它们的加载方式有关 - 无论如何,它正在做我现在想要的:-)
    【解决方案2】:

    代码最终看起来像这样:

    # -- select($x, $y, $x2, $y2)
    function select($x, $y, $x2, $y2) {
      if (! $this->selected) { // first selection. create new image resource, copy current image to it, set transparent color
        $this->copy = new MyImage($this->x, $this->y); // tmp image resource
        imagecopymerge($this->copy->img, $this->img, 0, 0, 0, 0, $this->x, $this->y, 100); // copy the original to it
        $this->copy->trans = imagecolorallocatealpha($this->copy->img, 0, 0, 0, 127); // yep, it's see-through black
        imagealphablending($this->copy->img, false);                                  // (with alphablending on, drawing transparent areas won't really do much..)
        imagecolortransparent($this->copy->img, $this->copy->trans);                  // somehow this doesn't seem to affect actual black areas that were already in the image (phew!)
        $this->selected = true;
      }
      $this->copy->rect($x, $y, $x2, $y2, $this->copy->trans, 1); // Finally erase the defined area from the copy
    }
    
    # -- deselect()
    function deselect() {
      if (! $this->selected) return false;
      if (func_num_args() == 4) { // deselect an area from the current selection
        list($x, $y, $x2, $y2) = func_get_args();
        imagecopymerge($this->copy->img, $this->img, $x, $y, $x, $y, $x2-$x, $y2-$y, 100);
      }else{ // deselect everything, draw the perforated copy back over the original
        imagealphablending($this->img, true);
        imagecopymerge($this->img, $this->copy->img, 0, 0, 0, 0, $this->x, $this->y, 100); // copy the copy back
        $this->copy->__destruct();
        $this->selected = false;
      }
    }
    

    对于那些好奇的人,这里有两个类:

    http://dev.expocom.nl/functions.php?id=104 (image.class.php)
    http://dev.expocom.nl/functions.php?id=171 (MyImage.class.php extends image.class.php)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-01
      • 2010-12-30
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多