diff --git a/README.md b/README.md index ed43a20..7ed131d 100755 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The gd library is procedural and difficult to use, so I created this library to Our reference: -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) +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) diff --git a/TODO.md b/TODO.md index 3036039..9d05146 100755 --- a/TODO.md +++ b/TODO.md @@ -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 \ No newline at end of file diff --git a/composer.json b/composer.json index bcae6a3..cc5cd3b 100755 --- a/composer.json +++ b/composer.json @@ -4,7 +4,6 @@ "type": "library", "keywords": ["image", "thumbnail"], "license": "MIT", - "version": "0.1.0", "authors": [ { "name": "Adinan Cenci", diff --git a/examples/r-scatter.php b/examples/r-scatter.php index e394bd7..7819631 100755 --- a/examples/r-scatter.php +++ b/examples/r-scatter.php @@ -13,7 +13,7 @@ $image = new File('images/original.jpeg'); $image->resize(500); -$image->scatter(50, 30); +$image->scatter(3, 5); /*-----------------------------*/ diff --git a/src/File.php b/src/File.php index dee6ec3..7111f40 100755 --- a/src/File.php +++ b/src/File.php @@ -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); diff --git a/src/Image.php b/src/Image.php index ef9fc80..cc0d3aa 100755 --- a/src/Image.php +++ b/src/Image.php @@ -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; @@ -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); @@ -30,7 +30,7 @@ public function __construct($width, $height, $src = null) } public function __get($var) - { + { if (in_array($var, $this->readOnly)) { return $this->{$var}; } @@ -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; @@ -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; @@ -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); @@ -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); } @@ -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 @@ -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); } @@ -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; } @@ -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; @@ -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; } @@ -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); @@ -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]; @@ -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); @@ -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); diff --git a/src/Text.php b/src/Text.php index a67f793..5390fae 100755 --- a/src/Text.php +++ b/src/Text.php @@ -15,7 +15,7 @@ class Text protected $width = null; protected $maxWidth = null; - + protected $paddingTop = 0; protected $paddingRight = 0; protected $paddingBottom = 0; @@ -112,7 +112,7 @@ public function getImage() switch ($this->alignment) { case 'left': return $this->printTextLeft(); - break; + break; case 'center': return $this->printTextCenter(); break; @@ -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; @@ -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); } @@ -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; @@ -189,7 +192,7 @@ protected function printTextLeft() { $image = $this->prepareImage(); $lineHeight = Helper::pointToPixel($this->lineHeight); - + $x = $this->paddingLeft; $y = $lineHeight + $this->paddingTop; @@ -225,7 +228,7 @@ protected function printTextCenter() { $image = $this->prepareImage(); $lineHeight = Helper::pointToPixel($this->lineHeight); - + $y = $lineHeight + $this->paddingTop; foreach ($this->lines as $line) { @@ -233,7 +236,10 @@ protected function printTextCenter() $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; } @@ -244,7 +250,7 @@ protected function printTextJustify() { $image = $this->prepareImage(); $lineHeight = Helper::pointToPixel($this->lineHeight); - + $y = $lineHeight + $this->paddingTop; foreach ($this->lines as $line) { @@ -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; diff --git a/src/TrueColor.php b/src/TrueColor.php index 798c0fd..73d45c3 100755 --- a/src/TrueColor.php +++ b/src/TrueColor.php @@ -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);