Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
| Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
php:gd [2015/10/07 12:59] webproducer angelegt |
php:gd [2015/12/19 00:00] (aktuell) |
||
|---|---|---|---|
| Zeile 5: | Zeile 5: | ||
| Mit der folgenden PHP-Funktion ist es möglich, einen Text zu einer bestehenden Grafik hinzuzufügen: | Mit der folgenden PHP-Funktion ist es möglich, einen Text zu einer bestehenden Grafik hinzuzufügen: | ||
| - | <code php func_createImage.php> | + | <code php func_addTextToImage.php> |
| - | function createImage( $text, $image_out ) | + | function addTextToImage( $text, $image_out ) |
| { | { | ||
| // ***** BEGIN CONFIGURATION ***** // | // ***** BEGIN CONFIGURATION ***** // | ||
| // original image | // original image | ||
| - | $image = "original.jpg"; | + | $filename = "template.gif"; |
| - | $width = 180; | + | $width = 150; |
| - | $height = 180; | + | $height = 150; |
| // text position | // text position | ||
| $left = -1; // -1 for horizontal center | $left = -1; // -1 for horizontal center | ||
| - | $top = 200; // -1 for vertical center | + | $top = 50; // -1 for vertical center |
| // text font | // text font | ||
| $ttf = 'arial.ttf'; | $ttf = 'arial.ttf'; | ||
| - | $size = 28; | + | $size = 18; |
| $angle = 0; | $angle = 0; | ||
| // text color | // text color | ||
| - | $red = 0; // from 0 (black) to 255 (white) | + | $red = 255; // from 0 (black) to 255 (white) |
| - | $green = 0; // from 0 (black) to 255 (white) | + | $green = 255; // from 0 (black) to 255 (white) |
| - | $blue = 0; // from 0 (black) to 255 (white) | + | $blue = 255; // from 0 (black) to 255 (white) |
| // ***** END CONFIGURATION ***** // | // ***** END CONFIGURATION ***** // | ||
| - | $im = imagecreatefromjpeg( $image ); // create image | + | if( file_exists( $filename)) { |
| - | $col = ImageColorAllocate( $im, $red, $green, $blue ); // create color | + | |
| - | if( $left == -1 | $top == -1 ) { // horizontal or vertical center | + | $functions = [".gif" => ["imagecreatefromgif", "imagegif"], |
| + | ".jpg" => ["imagecreatefromjpeg", "imagejpeg"], | ||
| + | ".jpeg" => ["imagecreatefromjpeg", "imagejpeg"], | ||
| + | ".png" => ["imagecreatefrompng", "imagepng"] | ||
| + | ]; | ||
| - | $box = imagettfbbox( $size, $angle, $ttf, $text ); // get box from text | + | $pos = strrpos( $filename, "." ); |
| + | $ext = strtolower( substr( $filename, $pos ) ); | ||
| + | $func = $functions[$ext][0]; // get functionname | ||
| + | $im = $func( $filename ); // function imagecreate... | ||
| - | if( $left == -1 ) { // horizontal center | + | $col = ImageColorAllocate( $im, $red, $green, $blue ); // create color |
| - | $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 | + | if( $left == -1 | $top == -1 ) { // horizontal or vertical center |
| - | $text_height = abs( $box[5] - $box[1] ); | + | |
| - | $top = round( ( $height - $text_height ) / 2 ); // new value for top | + | $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 ) + $text_height; // new value for top | ||
| + | } | ||
| } | } | ||
| - | } | ||
| - | Imagettftext( $im, $size, $angle, $left, $top, $col, $ttf, $text ); | + | Imagettftext( $im, $size, $angle, $left, $top, $col, $ttf, $text ); |
| - | imagejpeg( $im, $image_out ); | + | |
| + | $func = $functions[$ext][1]; // get functionname | ||
| + | $func( $im, $image_out ); // function imagecreate... | ||
| + | } | ||
| + | else | ||
| + | echo "file not exist!"; | ||
| } | } | ||
| </code> | </code> | ||
| Zeile 57: | Zeile 74: | ||
| <code php> | <code php> | ||
| - | createImage( "Dieser Text soll in die Grafik", "new.jpg" ); | + | addTextToImage( "PHP ist toll", "new.gif" ); |
| </code> | </code> | ||