Skip to content

Commit

Permalink
fixing type problems
Browse files Browse the repository at this point in the history
  • Loading branch information
adinan-cenci committed Oct 10, 2023
1 parent 126235a commit 68df2c3
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The gd library is procedural and difficult to use, so I created this library to
Our reference:

<img src="example-original.jpg" style="max-width: 100%" />
<small>Photo by [ipet photo](https://unsplash.com/@ipet_photo?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)</small>
Photo by [ipet photo](https://unsplash.com/@ipet_photo?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)



Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Todo
- Add feature of create pattern with images, to remeat it multiple times.
- Add feature to create pattern with images, to repeat it multiple times.
- Figure how to calculate how much memory is need to load image and how to avoid memory size error
- Add support to split animated gifs
- Add support to create animated gifs
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"type": "library",
"keywords": ["image", "thumbnail"],
"license": "MIT",
"version": "0.1.0",
"authors": [
{
"name": "Adinan Cenci",
Expand Down
2 changes: 1 addition & 1 deletion examples/r-scatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

$image = new File('images/original.jpeg');
$image->resize(500);
$image->scatter(50, 30);
$image->scatter(3, 5);

/*-----------------------------*/

Expand Down
2 changes: 1 addition & 1 deletion src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class File extends Image

protected $readOnly = array('src', 'width', 'height', 'mime', 'ratio', 'file');

public function __construct($file)
public function __construct(string $file)
{
$info = getimagesize($file);

Expand Down
63 changes: 40 additions & 23 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

class Image
{
protected $width = 0;
protected $height = 0;
protected int $width = 0;
protected int $height = 0;

/** @var float $ratio Quotient between the $width and $height */
protected $ratio = 0;
protected float $ratio = 0;

/** @var image resource identifier $src */
protected $src = null;
Expand All @@ -17,7 +17,7 @@ class Image

protected $readOnly = array('src', 'width', 'height', 'ratio');

public function __construct($width, $height, $src = null)
public function __construct(int $width, int $height, $src = null)
{
if (! $src) {
$src = imagecreate($width, $height);
Expand All @@ -30,7 +30,7 @@ public function __construct($width, $height, $src = null)
}

public function __get($var)
{
{
if (in_array($var, $this->readOnly)) {
return $this->{$var};
}
Expand Down Expand Up @@ -69,21 +69,23 @@ public function copy($x = 0, $y = 0, $width = null, $height = null)
* The same works if you inform the height but pass a falsy
* value to $width
*/
public function resize($width, $height = null)
public function resize(int $width, ?int $height = null)
{
if (!$width && !$height) {
trigger_error('Inform the image\s new dimensions');
}

if ($width && !$height) {
$height = $width / $this->ratio;
$height = (int) ($width / $this->ratio);
} elseif (!$width && $height) {
$width = $height * $this->ratio;
$width = (int) ($height * $this->ratio);
}

$newSrc = self::newTrueColorTransparent($width, $height);

imagecopyresampled($newSrc, $this->src, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
$newSrc = self::newTrueColorTransparent((int) $width, (int) $height);

$w = (int) $this->width;
$h = (int) $this->height;
imagecopyresampled($newSrc, $this->src, 0, 0, 0, 0, $width, $height, $w, $h);
$this->src = $newSrc;
$this->saveAlpha = true;
$this->alphaBlending = false;
Expand All @@ -103,7 +105,7 @@ public function crop($x, $y, $width, $height)
* @param Image/resource $image Accepts an Image object or an image resource identifier.
* @return bool
*/
public function paste($image, $x = 0, $y = 0, $width = null, $height = null)
public function paste($image, int $x = 0, int $y = 0, ?int $width = null, ?int $height = null)
{
if ($image instanceof Image) {
$width = $width ? $width : $image->width;
Expand All @@ -115,9 +117,14 @@ public function paste($image, $x = 0, $y = 0, $width = null, $height = null)
$height = $height ? $height : imagesy($image);
}

$width = (int) $width;
$height = (int) $height;

$this->alpha(false);

$success = imagecopyresampled($this->src, $resource, $x, $y, 0, 0, $width, $height, $image->width, $image->height);
$w = (int) $image->width;
$h = (int) $image->height;
$success = imagecopyresampled($this->src, $resource, $x, $y, 0, 0, $width, $height, $w, $h);

$this->alpha(true);

Expand Down Expand Up @@ -172,7 +179,7 @@ public function contrast($level)
* Adds or subtract rgb values from each pixel
* From -255 to 255
*/
public function colorize($r, $g, $b, $a = null)
public function colorize($r, $g, $b, $a = 0)
{
imagefilter($this->src, IMG_FILTER_COLORIZE, $r, $g, $b, $a);
}
Expand Down Expand Up @@ -232,14 +239,14 @@ public function pixelate($size, $advanced = false)
* @param identifier/string $color
*/

public function scatter($substraction, $addition, $color = null)
public function scatter($substraction, $addition, $color = [])
{
if (! defined('IMG_FILTER_SCATTER')) {
return false;
}

imagefilter($this->src, IMG_FILTER_SCATTER, $substraction, $addition, $color);
}
}

/***********************************************
*** Helpful methods
Expand All @@ -248,8 +255,8 @@ public function scatter($substraction, $addition, $color = null)
/** $paste $image at center of this one */
public function centerIt($image)
{
$x = (($this->width - $image->width) / 2);
$y = (($this->height - $image->height) / 2);
$x = (int) (($this->width - $image->width) / 2);
$y = (int) (($this->height - $image->height) / 2);
return $this->paste($image, $x, $y, $image->width, $image->height);
}

Expand All @@ -276,6 +283,11 @@ public function fillWith($image, $align = '')
$x = (($width - $this->width) / 2) * -1;
}

$x = (int) $x;
$y = (int) $y;
$width = (int) $width;
$height = (int) $height;

$this->paste($image, $x, $y, $width, $height);
return $this;
}
Expand All @@ -299,7 +311,7 @@ public function fit($image, $align = '')
$y = ($this->height - $image->height) / 2;

} else if ($this->isThinnerThan($image)) {

$width = $this->width;
$height = $this->width / $image->ratio;

Expand All @@ -314,6 +326,11 @@ public function fit($image, $align = '')
$x = ($this->width - $width) / 2;
}

$x = (int) $x;
$y = (int) $y;
$width = (int) $width;
$height = (int) $height;

$this->paste($image, $x, $y, $width, $height);
return $this;
}
Expand Down Expand Up @@ -359,7 +376,7 @@ public function string($font, $x, $y, $string, $color)
return imagestring($this->src, $font, $x, $y, $string, $color);
}

public function ttfText($fontSize, $angle, $x, $y, $color, $fontFile, $text)
public function ttfText($fontSize, int $angle, int $x, int $y, $color, $fontFile, $text)
{
$color = $this->allocateColor($color);
return imagettftext($this->src, $fontSize, $angle, $x, $y, $color, $fontFile, $text);
Expand Down Expand Up @@ -442,7 +459,7 @@ public function alpha($bool = true)
*** Text
************************************************/

public static function imageTtfBbox($fontSize, $angle = 0, $fontFile, $text)
public static function imageTtfBbox(int $fontSize, int $angle, string $fontFile, string $text)
{
$box = imagettfbbox($fontSize, $angle, $fontFile, $text);
$box['width'] = $box[4] - $box[6];
Expand Down Expand Up @@ -550,7 +567,7 @@ public function allocateColor($color)
imagecolorallocatealpha($this->src, $r, $g, $b, $a);
}

public static function newTrueColorTransparent($width, $height)
public static function newTrueColorTransparent(int $width, int $height)
{
$src = imagecreatetruecolor($width, $height);
imagesavealpha($src, true);
Expand Down Expand Up @@ -582,7 +599,7 @@ protected static function createFromType($file, $type)
);

$func = $functions[$type];
$thumb = $func($file);
$thumb = @$func($file);

if ($type == 'image/png') {
imagealphablending($thumb, false);
Expand Down
32 changes: 20 additions & 12 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Text

protected $width = null;
protected $maxWidth = null;

protected $paddingTop = 0;
protected $paddingRight = 0;
protected $paddingBottom = 0;
Expand Down Expand Up @@ -112,7 +112,7 @@ public function getImage()
switch ($this->alignment) {
case 'left':
return $this->printTextLeft();
break;
break;
case 'center':
return $this->printTextCenter();
break;
Expand All @@ -126,9 +126,9 @@ public function getImage()
}

public function sortLines()
{
{
$this->lines = explode("\n", $this->text);

if ($this->maxWidth == false and $this->width == false) {
$d = $this->textDim($this->text);
$this->imageWidth = $d['width'] + $this->paddingLeft + $this->paddingRight;
Expand All @@ -154,8 +154,8 @@ public function sortLines()

$newLine = '';

while ($w > $mxLw) {
$word = $this->removeLastWord($line);
while ($w > $mxLw) {
$word = $this->removeLastWord($line);
$newLine = $word.' '.$newLine;
$w = $this->textWidth($line);
}
Expand All @@ -179,7 +179,10 @@ public function sortLines()

protected function prepareImage()
{
$image = new Image($this->imageWidth, $this->imageHeight);
$w = (int) $this->imageWidth;
$h = (int) $this->imageHeight;

$image = new Image($w, $h);
$image->fill($this->background);

return $image;
Expand All @@ -189,7 +192,7 @@ protected function printTextLeft()
{
$image = $this->prepareImage();
$lineHeight = Helper::pointToPixel($this->lineHeight);

$x = $this->paddingLeft;
$y = $lineHeight + $this->paddingTop;

Expand Down Expand Up @@ -225,15 +228,18 @@ protected function printTextCenter()
{
$image = $this->prepareImage();
$lineHeight = Helper::pointToPixel($this->lineHeight);

$y = $lineHeight + $this->paddingTop;

foreach ($this->lines as $line) {

$w = $this->textWidth($line);
$x = ($this->imageWidth - $w) / 2;

$image->ttfText($this->fontSize, 0, $x, $y, $this->color, $this->fontFile, $line);
$x2 = (int) $x;
$y2 = (int) $y;

$image->ttfText($this->fontSize, 0, $x2, $y2, $this->color, $this->fontFile, $line);
$y += $lineHeight;
}

Expand All @@ -244,7 +250,7 @@ protected function printTextJustify()
{
$image = $this->prepareImage();
$lineHeight = Helper::pointToPixel($this->lineHeight);

$y = $lineHeight + $this->paddingTop;

foreach ($this->lines as $line) {
Expand All @@ -253,11 +259,13 @@ protected function printTextJustify()
$whiteSpace = ($this->imageWidth - $this->paddingLeft - $this->paddingRight - $this->textWidth(str_replace(' ', '', $line))) / (count($words) - 1) ;

foreach ($words as $word) {
$x = (int) $x;
$y = (int) $y;
$image->ttfText($this->fontSize, 0, $x, $y, $this->color, $this->fontFile, $word);
$x += $this->textWidth($word) + $whiteSpace;
}

$y += $lineHeight;
$y += $lineHeight;
}

return $image;
Expand Down
2 changes: 1 addition & 1 deletion src/TrueColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class TrueColor extends Image
{
public function __construct($width, $height, $src = null)
public function __construct(int $width, int $height, ?string $src = null)
{
if (! $src) {
$src = imagecreatetruecolor($width, $height);
Expand Down

0 comments on commit 68df2c3

Please sign in to comment.