Код в итоге выглядел так:
# -- 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)