Last active
June 3, 2026 08:59
-
-
Save atomjoy/172de9629dd5458abf7d8a77a3d2298b to your computer and use it in GitHub Desktop.
Laravel Intervention text watermark on image with custom font.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Actions\Website; | |
| use Illuminate\Support\Facades\Storage; | |
| use Intervention\Image\ImageManager; | |
| use Intervention\Image\Drivers\Gd\Driver; | |
| use Intervention\Image\Interfaces\ImageInterface; | |
| use Intervention\Image\Typography\Font; | |
| class WatermarkImage | |
| { | |
| /** | |
| * Image text watermark | |
| * | |
| * @param string $path Storage image path | |
| * @param string $text Text | |
| * @param string $image Image public path | |
| * @param string $font Font public path .ttf (required) | |
| * @return void | |
| */ | |
| function handle($path, $text = 'Welcome', $image = 'default/test/service.webp', $color = '#ff2200', $size = 50, $font = 'default/fonts/ScienceGothic.ttf', $logo = true) | |
| { | |
| $image = ImageManager::withDriver(Driver::class) | |
| ->read(public_path($image)); | |
| if ($logo) { | |
| $this->watermark($image); | |
| } | |
| $ttf = (new Font(public_path($font))) | |
| ->setSize($size) | |
| ->setColor($color) | |
| ->setAlignment('center') | |
| ->setValignment('middle'); | |
| $image->text($text, $image->width() / 2, $image->height() / 2, $ttf); | |
| Storage::put($path, $image->toWebp(100)); | |
| } | |
| /** | |
| * Add watermark logo | |
| * | |
| * @param ImageInterface $image | |
| * @param string $path Image path | |
| * @return void | |
| */ | |
| protected function watermark(ImageInterface &$image, $path = 'default/watermark.webp') | |
| { | |
| $image->place(public_path($path), 'bottom-right', 10, 10); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment