Skip to content

Commit

Permalink
support php8.0 and following
Browse files Browse the repository at this point in the history
  • Loading branch information
HimmelKreis4865 committed Jan 25, 2023
1 parent add5ac5 commit 7ae0af7
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 220 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"license": "GPL-3.0",
"require": {
"php": "^7.1"
"php": ">=8.0",
},
"autoload": {
"psr-4": {
Expand Down
56 changes: 18 additions & 38 deletions image/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace image;

use GdImage;
use image\color\Color;
use image\font\BaseFont;
use image\font\ExtendedFont;
Expand Down Expand Up @@ -49,37 +50,17 @@ final class Image {
/** @var string[] an array with all currently supported extensions */
public const SUPPORTED_EXTENSIONS = ["png", "jpg", "jpeg"];

/** @var resource $image */
public $image;

/**
* @var int $width manual width,
* will be removed in future and replaced with @see imagesx
*/
protected $width;

/**
* @var int $height manual height,
* will be removed in future and replaced with @see imagesy()
*/
protected $height;

/**
* Image constructor.
*
* @param resource $image has a type of resource,
* @param GdImage $image has a type of resource,
* to create an image @see Image::make()
* or import it from a file @see Image::fromFile()
*
* @param int $width width for the image, will be removed in future
* @param int $height height of the image, will be removed in future
*/
public function __construct($image, int $width = 500, int $height = 500) {
if (!is_resource($image)) throw new InvalidArgumentException("Given image is not a valid resource!");
$this->image = $image;
$this->width = $width;
$this->height = $height;
}
public function __construct(public GdImage $image, private int $width = 500, private int $height = 500) { }

/**
* Tries to create an image from a path (already existing file)
Expand Down Expand Up @@ -120,9 +101,9 @@ public static function make(int $width, int $height): self {
*
* @param string $path
*
* @return false|\GdImage|resource|null
* @return GdImage|null|false
*/
protected static function createImage(string $path) {
protected static function createImage(string $path): GdImage|null|false {
$ext = Utils::getFileExtension($path);

switch ($ext) {
Expand Down Expand Up @@ -151,7 +132,7 @@ protected static function createImage(string $path) {
* @param Vector2 $position
* @param Color $color Can be a gradient
*/
public function drawTriangle(int $size, Vector2 $position, Color $color) {
public function drawTriangle(int $size, Vector2 $position, Color $color): void {
for ($i = $position->getY(); $i < ($size + $position->getY()); $i++) {
imageline($this->image, $i, $i, ($size + $position->getX()), $i, $color->getColor($this->image, $i, $size));
}
Expand All @@ -164,7 +145,7 @@ public function drawTriangle(int $size, Vector2 $position, Color $color) {
*
* @param Color $color Can be a gradient
*/
public function fill(Color $color) {
public function fill(Color $color): void {
if ($color->isSingleColored()) {
imagefill($this->image, 0, 0, $color->getColor($this->image, 0, 0));
return;
Expand All @@ -182,9 +163,9 @@ public function fill(Color $color) {
* @param int $degrees
* @param bool $change if true, the image will automatically change its rotation too
*
* @return false|\GdImage|resource
* @return false|GdImage|resource
*/
public function rotate(int $degrees, bool $change = true) {
public function rotate(int $degrees, bool $change = true): false|GdImage {
if ($degrees < 0 or $degrees > 360) throw new OutOfRangeException("You can only rotate between 0-360 degrees! $degrees is out of range!");
$image = imagerotate($this->image, floatval($degrees), 0);
if ($change) $this->image = $image;
Expand All @@ -198,7 +179,7 @@ public function rotate(int $degrees, bool $change = true) {
*
* @param BaseShape $baseShape
*/
public function drawShape(BaseShape $baseShape) {
public function drawShape(BaseShape $baseShape): void {
$baseShape->draw($this->image);
}

Expand All @@ -207,7 +188,7 @@ public function drawShape(BaseShape $baseShape) {
*
* @api
*/
public function display() {
public function display(): void {
header('Content-Type: image/png');
imagepng($this->image);
imagedestroy($this->image);
Expand All @@ -220,7 +201,7 @@ public function display() {
*
* @param string $path
*/
public function save(string $path) {
public function save(string $path): void {
if (file_exists($path) or is_dir($path)) throw new InvalidArgumentException("Cannot overwrite an existing element for save progress!");

switch (Utils::getFileExtension($path)) {
Expand All @@ -241,7 +222,7 @@ public function save(string $path) {
*
* @param string|Font $text
*/
public function addBaseText($text) {
public function addBaseText(Font|string $text): void {
if (is_string($text)) $text = new BaseFont($text);
if (!$text instanceof Font) throw new InvalidArgumentException("You can either pass a Font instance or a string to add!");
imagestring($this->image, $text->getFontSize(), $text->getPadding()->getX(), $text->getPadding()->getY(), $text->getText(), $text->getColor()->toImageInt($this->image));
Expand All @@ -254,7 +235,7 @@ public function addBaseText($text) {
*
* @param ExtendedFont $font
*/
public function addExtendedText(ExtendedFont $font) {
public function addExtendedText(ExtendedFont $font): void {
imagettftext($this->image, $font->getFontSize(), $font->getAngle(), $font->getPadding()->getX(), $font->getPadding()->getY(), $font->getColor()->toImageInt($this->image), $font->getFontPath(), $font->getText());
}

Expand All @@ -269,13 +250,12 @@ public function addExtendedText(ExtendedFont $font) {
* For a great experience and nice looking pictures, use @see Image::toSquare() before
* @param int $opacity
*/
public function addImage(Image $image, Vector2 $padding, bool $rounded = false, int $opacity = 100) {
public function addImage(Image $image, Vector2 $padding, bool $rounded = false, int $opacity = 100): void {
$image = $image->image;
$width = imagesx($image);
$height = imagesy($image);

if ($rounded) {

$mask = imagecreatetruecolor($width, $height);
$maskTransparent = imagecolorallocate($mask, 255, 0, 255);
imagecolortransparent($mask, $maskTransparent);
Expand All @@ -302,7 +282,7 @@ public function addImage(Image $image, Vector2 $padding, bool $rounded = false,
* @param int $width
* @param int $height
*/
public function resizeTo(int $width, int $height) {
public function resizeTo(int $width, int $height): void {
$new = imagecreatetruecolor($width, $height);
imagecopyresampled($new, $this->image, 0, 0, 0, 0, $width, $height, imagesx($this->image), imagesy($this->image));
$this->image = $new;
Expand All @@ -317,7 +297,7 @@ public function resizeTo(int $width, int $height) {
*
* @param int $type
*/
public function toSquare(int $type = SquareConverter::TYPE_CENTER) {
public function toSquare(int $type = SquareConverter::TYPE_CENTER): void {
switch ($type) {
case SquareConverter::TYPE_CENTER:
$this->image = SquareConverter::converterToCenter($this->image);
Expand All @@ -339,7 +319,7 @@ public function toSquare(int $type = SquareConverter::TYPE_CENTER) {
* @param int $flipMode for valid modes,
* checkout @see FlipTypes
*/
public function flip(int $flipMode) {
public function flip(int $flipMode): void {
if (in_array($flipMode, [FlipTypes::TYPE_HORIZONTAL, FlipTypes::TYPE_VERTICAL, FlipTypes::TYPE_BOTH]))
imageflip($this->image, $flipMode);
}
Expand Down
5 changes: 3 additions & 2 deletions image/color/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace image\color;

use GdImage;
use function ctype_xdigit;
use function hexdec;
use function is_int;
Expand All @@ -17,13 +18,13 @@ abstract public function isSingleColored(): bool;
*
* @internal
*
* @param $image
* @param GdImage $image
* @param int $i important for multicolored classes such as Gradient
* @param int $size important for multicolored classes such as Gradient
*
* @return false|int
*/
abstract public function getColor($image, int $i, int $size);
abstract public function getColor(GdImage $image, int $i, int $size): false|int;

/**
* Returns a color number by a hex, might be used in future versions
Expand Down
19 changes: 8 additions & 11 deletions image/color/Gradient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

namespace image\color;

use GdImage;
use image\color\components\RGB;
use function floor;
use function imagecolorallocate;

class Gradient extends Color {
/** @var RGB $begin */
protected $begin;

/** @var RGB $end */
protected $end;

public function __construct(RGB $begin, RGB $end) {
$this->begin = $begin;
$this->end = $end;
}
/**
* @param RGB $begin
* @param RGB $end
*/
public function __construct(private RGB $begin, private RGB $end) { }

/**
* Returns whether its a single color or consists of more than one color
Expand All @@ -34,15 +31,15 @@ public function isSingleColored(): bool {
*
* @internal
*
* @param $image
* @param GdImage $image
* @param int $i important to decide which color is actually needed
* @param int $size important to decide which color is actually needed
*
* @warning Might throw an exception because R/G/B values can become above 255 in some use cases
*
* @return false|int
*/
public function getColor($image, int $i, int $size) {
public function getColor(GdImage $image, int $i, int $size): false|int {
return imagecolorallocate($image, (floor(($i * ($this->end->getRed() - $this->begin->getRed()) / $size)) + $this->begin->getRed()), (floor(($i * ($this->end->getGreen() - $this->begin->getGreen()) / $size)) + $this->begin->getGreen()), (floor(($i * ($this->end->getBlue() - $this->begin->getBlue()) / $size)) + $this->begin->getBlue()));
}
}
11 changes: 4 additions & 7 deletions image/color/SingleColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

namespace image\color;

use GdImage;
use image\color\components\RGB;
use function imagecolorallocate;

class SingleColor extends Color {
/** @var RGB $color */
protected $color;

/**
* SingleColor constructor.
*
* @param RGB $color
*/
public function __construct(RGB $color) {
$this->color = $color;
}
public function __construct(private RGB $color) { }

/**
* Returns whether its a single color or consists of more than one color
Expand All @@ -34,13 +31,13 @@ public function isSingleColored(): bool {
*
* @internal
*
* @param $image
* @param GdImage $image
* @param int $i not important here, but still needed
* @param int $size not important here, but still needed
*
* @return false|int
*/
public function getColor($image, int $i, int $size) {
public function getColor(GdImage $image, int $i, int $size): false|int {
return imagecolorallocate($image, $this->color->getRed(), $this->color->getGreen(), $this->color->getBlue());
}
}
2 changes: 1 addition & 1 deletion image/color/components/ColorComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface ColorComponent {
*
* @return bool
*/
public static function isValid($data): bool;
public static function isValid(mixed $data): bool;
}
16 changes: 3 additions & 13 deletions image/color/components/RGB.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,8 @@ class RGB implements ColorComponent {

/** @var int max value for a RGB tag */
public const MAX_VAL = 255;


/** @var int $red */
private $red;

/** @var int $green */
private $green;

/** @var int $blue */
private $blue;

private int $red;

/**
* RGB constructor.
Expand All @@ -29,16 +21,14 @@ class RGB implements ColorComponent {
* @param int $green
* @param int $blue
*/
public function __construct($red, int $green = 0, int $blue = 0) {
public function __construct($red, private int $green = 0, private int $blue = 0) {
if (is_array($red)) {
$rgb = self::fromArray($red);
if ($rgb === null) throw new \InvalidArgumentException("Could not parse rgb due to invalid input array!");
$this->parseRGB($rgb);
return;
}
$this->red = $red;
$this->green = $green;
$this->blue = $blue;
}


Expand Down
18 changes: 1 addition & 17 deletions image/font/BaseFont.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@
use position\Vector2;

class BaseFont implements Font {
/** @var string $text */
protected $text;

/** @var int $fontSize */
protected $fontSize;

/** @var Vector2|null $padding */
protected $padding;

/** @var RGB|null $color */
protected $color;

/**
* BaseFont constructor.
Expand All @@ -26,12 +15,7 @@ class BaseFont implements Font {
* @param Vector2|null $padding
* @param RGB|null $color
*/
public function __construct(string $text, int $fontSize = 3, ?Vector2 $padding = null, RGB $color = null) {
$this->text = $text;
$this->fontSize = $fontSize;
$this->padding = $padding;
$this->color = $color;
}
public function __construct(private string $text, private int $fontSize = 3, private ?Vector2 $padding = null, private ?RGB $color = null) { }

/**
* Returns the content of the text
Expand Down
Loading

0 comments on commit 7ae0af7

Please sign in to comment.