Draw polygon on given image

@param  Image   $image
@param  int     $x
@param  int     $y
@return boolean
178731
public function applyToImage(Image $image, $x = 0, $y = 0)
    {
        $polygon = new \ImagickDraw;

        // set background
        $bgcolor = new Color($this->background);
        $polygon->setFillColor($bgcolor->getPixel());

        // set border
        if ($this->hasBorder()) {
            $border_color = new Color($this->border_color);
            $polygon->setStrokeWidth($this->border_width);
            $polygon->setStrokeColor($border_color->getPixel());
        }

        $polygon->polygon($this->points);

        $image->getCore()->drawImage($polygon);

        return true;
    }
211993
public function execute($image)
    {
        $colors = [];

        $size = $image->getSize();

        for ($x=0; $x <= ($size->width-1); $x++) {
            for ($y=0; $y <= ($size->height-1); $y++) {
                $colors[] = $image->pickColor($x, $y, 'array');
            }
        }

        $this->setOutput(md5(serialize($colors)));

        return true;
    }
914111
public function execute($image)
    {
        $width = $this->argument(0)->type('digit')->required()->value();
        $height = $this->argument(1)->type('digit')->required()->value();
        $x = $this->argument(2)->type('digit')->value();
        $y = $this->argument(3)->type('digit')->value();

        if (is_null($width) || is_null($height)) {
            throw new \Intervention\Image\Exception\InvalidArgumentException(
                "Width and height of cutout needs to be defined."
            );
        }

        $cropped = new Size($width, $height);
        $position = new Point($x, $y);

        // align boxes
        if (is_null($x) && is_null($y)) {
            $position = $image->getSize()->align('center')->relativePosition($cropped->align('center'));
        }

        // crop image core
        return $this->modify($image, 0, 0, $position->x, $position->y, $cropped->width, $cropped->height, $cropped->width, $cropped->height);
    }
284169
public function run(Image $image)
    {
        $contrast = $this->getContrast();

        if ($contrast !== null) {
            $image->contrast($contrast);
        }

        return $image;
    }
