Skip to content

Commit

Permalink
Move to intervention/image v3
Browse files Browse the repository at this point in the history
  • Loading branch information
miguilimzero committed Feb 2, 2024
1 parent eab5421 commit 37f1d4a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"require": {
"php": "^8.0",
"intervention/image": "^2.7"
"intervention/image": "^3.0"
}
}
45 changes: 26 additions & 19 deletions src/AntiBotLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Miguilim\AntiBotLinks;

use Intervention\Image\ImageManagerStatic as Image;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Geometry\Line;
use Intervention\Image\Typography\Font;
use Miguilim\AntiBotLinks\CacheAdapters\AbstractCacheAdapter;

class AntiBotLinks
Expand Down Expand Up @@ -114,7 +117,8 @@ protected function generateRandomImage(string $word): array
$shadowColor = $this->generateRandomColor(isShadowColor: true);

// Create blank canvas
$image = Image::canvas($width, $height);
$manager = new ImageManager(new Driver());
$image = $manager->create($width, $height);

// Add link background
if ($this->background) {
Expand All @@ -123,36 +127,39 @@ protected function generateRandomImage(string $word): array
}

// Add link text
$image->text($word, (int) ($width / 2), (int) ($height / 2) + 1, function ($font) use ($angle, $fontFile, $shadowColor): void {
$font->angle($angle);
$font->file($fontFile);
$font->size(22);
$font->align('center');
$font->valign('middle');
$font->color($shadowColor);
})->text($word, (int) ($width / 2), (int) ($height / 2), function ($font) use ($angle, $fontFile, $textColor): void {
$font->angle($angle);
$font->file($fontFile);
$font->size(22);
$font->align('center');
$font->valign('middle');
$font->color($textColor);
$image->text($word, (int) ($width / 2), (int) ($height / 2) + 1, function (Font $font) use ($angle, $fontFile, $shadowColor): void {
$font->setFilename($fontFile);
$font->setAngle($angle);
$font->setSize(22);
$font->setAlignment('center');
$font->setValignment('middle');
$font->setColor($shadowColor);
})->text($word, (int) ($width / 2), (int) ($height / 2), function (Font $font) use ($angle, $fontFile, $textColor): void {
$font->setFilename($fontFile);
$font->setAngle($angle);
$font->setSize(22);
$font->setAlignment('center');
$font->setValignment('middle');
$font->setColor($textColor);
});

// Add link noise
if ($this->noise) {
for ($i = 0; $i < round($width / random_int(16, 22) * 10); ++$i) {
$x = random_int(1, $width - 3);
$y = random_int(1, $height - 3);
$image->line($x, $y, $x + random_int(1, 2), $y + ((random_int(0, 1)) ? -1 : +1), function ($draw) use ($textColor): void {
$draw->color($textColor);

$image->drawLine(function (Line $line) use ($x, $y, $textColor) {
$line->from($x, $y);
$line->to($x + random_int(1, 2), $y + ((random_int(0, 1)) ? -1 : +1));
$line->setBackgroundColor($textColor);
});
}
}

return [
'width' => $width,
'base64' => $image->encode('data-url')->encoded
'base64' => $image->toJpeg()->toDataUri()
];
}

Expand Down

0 comments on commit 37f1d4a

Please sign in to comment.