Skip to content

Commit

Permalink
rotate, setBrush, drawLineBrush, drawArc methods implemented, few new…
Browse files Browse the repository at this point in the history
… examples
  • Loading branch information
nightflyza committed Feb 4, 2025
1 parent 357131d commit 44e400e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
16 changes: 16 additions & 0 deletions examples/drawarc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require_once 'bootstrap.php';
$pixelCraft->createImage(100, 100);

$pixelCraft->addColor('red', 255, 0, 0);
$pixelCraft->addColor('darkred', 90, 0, 0);

for ($i = 60; $i > 50; $i--) {
$pixelCraft->drawArc(50, $i, 100, 50, 75, 360, 'darkred', IMG_ARC_PIE);
}

$pixelCraft->drawArc(50, 50, 100, 50, 75, 360, 'red', IMG_ARC_PIE);

$pixelCraft->scale(2);
$pixelCraft->renderImage();
11 changes: 11 additions & 0 deletions examples/rotate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once 'bootstrap.php';


$pixelCraft->loadImage('../assets/fox.jpg');
$pixelCraft->rotate(90);

$originalFileType = $pixelCraft->getImageType();

$pixelCraft->renderImage($originalFileType);
25 changes: 25 additions & 0 deletions examples/sierpinski.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

require_once 'bootstrap.php';

$widht = 700;
$height = 200;

$pixelCraft->createImage($widht, $height);
$points = array(
array('x' => 00, 'y' => 10),
array('x' => 0, 'y' => 190),
array('x' => 800, 'y' => 190)
);

$x = $widht;
$y = $height;
for ($i = 0; $i < 100000; $i++) {
$pixelCraft->drawPixel(round($x), round($y), 'red');
$a = rand(0, 2);
$x = ($x + $points[$a]['x']) / 2;
$y = ($y + $points[$a]['y']) / 2;
}


$pixelCraft->renderImage();
68 changes: 66 additions & 2 deletions src/api.pixelcraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ class PixelCraft {
*/
protected $watermark = '';

/**
* Contains currently loaded brush image
*
* @var GDimage
*/
protected $brush = '';

/**
* Schweigen im wald
*/
Expand Down Expand Up @@ -703,7 +710,7 @@ public function drawRectangle($x1, $y1, $x2, $y2, $colorName) {
}

/**
* Draws a line
* Draws a line using some color
*
* @param int $x1
* @param int $y1
Expand All @@ -717,6 +724,20 @@ public function drawLine($x1, $y1, $x2, $y2, $colorName) {
imageline($this->image, $x1, $y1, $x2, $y2, $this->allocateColor($colorName));
}

/**
* Draws a line using preloaded brush
*
* @param int $x1
* @param int $y1
* @param int $x2
* @param int $y2
*
* @return void
*/
public function drawLineBrush($x1, $y1, $x2, $y2) {
imageline($this->image, $x1, $y1, $x2, $y2, IMG_COLOR_BRUSHED);
}

/**
* Puts preloaded watermark on base image
*
Expand Down Expand Up @@ -863,7 +884,6 @@ public function getColorMap($hex = true) {
return ($result);
}


/**
* Calculates the brightness of a pixel at the specified coordinates.
*
Expand All @@ -878,4 +898,48 @@ public function getPixelBrightness($x, $y) {
$result = $this->rgbToBrightness($pixelColor);
return ($result);
}

/**
* Rotates the image by the specified angle clockwise
*
* @param int $angle The angle in degrees to rotate the image.
*
* @return void
*/
public function rotate($angle) {
$this->image = imagerotate($this->image, 360 - $angle, 0);
}

/**
* Sets the brush for the image using the specified file path.
*
* @param string $filePath The path to the image file to be used as a brush.
*
* @return bool
*/
public function setBrush($filePath) {
$result = $this->loadImageFile($filePath, 'brush');
if ($result) {
imagesetbrush($this->image, $this->brush);
}
return ($result);
}

/**
* Draw a partial arc and fill it
*
* @param int $x The x-coordinate of the center.
* @param int $y The y-coordinate of the center.
* @param int $width The width of the arc.
* @param int $height The height of the arc.
* @param int $startAngle The start angle of the arc in degrees.
* @param int $endAngle The end angle of the arc in degrees.
* @param string $colorName The name of the color to use for the arc.
* @param int $style The style of the arc. Available: IMG_ARC_PIE, IMG_ARC_CHORD, IMG_ARC_NOFILL, IMG_ARC_EDGED
*
* @return void
*/
public function drawArc($x, $y, $width, $height, $startAngle, $endAngle, $colorName, $style = IMG_ARC_PIE) {
imagefilledarc($this->image, $x, $y, $width, $height, $startAngle, $endAngle, $this->allocateColor($colorName), $style);
}
}

0 comments on commit 44e400e

Please sign in to comment.