499778
public function draw($X, $Y, $Width, $Height, array $Format = [])
    {
        $Values = isset($Format["Values"]) ? $Format["Values"] : VOID;
        $IndicatorSections = isset($Format["IndicatorSections"]) ? $Format["IndicatorSections"] : null;
        $ValueDisplay = isset($Format["ValueDisplay"]) ? $Format["ValueDisplay"] : INDICATOR_VALUE_BUBBLE;
        $SectionsMargin = isset($Format["SectionsMargin"]) ? $Format["SectionsMargin"] : 4;
        $DrawLeftHead = isset($Format["DrawLeftHead"]) ? $Format["DrawLeftHead"] : true;
        $DrawRightHead = isset($Format["DrawRightHead"]) ? $Format["DrawRightHead"] : true;
        $HeadSize = isset($Format["HeadSize"]) ? $Format["HeadSize"] : floor($Height / 4);
        $TextPadding = isset($Format["TextPadding"]) ? $Format["TextPadding"] : 4;
        $CaptionLayout = isset($Format["CaptionLayout"]) ? $Format["CaptionLayout"] : INDICATOR_CAPTION_EXTENDED;
        $CaptionPosition = isset($Format["CaptionPosition"]) ? $Format["CaptionPosition"] : INDICATOR_CAPTION_INSIDE;
        $CaptionColorFactor = isset($Format["CaptionColorFactor"]) ? $Format["CaptionColorFactor"] : null;
        $CaptionR = isset($Format["CaptionR"]) ? $Format["CaptionR"] : 255;
        $CaptionG = isset($Format["CaptionG"]) ? $Format["CaptionG"] : 255;
        $CaptionB = isset($Format["CaptionB"]) ? $Format["CaptionB"] : 255;
        $CaptionAlpha = isset($Format["CaptionAlpha"]) ? $Format["CaptionAlpha"] : 100;
        $SubCaptionColorFactor = isset($Format["SubCaptionColorFactor"]) ? $Format["SubCaptionColorFactor"] : null;
        $SubCaptionR = isset($Format["SubCaptionR"]) ? $Format["SubCaptionR"] : 50;
        $SubCaptionG = isset($Format["SubCaptionG"]) ? $Format["SubCaptionG"] : 50;
        $SubCaptionB = isset($Format["SubCaptionB"]) ? $Format["SubCaptionB"] : 50;
        $SubCaptionAlpha = isset($Format["SubCaptionAlpha"]) ? $Format["SubCaptionAlpha"] : 100;
        $ValueFontName = isset($Format["ValueFontName"]) ? $Format["ValueFontName"] : $this->pChartObject->FontName;
        $ValueFontSize = isset($Format["ValueFontSize"]) ? $Format["ValueFontSize"] : $this->pChartObject->FontSize;
        $CaptionFontName = isset($Format["CaptionFontName"])
            ? $Format["CaptionFontName"] : $this->pChartObject->FontName
        ;
        $CaptionFontSize = isset($Format["CaptionFontSize"])
            ? $Format["CaptionFontSize"] : $this->pChartObject->FontSize
        ;
        $Unit = isset($Format["Unit"]) ? $Format["Unit"] : "";

        /* Convert the Values to display to an array if needed */
        if (!is_array($Values)) {
            $Values = [$Values];
        }

        /* No section, let's die */
        if ($IndicatorSections == null) {
            return 0;
        }

        /* Determine indicator visual configuration */
        $OverallMin = $IndicatorSections[0]["End"];
        $OverallMax = $IndicatorSections[0]["Start"];
        foreach ($IndicatorSections as $Key => $Settings) {
            if ($Settings["End"] > $OverallMax) {
                $OverallMax = $Settings["End"];
            }
            if ($Settings["Start"] < $OverallMin) {
                $OverallMin = $Settings["Start"];
            }
        }
        $RealWidth = $Width - (count($IndicatorSections) - 1) * $SectionsMargin;
        $XScale = $RealWidth / ($OverallMax - $OverallMin);

        $X1 = $X;
        $ValuesPos = [];
        foreach ($IndicatorSections as $Key => $Settings) {
            $Color = ["R" => $Settings["R"], "G" => $Settings["G"], "B" => $Settings["B"]];
            $Caption = $Settings["Caption"];
            $SubCaption = $Settings["Start"] . " - " . $Settings["End"];

            $X2 = $X1 + ($Settings["End"] - $Settings["Start"]) * $XScale;

            if ($Key == 0 && $DrawLeftHead) {
                $Poly = [];
                $Poly[] = $X1 - 1;
                $Poly[] = $Y;
                $Poly[] = $X1 - 1;
                $Poly[] = $Y + $Height;
                $Poly[] = $X1 - 1 - $HeadSize;
                $Poly[] = $Y + ($Height / 2);
                $this->pChartObject->drawPolygon($Poly, $Color);
                $this->pChartObject->drawLine($X1 - 2, $Y, $X1 - 2 - $HeadSize, $Y + ($Height / 2), $Color);
                $this->pChartObject->drawLine(
                    $X1 - 2,
                    $Y + $Height,
                    $X1 - 2 - $HeadSize,
                    $Y + ($Height / 2),
                    $Color
                );
            }

            /* Determine the position of the breaks */
            $Break = [];
            foreach ($Values as $iKey => $Value) {
                if ($Value >= $Settings["Start"] && $Value <= $Settings["End"]) {
                    $XBreak = $X1 + ($Value - $Settings["Start"]) * $XScale;
                    $ValuesPos[$Value] = $XBreak;
                    $Break[] = floor($XBreak);
                }
            }

            if ($ValueDisplay == INDICATOR_VALUE_LABEL) {
                if (!count($Break)) {
                    $this->pChartObject->drawFilledRectangle($X1, $Y, $X2, $Y + $Height, $Color);
                } else {
                    sort($Break);
                    $Poly = [];
                    $Poly[] = $X1;
                    $Poly[] = $Y;
                    $LastPointWritten = false;
                    foreach ($Break as $iKey => $Value) {
                        if ($Value - 5 >= $X1) {
                            $Poly[] = $Value - 5;
                            $Poly[] = $Y;
                        } elseif ($X1 - ($Value - 5) > 0) {
                            $Offset = $X1 - ($Value - 5);
                            $Poly = [$X1, $Y + $Offset];
                        }

                        $Poly[] = $Value;
                        $Poly[] = $Y + 5;

                        if ($Value + 5 <= $X2) {
                            $Poly[] = $Value + 5;
                            $Poly[] = $Y;
                        } elseif (($Value + 5) > $X2) {
                            $Offset = ($Value + 5) - $X2;
                            $Poly[] = $X2;
                            $Poly[] = $Y + $Offset;
                            $LastPointWritten = true;
                        }
                    }
                    if (!$LastPointWritten) {
                        $Poly[] = $X2;
                        $Poly[] = $Y;
                    }
                    $Poly[] = $X2;
                    $Poly[] = $Y + $Height;
                    $Poly[] = $X1;
                    $Poly[] = $Y + $Height;

                    $this->pChartObject->drawPolygon($Poly, $Color);
                }
            } else {
                $this->pChartObject->drawFilledRectangle($X1, $Y, $X2, $Y + $Height, $Color);
            }

            if ($Key == count($IndicatorSections) - 1 && $DrawRightHead) {
                $Poly = [];
                $Poly[] = $X2 + 1;
                $Poly[] = $Y;
                $Poly[] = $X2 + 1;
                $Poly[] = $Y + $Height;
                $Poly[] = $X2 + 1 + $HeadSize;
                $Poly[] = $Y + ($Height / 2);
                $this->pChartObject->drawPolygon($Poly, $Color);
                $this->pChartObject->drawLine($X2 + 1, $Y, $X2 + 1 + $HeadSize, $Y + ($Height / 2), $Color);
                $this->pChartObject->drawLine(
                    $X2 + 1,
                    $Y + $Height,
                    $X2 + 1 + $HeadSize,
                    $Y + ($Height / 2),
                    $Color
                );
            }

            $YOffset = 0;
            $XOffset = 0;
            if ($CaptionPosition == INDICATOR_CAPTION_INSIDE) {
                $TxtPos = $this->pChartObject->getTextBox(
                    $X1,
                    $Y + $Height + $TextPadding,
                    $CaptionFontName,
                    $CaptionFontSize,
                    0,
                    $Caption
                );
                $YOffset = ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]) + $TextPadding;

                if ($CaptionLayout == INDICATOR_CAPTION_EXTENDED) {
                    $TxtPos = $this->pChartObject->getTextBox(
                        $X1,
                        $Y + $Height + $TextPadding,
                        $CaptionFontName,
                        $CaptionFontSize,
                        0,
                        $SubCaption
                    );
                    $YOffset = $YOffset + ($TxtPos[0]["Y"] - $TxtPos[2]["Y"]) + $TextPadding * 2;
                }
                $XOffset = $TextPadding;
            }

            if ($CaptionColorFactor == null) {
                $CaptionColor = [
                    "Align" => TEXT_ALIGN_TOPLEFT,
                    "FontName" => $CaptionFontName,
                    "FontSize" => $CaptionFontSize,
                    "R" => $CaptionR,
                    "G" => $CaptionG,
                    "B" => $CaptionB,
                    "Alpha" => $CaptionAlpha
                ];
            } else {
                $CaptionColor = [
                    "Align" => TEXT_ALIGN_TOPLEFT,
                    "FontName" => $CaptionFontName,
                    "FontSize" => $CaptionFontSize,
                    "R" => $Settings["R"] + $CaptionColorFactor,
                    "G" => $Settings["G"] + $CaptionColorFactor,
                    "B" => $Settings["B"] + $CaptionColorFactor
                ];
            }

            if ($SubCaptionColorFactor == null) {
                $SubCaptionColor = [
                    "Align" => TEXT_ALIGN_TOPLEFT,
                    "FontName" => $CaptionFontName,
                    "FontSize" => $CaptionFontSize,
                    "R" => $SubCaptionR,
                    "G" => $SubCaptionG,
                    "B" => $SubCaptionB,
                    "Alpha" => $SubCaptionAlpha
                ];
            } else {
                $SubCaptionColor = [
                    "Align" => TEXT_ALIGN_TOPLEFT,
                    "FontName" => $CaptionFontName,
                    "FontSize" => $CaptionFontSize,
                    "R" => $Settings["R"] + $SubCaptionColorFactor,
                    "G" => $Settings["G"] + $SubCaptionColorFactor,
                    "B" => $Settings["B"] + $SubCaptionColorFactor
                ];
            }

            $RestoreShadow = $this->pChartObject->Shadow;
            $this->pChartObject->Shadow = false;

            if ($CaptionLayout == INDICATOR_CAPTION_DEFAULT) {
                $this->pChartObject->drawText($X1, $Y + $Height + $TextPadding, $Caption, $CaptionColor);
            } elseif ($CaptionLayout == INDICATOR_CAPTION_EXTENDED) {
                $TxtPos = $this->pChartObject->getTextBox(
                    $X1,
                    $Y + $Height + $TextPadding,
                    $CaptionFontName,
                    $CaptionFontSize,
                    0,
                    $Caption
                );
                $CaptionHeight = $TxtPos[0]["Y"] - $TxtPos[2]["Y"];

                $this->pChartObject->drawText(
                    $X1 + $XOffset,
                    $Y + $Height - $YOffset + $TextPadding,
                    $Caption,
                    $CaptionColor
                );
                $this->pChartObject->drawText(
                    $X1 + $XOffset,
                    $Y + $Height - $YOffset + $CaptionHeight + $TextPadding * 2,
                    $SubCaption,
                    $SubCaptionColor
                );
            }

            $this->pChartObject->Shadow = $RestoreShadow;

            $X1 = $X2 + $SectionsMargin;
        }

        $RestoreShadow = $this->pChartObject->Shadow;
        $this->pChartObject->Shadow = false;

        foreach ($Values as $Key => $Value) {
            if ($Value >= $OverallMin && $Value <= $OverallMax) {
                foreach ($IndicatorSections as $Key => $Settings) {
                    if ($Value >= $Settings["Start"] && $Value <= $Settings["End"]) {
                        $X1 = $ValuesPos[$Value]; //$X + $Key*$SectionsMargin + ($Value - $OverallMin) * $XScale;

                        if ($ValueDisplay == INDICATOR_VALUE_BUBBLE) {
                            $TxtPos = $this->pChartObject->getTextBox(
                                $X1,
                                $Y,
                                $ValueFontName,
                                $ValueFontSize,
                                0,
                                $Value . $Unit
                            );
                            $Radius = floor(($TxtPos[1]["X"] - $TxtPos[0]["X"] + $TextPadding * 4) / 2);

                            $this->pChartObject->drawFilledCircle(
                                $X1,
                                $Y,
                                $Radius + 4,
                                [
                                    "R" => $Settings["R"] + 20,
                                    "G" => $Settings["G"] + 20,
                                    "B" => $Settings["B"] + 20
                                ]
                            );
                            $this->pChartObject->drawFilledCircle(
                                $X1,
                                $Y,
                                $Radius,
                                ["R" => 255, "G" => 255, "B" => 255]
                            );

                            $TextSettings = [
                                "Align" => TEXT_ALIGN_MIDDLEMIDDLE,
                                "FontName" => $ValueFontName,
                                "FontSize" => $ValueFontSize
                            ];
                            $this->pChartObject->drawText($X1 - 1, $Y - 1, $Value . $Unit, $TextSettings);
                        } elseif ($ValueDisplay == INDICATOR_VALUE_LABEL) {
                            $Caption = [
                                [
                                    "Format" => [
                                        "R" => $Settings["R"],
                                        "G" => $Settings["G"],
                                        "B" => $Settings["B"],
                                        "Alpha" => 100
                                    ],
                                    "Caption" => $Value . $Unit
                                ]
                            ];
                            $this->pChartObject->drawLabelBox(
                                floor($X1),
                                floor($Y) + 2,
                                "Value - " . $Settings["Caption"],
                                $Caption
                            );
                        }
                    }
                    $X1 = $X2 + $SectionsMargin;
                }
            }
        }
        $this->pChartObject->Shadow = $RestoreShadow;
    }
