2010-12-30 10 views
1

Ich versuche, Text auf ein PNG zu schreiben, aber wenn ich es mache, legt es einen dunklen Rand darum herum, ich bin nicht sicher, warum.Anwenden von Text auf PNG-Transparenzproblem PHP

Das Originalbild:

Das verarbeitete Bild:

Code:

// Load the image 
$im = imagecreatefrompng("admin/public/images/map/order/wally.png"); 

// If there's an error, gtfo 
if(!$im) { 
    die(""); 
} 
$textColor = imagecolorallocate($im, 68, 68, 68); 

$width = imagesx($im); 
$height = imagesy($im); 

$fontSize = 5; 
$text = "AC"; 
// Calculate the left position of the text 
$leftTextPos = ($width - imagefontwidth($fontSize)*strlen($text))/2; 
// Write the string 
imagestring($im, $fontSize, $leftTextPos, $height-28, $text, $textColor); 
// Output the image 
header('Content-type: image/png'); 
imagepng($im); 
imagedestroy($im); 

Antwort

2

ich mehrmals um dieses Problem gehabt haben, mir die Antwort finden lassen ...

Ok, etwas gefunden:

imagesavealpha($im, true); 
imagealphablending($im, true); 

Schreiben Sie das vor imagepng.

+0

fantastisch, funktioniert vielen Dank: D – Titan

+0

Gern geschehen! :) – Christian

2

Ja, das Speichern mit Alpha ist wichtig, aber das Laden ist auch wichtig. Ihr PNG-Bild ist möglicherweise transparent, aber es ist auch eine gute Vorgehensweise, dies zu berücksichtigen.

Sie müssten ein echtes Farbbild erstellen, Alpha-Farbe festlegen und dann Ihr geladenes Bild mit Text darüber zeichnen. So etwas wie das:

// create true color image 
$img = imagecreatetruecolor($width, $height); 
$transparent_color = imagecolorallocatealpha($img, 255, 255, 255, 0); 

imagealphablending($img, false); 
imagefillrectangle($img, 0, 0, $width, $height, $transparent_color); 
imagealphablending($img, true); 

// draw previously loaded PNG image 
imagecopy($img, $loaded_img, 0, 0, 0, 0, $width, $height); 

// draw your text 

// save the whole thing 
imagesavealpha($img, true); 
imagepng($img, $file); 
Verwandte Themen