A
A
Alexander Arbuzov2012-12-14 11:00:51
PHP
Alexander Arbuzov, 2012-12-14 11:00:51

gd or imagemagick. How to cut a polygon from an image?

Is it possible to cut a polygon from an image using GD or ImageMagic to paste it into another image?
imagecopy()/imagecopyresized()/imagecopyresampled() - allow you to cut out a rectangle. And what is needed is a polygon, more precisely, a pentagon (honeycomb).

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stdit, 2012-12-14
@Stdit

Erm... a honeycomb is a hexagon. :)
You can try to do something like this:
1. Copy the rectangle that describes the polygon we need, and paste it into a temporary image.
2. We calculate the areas that need to be cut, form triangles from them.
3. Fill these triangles with a transparent color.
4. Copy the resulting hexagon (in the form of a rectangle) and paste it into another picture.

D
doranych, 2012-12-14
@doranych

I am afraid it is not.
But the task can be done in another way.
You can first reduce the picture so that the required area is the right size. Then, using false manipulations, it overlays a template PNG image with a transparent pentagonal part in the center and filled with, say, white.
And the final stage is to crop the image in such a way as to remove as much as possible the white - empty component, i.e. the rectangle described around the cell and the actual profit.

P
PaulZi, 2012-12-14
@PaulZi

For GD, you can use a pre-prepared mask in a png file. Here is the code from the class for overlaying one image on another. The blend parameter determines how it will be overlaid - the alpha channel is copied, or overlaid using it.

/*
  mask($mask)
  Описание:	Накладывает на исходное изображение, второе в центр.
  Аргументы:
    mask	- второе изображение
    align	- выравнивание маски
    blend	- режим наложения прозрачности маски
  Возвращает:	итоговое изображение
*/
public function mask($mask, $align=0, $blend=true)
{
  $out = imagecreatetruecolor($this->w, $this->h);
  imagealphablending($out, false);
  imagecopy($out, $this->img, 0, 0, 0, 0, $this->w, $this->h);
  imagealphablending($out, $blend);
    
  // выравнивание
  $posx = ($align & self::AGN_L) ? 0 : (($align & self::AGN_R) ? $this->w - $mask->w : ($this->w - $mask->w)>>1);
  $posy = ($align & self::AGN_T) ? 0 : (($align & self::AGN_B) ? $this->h - $mask->h : ($this->h - $mask->h)>>1);
  
  imagecopy($out, $mask->img, $posx, $posy, 0, 0, $mask->w, $mask->h);
  return $out;
}

D
demimurych, 2012-12-14
@demimurych

www.imagemagick.org/Usage/thumbnails/
Mask 'n' Paint Technique

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question