/** * Draws one pixel to a given image * * @param \Intervention\Image\Image $image * @return boolean */ 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()); }
/** * Draw current instance of line to given endpoint on given image * * @param Image $image * @param integer $x * @param integer $y * @return boolean */ public function applyToImage(Image $image, $x = 0, $y = 0) { $color = new Color($this->color); foreach ($image as $frame) { imageline($frame->getCore(), $x, $y, $this->x, $this->y, $color->getInt()); } return true; }
/** * Rotates image counter clockwise * * @param Intervention\Image\Image $image * @return boolean */ public function execute($image) { $angle = $this->argument(0)->type('numeric')->required()->value(); $color = $this->argument(1)->value(); $color = new Color($color); // rotate image $image->setCore(imagerotate($image->getCore(), $angle, $color->getInt())); return true; }
/** * Creates new image instance * * @param integer $width * @param integer $height * @param string $background * @return \Intervention\Image\Image */ public function newImage($width, $height, $background = null) { // create empty resource $core = imagecreatetruecolor($width, $height); $image = new \Intervention\Image\Image(new self(), $this->newContainer($core)); // set background color $background = new Color($background); imagefill($image->getCore(), 0, 0, $background->getInt()); return $image; }
/** * Draw rectangle to given image at certain position * * @param Image $image * @param integer $x * @param integer $y * @return boolean */ public function applyToImage(Image $image, $x = 0, $y = 0) { $background = new Color($this->background); imagefilledrectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $background->getInt()); if ($this->hasBorder()) { $border_color = new Color($this->border_color); imagesetthickness($image->getCore(), $this->border_width); imagerectangle($image->getCore(), $this->x1, $this->y1, $this->x2, $this->y2, $border_color->getInt()); } return true; }
/** * Draw ellipse instance on given image * * @param Image $image * @param integer $x * @param integer $y * @return boolean */ public function applyToImage(Image $image, $x = 0, $y = 0) { $background = new Color($this->background); imagefilledellipse($image->getCore(), $x, $y, $this->width, $this->height, $background->getInt()); if ($this->hasBorder()) { $border_color = new Color($this->border_color); imagesetthickness($image->getCore(), $this->border_width); imageellipse($image->getCore(), $x, $y, $this->width, $this->height, $border_color->getInt()); } return true; }
/** * Draw rectangle to given GD resource * * @param resource $resource * @param integer $x * @param integer $y * @return boolean */ private function applyToResource($resource, $x, $y) { $background = new Color($this->background); imagefilledrectangle($resource, $this->x1, $this->y1, $this->x2, $this->y2, $background->getInt()); if ($this->hasBorder()) { $border_color = new Color($this->border_color); imagesetthickness($resource, $this->border_width); imagerectangle($resource, $this->x1, $this->y1, $this->x2, $this->y2, $border_color->getInt()); } return true; }
/** * Draw polygon on given image * * @param Image $image * @param integer $x * @param integer $y * @return boolean */ public function applyToImage(Image $image, $x = 0, $y = 0) { $background = new Color($this->background); imagefilledpolygon($image->getCore(), $this->points, intval(count($this->points) / 2), $background->getInt()); if ($this->hasBorder()) { $border_color = new Color($this->border_color); imagesetthickness($image->getCore(), $this->border_width); imagepolygon($image->getCore(), $this->points, intval(count($this->points) / 2), $border_color->getInt()); } return true; }
/** * Draw polygon on given GD resource * * @param resource $resource * @param integer $x * @param interger $y * @return boolean */ private function applyToResource($resource, $x, $y) { $background = new Color($this->background); imagefilledpolygon($resource, $this->points, intval(count($this->points) / 2), $background->getInt()); if ($this->hasBorder()) { $border_color = new Color($this->border_color); imagesetthickness($resource, $this->border_width); imagepolygon($resource, $this->points, intval(count($this->points) / 2), $border_color->getInt()); } return true; }
/** * Draw ellipse on given gd resource * * @param resource $resource * @return boolean */ private function applyToResource($resource, $x, $y) { // parse background color $background = new Color($this->background); if ($this->hasBorder()) { // slightly smaller ellipse to keep 1px bordered edges clean imagefilledellipse($resource, $x, $y, $this->width - 1, $this->height - 1, $background->getInt()); $border_color = new Color($this->border_color); imagesetthickness($resource, $this->border_width); // gd's imageellipse doesn't respect imagesetthickness so i use imagearc with 359.9 degrees here imagearc($resource, $x, $y, $this->width, $this->height, 0, 359.99, $border_color->getInt()); } else { imagefilledellipse($resource, $x, $y, $this->width, $this->height, $background->getInt()); } }
/** * Read color information from a certain position * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $x = $this->argument(0)->type('digit')->required()->value(); $y = $this->argument(1)->type('digit')->required()->value(); $format = $this->argument(2)->type('string')->value('array'); // pick color $color = imagecolorat($image->getCore(), $x, $y); if (!imageistruecolor($image->getCore())) { $color = imagecolorsforindex($image->getCore(), $color); $color['alpha'] = round(1 - $color['alpha'] / 127, 2); } $color = new Color($color); // format to output $this->setOutput($color->format($format)); return true; }
/** * Draw ellipse instance on given image * * @param Image $image * @param integer $x * @param integer $y * @return boolean */ public function applyToImage(Image $image, $x = 0, $y = 0) { // parse background color $background = new Color($this->background); if ($this->hasBorder()) { // slightly smaller ellipse to keep 1px bordered edges clean imagefilledellipse($image->getCore(), $x, $y, $this->width - 1, $this->height - 1, $background->getInt()); $border_color = new Color($this->border_color); imagesetthickness($image->getCore(), $this->border_width); // gd's imageellipse doesn't respect imagesetthickness so i use imagearc with 359.9 degrees here imagearc($image->getCore(), $x, $y, $this->width, $this->height, 0, 359.99, $border_color->getInt()); } else { imagefilledellipse($image->getCore(), $x, $y, $this->width, $this->height, $background->getInt()); } return true; }
/** * Fills image with color or pattern * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $filling = $this->argument(0)->value(); $x = $this->argument(1)->type('digit')->value(); $y = $this->argument(2)->type('digit')->value(); $width = imagesx($image->getCore()); $height = imagesy($image->getCore()); try { // set image tile filling $tile = $image->getDriver()->init($filling); } catch (\Intervention\Image\Exception\NotReadableException $e) { // set solid color filling $color = new Color($filling); $filling = $color->getInt(); } foreach ($image as $frame) { if (isset($tile)) { imagesettile($frame->getCore(), $tile->getCore()); $filling = IMG_COLOR_TILED; } imagealphablending($frame->getCore(), true); if (is_int($x) && is_int($y)) { // resource should be visible through transparency $base = $image->getDriver()->newImage($width, $height)->getCore(); imagecopy($base, $frame->getCore(), 0, 0, 0, 0, $width, $height); // floodfill if exact position is defined imagefill($frame->getCore(), $x, $y, $filling); // copy filled original over base imagecopy($base, $frame->getCore(), 0, 0, 0, 0, $width, $height); // set base as new resource-core imagedestroy($frame->getCore()); $frame->setCore($base); } else { // fill whole image otherwise imagefilledrectangle($frame->getCore(), 0, 0, $width - 1, $height - 1, $filling); } } isset($tile) ? imagedestroy($tile->getCore()) : null; return true; }
public function testDiffers() { $c1 = new Color(array(0, 0, 0)); $c2 = new Color(array(0, 0, 0)); $this->assertEquals(false, $c1->differs($c2)); $c1 = new Color(array(1, 0, 0)); $c2 = new Color(array(0, 0, 0)); $this->assertEquals(true, $c1->differs($c2)); $c1 = new Color(array(1, 0, 0)); $c2 = new Color(array(0, 0, 0)); $this->assertEquals(false, $c1->differs($c2, 10)); $c1 = new Color(array(127, 127, 127)); $c2 = new Color(array(0, 0, 0)); $this->assertEquals(true, $c1->differs($c2, 49)); $c1 = new Color(array(127, 127, 127)); $c2 = new Color(array(0, 0, 0)); $this->assertEquals(false, $c1->differs($c2, 50)); }
/** * Draw current instance of line to given endpoint on given image * * @param Image $image * @param integer $x * @param integer $y * @return boolean */ public function applyToImage(Image $image, $x = 0, $y = 0) { $color = new Color($this->color); imageline($image->getCore(), $x, $y, $this->x, $this->y, $color->getInt()); return true; }
public function applyToImage(Image $image, $posx = 0, $posy = 0) { // format text $text = $this->format(); // box size $box = $this->isBoxed() ? $this->box : $this->getBoxSize($text); // create empty resource $canvas = imagecreatetruecolor($box->getWidth() + self::PADDING * 2, $box->getHeight() + self::PADDING * 2); imagealphablending($canvas, true); // set background color transparent (2147483647 (1291845632)) imagefill($canvas, 0, 0, 2147483647); // parse text color $color = new Color($this->color); $lines = $this->getLines($text); $baseline = $this->getCoreBoxSize($lines[0]); $box->align(sprintf('%s-%s', $this->align, 'top')); $ystart = 0; if ($this->isBoxed()) { switch (strtolower($this->valign)) { case 'bottom': $ystart = $box->getHeight() - $this->getBoxSize($text)->getHeight(); break; case 'center': case 'middle': $ystart = ($box->getHeight() - $this->getBoxSize($text)->getHeight()) / 2; break; } } // write line by line foreach ($lines as $count => $line) { $linesize = $this->getCoreBoxSize(trim($line)); $relative = $box->relativePosition($linesize->align($this->align)); if ($this->hasApplicableFontFile()) { // draw ttf text imagettftext($canvas, $this->getPointSize(), 0, self::PADDING + $relative->x, self::PADDING + $ystart + $baseline->getHeight() + $count * $this->lineHeight * $this->size * 1.5, $color->getInt(), $this->file, $line); } else { // draw text imagestring($canvas, $this->getInternalFont(), self::PADDING + $relative->x, self::PADDING + $ystart + $count * $this->lineHeight * $baseline->getHeight(), trim($line), $color->getInt()); } } // valign switch (strtolower($this->valign)) { case 'top': # nothing to do... break; case 'center': case 'middle': $box->pivot->moveY($box->getHeight() / 2); break; case 'bottom': $box->pivot->moveY($box->getHeight()); break; default: case 'baseline': $baseline = $this->hasApplicableFontFile() ? $baseline->getHeight() : $this->getInternalFontBaseline(); $box->pivot->moveY($baseline); break; } if ($this->isBoxed()) { $box->align('top-left'); } // rotate canvas if ($this->angle != 0) { $canvas = imagerotate($canvas, $this->angle, 2147483647); $box->rotate($this->angle); } // enable alphablending for imagecopy imagealphablending($image->getCore(), true); foreach ($image as $frame) { // insert canvas imagecopy($frame->getCore(), $canvas, $posx - $box->pivot->x - self::PADDING, $posy - $box->pivot->y - self::PADDING, 0, 0, imagesx($canvas), imagesy($canvas)); } }
/** * Draws font to given image at given position * * @param Image $image * @param integer $posx * @param integer $posy * @return void */ public function applyToImage(Image $image, $posx = 0, $posy = 0) { // parse text color $color = new Color($this->color); if ($this->hasApplicableFontFile()) { if ($this->angle != 0 || is_string($this->align) || is_string($this->valign)) { $box = $this->getBoxSize(); $align = is_null($this->align) ? 'left' : strtolower($this->align); $valign = is_null($this->valign) ? 'bottom' : strtolower($this->valign); // correction on position depending on v/h alignment switch ($align . '-' . $valign) { case 'center-top': $posx = $posx - round(($box[6] + $box[4]) / 2); $posy = $posy - round(($box[7] + $box[5]) / 2); break; case 'right-top': $posx = $posx - $box[4]; $posy = $posy - $box[5]; break; case 'left-top': $posx = $posx - $box[6]; $posy = $posy - $box[7]; break; case 'center-center': case 'center-middle': $posx = $posx - round(($box[0] + $box[4]) / 2); $posy = $posy - round(($box[1] + $box[5]) / 2); break; case 'right-center': case 'right-middle': $posx = $posx - round(($box[2] + $box[4]) / 2); $posy = $posy - round(($box[3] + $box[5]) / 2); break; case 'left-center': case 'left-middle': $posx = $posx - round(($box[0] + $box[6]) / 2); $posy = $posy - round(($box[1] + $box[7]) / 2); break; case 'center-bottom': $posx = $posx - round(($box[0] + $box[2]) / 2); $posy = $posy - round(($box[1] + $box[3]) / 2); break; case 'right-bottom': $posx = $posx - $box[2]; $posy = $posy - $box[3]; break; case 'left-bottom': $posx = $posx - $box[0]; $posy = $posy - $box[1]; break; } } // enable alphablending for imagettftext imagealphablending($image->getCore(), true); // draw ttf text imagettftext($image->getCore(), $this->getPointSize(), $this->angle, $posx, $posy, $color->getInt(), $this->file, $this->text); } else { // get box size $box = $this->getBoxSize(); $width = $box['width']; $height = $box['height']; // internal font specific position corrections if ($this->getInternalFont() == 1) { $top_correction = 1; $bottom_correction = 2; } elseif ($this->getInternalFont() == 3) { $top_correction = 2; $bottom_correction = 4; } else { $top_correction = 3; $bottom_correction = 4; } // x-position corrections for horizontal alignment switch (strtolower($this->align)) { case 'center': $posx = ceil($posx - $width / 2); break; case 'right': $posx = ceil($posx - $width) + 1; break; } // y-position corrections for vertical alignment switch (strtolower($this->valign)) { case 'center': case 'middle': $posy = ceil($posy - $height / 2); break; case 'top': $posy = ceil($posy - $top_correction); break; default: case 'bottom': $posy = round($posy - $height + $bottom_correction); break; } // draw text imagestring($image->getCore(), $this->getInternalFont(), $posx, $posy, $this->text, $color->getInt()); } }