1250418
public function execute($image)
    {
        $width = $this->argument(0)->type('numeric')->required()->value();
        $height = $this->argument(1)->type('numeric')->required()->value();
        $x = $this->argument(2)->type('numeric')->required()->value();
        $y = $this->argument(3)->type('numeric')->required()->value();
        $callback = $this->argument(4)->type('closure')->value();

        $namespace = $image->getDriver()->getNamespace();
        $ellipse_classname = "\{$namespace}\Shapes\EllipseShape";

        $ellipse = new $ellipse_classname($width, $height);

        if ($callback instanceof Closure) {
            $callback($ellipse);
        }

        $ellipse->applyToImage($image, $x, $y);

        return true;
    }
183746
public function execute($image)
    {
        $width = $this->argument(0)->type('digit')->required()->value();
        $height = $this->argument(1)->type('digit')->required()->value();
        $anchor = $this->argument(2)->value('center');
        $relative = $this->argument(3)->type('boolean')->value(false);
        $bgcolor = $this->argument(4)->value();

        $original_width = $image->getWidth();
        $original_height = $image->getHeight();

        // check of only width or height is set
        $width = is_null($width) ? $original_width : intval($width);
        $height = is_null($height) ? $original_height : intval($height);

        // check on relative width/height
        if ($relative) {
            $width = $original_width + $width;
            $height = $original_height + $height;
        }

        // check for negative width/height
        $width = ($width <= 0) ? $width + $original_width : $width;
        $height = ($height <= 0) ? $height + $original_height : $height;

        // create new canvas
        $canvas = $image->getDriver()->newImage($width, $height, $bgcolor);

        // set copy position
        $canvas_size = $canvas->getSize()->align($anchor);
        $image_size = $image->getSize()->align($anchor);
        $canvas_pos = $image_size->relativePosition($canvas_size);
        $image_pos = $canvas_size->relativePosition($image_size);

        if ($width <= $original_width) {
            $dst_x = 0;
            $src_x = $canvas_pos->x;
            $src_w = $canvas_size->width;
        } else {
            $dst_x = $image_pos->x;
            $src_x = 0;
            $src_w = $original_width;
        }

        if ($height <= $original_height) {
            $dst_y = 0;
            $src_y = $canvas_pos->y;
            $src_h = $canvas_size->height;
        } else {
            $dst_y = $image_pos->y;
            $src_y = 0;
            $src_h = $original_height;
        }

        // make image area transparent to keep transparency
        // even if background-color is set
        $transparent = imagecolorallocatealpha($canvas->getCore(), 255, 255, 255, 127);
        imagealphablending($canvas->getCore(), false); // do not blend / just overwrite
        imagefilledrectangle($canvas->getCore(), $dst_x, $dst_y, $dst_x + $src_w - 1, $dst_y + $src_h - 1, $transparent);

        // copy image into new canvas
        imagecopy($canvas->getCore(), $image->getCore(), $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);

        // set new core to canvas
        $image->setCore($canvas->getCore());

        return true;
    }
