How to choose a color to make the image transparent?

First question, please be careful; -)

I wrote an image class that simplifies simple things (rectangles, text), basically a bunch of wrapper methods for PHP image functions.
What I'm trying to do now is to give the user the opportunity to determine the choice, and the following image operations affect only the selected area. I decided that I would do this by copying the image to imgTwo and deleting the selected area from it, perform the following operations with the images on the original, as usual, then when $ img-> deselect () is called, copy imgTwo back to the original, and destroy the copy .

  • Is this the best way? obviously, it will be difficult to identify unselected areas within the selected area, but I can live with it for now :)

Then, when I erase the selection from the copy, I draw a rectangle in a transparent color that works, but I cannot figure out how to select this color, being sure that this does not happen in the rest of the image. The input images in this application are true color PNGs, so there is no palette with color indexes (I think?).

  • There should be a better way than collecting the colors of each individual pixel, and then finding a color that does not appear in the $ existing_colours array. Correctly?
+3
source share
2 answers

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);
+3

:

# -- 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 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)
0

Source: https://habr.com/ru/post/1711339/


All Articles