Benutzer-Werkzeuge

Webseiten-Werkzeuge


php:gd

Dies ist eine alte Version des Dokuments!


GD Libary - Programmbibliothek für die Bearbeitung von Grafiken

Mit der GD Libary ist es möglich, Grafiken in PHP zu erzeugen oder zu ändern. Voraussetzung ist, dass die Programm-Bibliothek auf dem Server installiert ist.

Mit der folgenden PHP-Funktion ist es möglich, einen Text zu einer bestehenden Grafik hinzuzufügen:

func_createImage.php
function createImage( $text, $image_out )
{
    // ***** BEGIN CONFIGURATION ***** //
 
    // original image
    $image = "original.jpg";
    $width = 180;
    $height = 180;
 
    // text position
    $left   = -1;   // -1 for horizontal center
    $top    = 200;  // -1 for vertical center
 
    // text font
    $ttf    = 'arial.ttf';
    $size   = 28;
    $angle  = 0;
 
    // text color
    $red    = 0;    // from 0 (black) to 255 (white)
    $green  = 0;    // from 0 (black) to 255 (white)
    $blue   = 0;    // from 0 (black) to 255 (white)
 
    // ***** END CONFIGURATION ***** //
 
    $im = imagecreatefromjpeg( $image ); // create image
    $col = ImageColorAllocate( $im, $red, $green, $blue ); // create color
 
    if( $left == -1 | $top == -1 ) { // horizontal or vertical center
 
        $box = imagettfbbox( $size, $angle, $ttf, $text ); // get box from text
 
        if( $left == -1 ) { // horizontal center
            $text_width = abs( $box[4] - $box[0] ); // top-right - bottom-left
            $left = round( ( $width - $text_width ) / 2 ); // new value for left
        }
 
        if( $top == -1 ) { // vertical center
            $text_height = abs( $box[5] - $box[1] );
            $top = round( ( $height - $text_height ) / 2 ); // new value for top
        }
    }
 
    Imagettftext( $im, $size, $angle, $left, $top, $col, $ttf, $text );
    imagejpeg( $im, $image_out );
}

Aufruf-Beispiel:

createImage( "Dieser Text soll in die Grafik", "new.jpg" );
php/gd.1444215595.txt.gz · Zuletzt geändert: 2015/12/19 00:00 (Externe Bearbeitung)