1347850
public function crop($x, $y, $x2, $y2) {
		$width = $x2 - $x;
		$height = $y2 - $y;
		$image = imagecreatetruecolor($width, $height);
		imagealphablending($image, false);
		imagesavealpha($image, true);
		imagecopyresampled($image, $this->_image, 0, 0, $x, $y, $width, $height);
		imagedestroy($this->_image);
		$this->_image = $image;

		return $this;
	}
958585
public function execute($image)
    {
        $color = $this->argument(0)->required()->value();
        $color = new Color($color);
        $x = $this->argument(1)->type('digit')->required()->value();
        $y = $this->argument(2)->type('digit')->required()->value();

        return imagesetpixel($image->getCore(), $x, $y, $color->getInt());
    }
430168
private void drawPoint(BufferedImage image) {
        if (this.pointPower == -1) {
            return;
        }
        Random r = new Random();
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        int max = this.w * this.h / this.pointPower;
        for (int i = 0; i < max; i++) {
            int x = r.nextInt(w);
            int y = r.nextInt(h);
            g2.setStroke(new BasicStroke(1.5F));
            g2.setColor(this.randomColor());
            g2.drawOval(x, y, 1, 1);
        }
        g2.dispose();
    }
602966
public function drawPolygon($x, $y, $fillType = \ZendPdf\Page::SHAPE_DRAW_FILL_AND_STROKE, $fillMethod = \ZendPdf\Page::FILL_METHOD_NON_ZERO_WINDING)
    {
        foreach ($x as $index => &$value) {
            $this->convertCoordinatesFromUserSpace($value, $y[$index]);
        }
        $x = array_reverse($x, true);
        $y = array_reverse($y, true);
        return parent::drawPolygon($x, $y, $fillType, $fillMethod);
    }
