Skip to content

Commit

Permalink
Merge pull request #1 from miguilimzero/move-to-intervention-3
Browse files Browse the repository at this point in the history
Move to intervention/image v3
  • Loading branch information
miguilimzero authored Oct 27, 2024
2 parents eab5421 + c9dd6ff commit 5f5a1e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 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"
}
}
35 changes: 22 additions & 13 deletions src/AntiBotLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

namespace Miguilim\AntiBotLinks;

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

class AntiBotLinks
Expand Down Expand Up @@ -114,25 +119,26 @@ 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) {
$image->fill($this->asset('backgrounds/bg-' . random_int(1, 3) . '-' . (($this->darkTheme) ? 'l' : 'd') . '.png'));
// ->brightness(($this->darkTheme) ? mt_rand(5, 15) : mt_rand(55, 75));
}
// if ($this->background) {
// $image->fill($this->asset('backgrounds/bg-' . random_int(1, 3) . '-' . (($this->darkTheme) ? 'l' : 'd') . '.png'));
// // ->brightness(($this->darkTheme) ? mt_rand(5, 15) : mt_rand(55, 75));
// }

// Add link text
$image->text($word, (int) ($width / 2), (int) ($height / 2) + 1, function ($font) use ($angle, $fontFile, $shadowColor): void {
$image->text($word, (int) ($width / 2), (int) ($height / 2) + 1, function (FontFactory $font) use ($angle, $fontFile, $shadowColor): void {
$font->filename($fontFile);
$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 {
})->text($word, (int) ($width / 2), (int) ($height / 2), function (FontFactory $font) use ($angle, $fontFile, $textColor): void {
$font->filename($fontFile);
$font->angle($angle);
$font->file($fontFile);
$font->size(22);
$font->align('center');
$font->valign('middle');
Expand All @@ -144,15 +150,18 @@ protected function generateRandomImage(string $word): array
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 (LineFactory $line) use ($x, $y, $textColor) {
$line->from($x, $y);
$line->to($x + random_int(1, 2), $y + ((random_int(0, 1)) ? -1 : +1));
$line->color($textColor);
});
}
}

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

Expand Down

0 comments on commit 5f5a1e7

Please sign in to comment.