Bild mit base64 und ob_start() on-the-fly erstellen

PHP  (11.04.2010 16:35) Dieses kleine Script erstellt on-the-fly ein Bild und gibt es als base64 String aus.
So ist es nicht nötig eine extra Datei für die Bilderstellung zu erstellen.

Quellcode (ausblenden | aufklappen)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
   $img = imagecreatefromgif("bild.gif");
   $font = 'font.ttf';
   $red = imagecolorallocate($img, 255, 0, 0);
   $blue = imagecolorallocate($img, 0, 0, 255);
   $orange = imagecolorallocate($img, 252, 151, 2);
 
   imagettftext($img, 70, 20, 60, 300, $red, $font, "Komischer Text 1");
   imagettftext($img, 40, 20, 160, 320, $orange, $font, "Komischer Text 2");
   imagettftext($img, 30, 0, 140, 500, $blue, $font, "Komischer Text 3");
 
   ob_start();
      imagegif($img);
      imagedestroy($img);
      $out = base64_encode(ob_get_contents());
   ob_end_clean();
 
   echo "<img src='data:image/gif;base64,".$out."' alt='Bild'>";
 
?>