1047114
public function run(Image $image)
    {
        $format = $this->getFormat($image);
        $quality = $this->getQuality();

        if (in_array($format, ['jpg', 'pjpg'], true)) {
            $image = $image->getDriver()
                           ->newImage($image->width(), $image->height(), '#fff')
                           ->insert($image, 'top-left', 0, 0);
        }

        if ($format === 'pjpg') {
            $image->interlace();
            $format = 'jpg';
        }

        return $image->encode($format, $quality);
    }
1062845
public function execute($image)
    {
        $mask_source = $this->argument(0)->value();
        $mask_w_alpha = $this->argument(1)->type('bool')->value(false);

        $image_size = $image->getSize();

        // create empty canvas
        $canvas = $image->getDriver()->newImage($image_size->width, $image_size->height, [0,0,0,0]);

        // build mask image from source
        $mask = $image->getDriver()->init($mask_source);
        $mask_size = $mask->getSize();

        // resize mask to size of current image (if necessary)
        if ($mask_size != $image_size) {
            $mask->resize($image_size->width, $image_size->height);
        }

        imagealphablending($canvas->getCore(), false);

        if ( ! $mask_w_alpha) {
            // mask from greyscale image
            imagefilter($mask->getCore(), IMG_FILTER_GRAYSCALE);
        }

        // redraw old image pixel by pixel considering alpha map
        for ($x=0; $x < $image_size->width; $x++) {
            for ($y=0; $y < $image_size->height; $y++) {

                $color = $image->pickColor($x, $y, 'array');
                $alpha = $mask->pickColor($x, $y, 'array');

                if ($mask_w_alpha) {
                    $alpha = $alpha[3]; // use alpha channel as mask
                } else {

                    if ($alpha[3] == 0) { // transparent as black
                        $alpha = 0;
                    } else {

                        // $alpha = floatval(round((($alpha[0] + $alpha[1] + $alpha[3]) / 3) / 255, 2));

                        // image is greyscale, so channel doesn't matter (use red channel)
                        $alpha = floatval(round($alpha[0] / 255, 2));
                    }

                }

                // preserve alpha of original image...
                if ($color[3] < $alpha) {
                    $alpha = $color[3];
                }

                // replace alpha value
                $color[3] = $alpha;

                // redraw pixel
                $canvas->pixel($color, $x, $y);
            }
        }


        // replace current image with masked instance
        $image->setCore($canvas->getCore());

        return true;
    }
