3

最初の質問、優しくしてください ;-)

単純なもの (長方形、テキスト) を少し簡単にするイメージ クラスを作成しました。基本的には、PHP のイメージ関数のラッパー メソッドの束です。
私が今やろうとしているのは、ユーザーが選択を定義できるようにし、次の画像操作が選択された領域にのみ影響するようにすることです。画像を imgTwo にコピーし、そこから選択した領域を削除することでこれを行うと考えました。通常どおり元の画像に対して次の画像操作を行い、$img->deselect() が呼び出されたら、imgTwo を元にコピーします。 、コピーを破棄します。

  • これが最善の方法ですか?明らかに、選択された領域内で選択されていない領域を定義するのは難しいでしょうが、今のところそれで問題ありません:)

次に、コピーから選択範囲を消去する方法は、透明な色で長方形を描くことです。これは機能していますが、残りの部分では発生しないことを確認しながら、その色を選択する方法がわかりません画像の。このアプリケーションの入力画像はトゥルー カラーの PNG であるため、カラー インデックス付きのパレットはありません (だと思いますか?)。

  • 個々のピクセルの色を収集してから、$existing_colours 配列に表示されない色を見つけるよりも良い方法があるはずです..そうですか?
4

2 に答える 2

3

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);
于 2009-06-26T12:41:48.677 に答える
0

コードは次のようになりました。

# -- 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;
  }
}

興味のある方のために、2 つのクラスを次に示します。

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)
于 2009-06-28T10:12:19.387 に答える