TheFan1968

Bild-Manipulation mit Spatie

Feb 28th, 2026
6,903
1
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | Source Code | 1 0
  1. use Spatie\Image\Image;
  2. use Spatie\Image\Enums\AlignPosition;
  3. use Spatie\Image\Enums\Unit;
  4.  
  5. Image::load(storage_path('app/public/input.jpg'))
  6.     // 1. Größe anpassen (Fit kombiniert Resize & Crop)
  7.     ->fit(800, 600)
  8.    
  9.     // 2. Text mit Hintergrund hinzufügen
  10.     // Spatie erlaubt es, Text-Eigenschaften in einer Closure zu definieren
  11.     ->text('Ihr Text hier', function($font) {
  12.         $font->file(public_path('fonts/Roboto-Bold.ttf'))
  13.              ->size(40)
  14.              ->color('ffffff')
  15.              ->width(700) // Maximale Breite des Textblocks
  16.              ->align(AlignPosition::Center)
  17.              // Hier tricksen wir: Der Hintergrund wird direkt am Text ausgerichtet
  18.              ->background('rgba(0, 0, 0, 0.6)')
  19.              ->padding(20); // Abstand zwischen Text und Hintergrundbox
  20.     })
  21.    
  22.     // Position des Textblocks auf dem Gesamtbild
  23.     ->textPosition(AlignPosition::Bottom)
  24.    
  25.     ->save(storage_path('app/public/output.jpg'));
Advertisement