206549
public function flatten(&$image){
        if($image->isAnimated()){
            $imagick = $image->getCore()->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
            $image = new Image(
                $imagick,
                $image->getImageFile(),
                $image->getWidth(),
                $image->getHeight(),
                $image->getType(),
                '', // blocks
                false // animated
            );
        }
        return $this;
    }
263046
public function lineTo($x,$y)
	{
		
		$imageResource = $this->_imageResource;
		
		$_x = (int)$x;
		$_y = (int)$y;
		if($this->_beganFill){
			$this->_polygonPath[] = $_x;
			$this->_polygonPath[] = $_y;
		}else{
			imageline(
				$imageResource,
				$this->_oldX,
				$this->_oldY,
				$_x,
				$_y,
				$this->_lineDmColor->imagecolorallocatealpha($imageResource)
			);
		}
		$this->_oldX = $_x;
		$this->_oldY = $_y;
		return $this;
	}
747743
public function execute($image)
    {
        $this->setOutput(new Size(
            imagesx($image->getCore()),
            imagesy($image->getCore())
        ));

        return true;
    }
173489
public function addImage($image, $x = 0, $y = 0, $opacity = 50) {
		if (!$image instanceof Image) {
			$image = new Image($image);
		}
		if ($image->getRealType() == 'png') {
			return $this->copyFrom($image, 0, 0, $x, $y);
		}
		return $this->copyAndMergeFrom($image, $x, $y, $opacity);
	}