2012-11-07 2 views
11

Ich habe Bild erstellen Code in image_creator.So fügen Sie Text zu einem Bild mit PHP GD-Bibliothek

<?php 
header("Content-Type: image/jpeg"); 
$im = ImageCreateFromGif("photo.gif"); 
$black = ImageColorAllocate($im, 255, 255, 255); 
$start_x = 10; 
$start_y = 20; 
Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'verdana.ttf', "text to write"); 
Imagejpeg($im, '', 100); 
ImageDestroy($im); 
?> 

Die Datei für die Bildausgabe ist image.php und hat unter Code

<html> 
<head> 
</head> 
<body> 
    <img src="http://localhost/image_creator.php"/> 
</body> 

</html> 

Als ich image.php laufen, bekomme ich nur eine leere Seite. Wieso ist es so?

+2

Zunächst einmal $ black = ImageColorAllocate ($ im, 255, 255, 255); '- das ist weiß, nicht schwarz. Haben Sie Fehler? Überprüft das Fehlerprotokoll? – iMoses

+0

@iMoses Ich denke das '$ black' ist nur, weil er Code von php.net kopiert hat: http://php.net/manual/en/function.imagefttext.php – George

+0

Macht es nicht richtig oder macht Sinn. Ich bin sicher, sie haben es richtig gemacht. Es ist kein Problem, nur verwirrend. Das "leere" Bild könnte nur weiß auf weißem Hintergrund sein. – iMoses

Antwort

31

Verwenden Sie diesen Text zum Bild hinzufügen (kopiert von PHP for Kids)

<?php 
     //Set the Content Type 
     header('Content-type: image/jpeg'); 

     // Create Image From Existing File 
     $jpg_image = imagecreatefromjpeg('sunset.jpg'); 

     // Allocate A Color For The Text 
     $white = imagecolorallocate($jpg_image, 255, 255, 255); 

     // Set Path to Font File 
     $font_path = 'font.TTF'; 

     // Set Text to Be Printed On Image 
     $text = "This is a sunset!"; 

     // Print Text On Image 
     imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text); 

     // Send Image to Browser 
     imagejpeg($jpg_image); 

     // Clear Memory 
     imagedestroy($jpg_image); 
    ?> 
+11

Sie haben es einfach von http://www.phpforkids.com/php/php-gd-library-adding-text-writing.php eingefügt versuchen Sie, um das Problem stattdessen zu zeigen –

+2

Bitte zitieren Sie auch Ihre Quellen ... sonst ist es nur Plagiat. Ich habe das Zitat hinzugefügt. –

+0

Wenn ich mehrere Zeilen Text auf demselben Bild hinzufügen möchte, bevor ich es an den Browser sende, müssten mehrere 'imagettftext()' Funktionen hinzugefügt werden? Wie kann ich dieses Bild in einem aktuellen Verzeichnis auf meinem Server speichern, damit der Benutzer es jederzeit ausdrucken kann? – codeninja

0

Problem hier ist, $black = ImageColorAllocate($im, 255, 255, 255); // < == dies nicht schwarz, seine weißen // für schwarz es sein sollte,

$black = ImageColorAllocate($im, 0, 0, 0); 
+1

Es ist nur ein Name von variabler Typ, es ist kein Problem (duh) – stormrage

0

Problem hier ist

$black = ImageColorAllocate($im, 255, 255, 255); 

das ist nicht schwarz, es ist weiß. Für Schwarz sollte es so sein,

$black = ImageColorAllocate($im, 0, 0, 0); 
Verwandte Themen