Archiv verlassen und diese Seite im Standarddesign anzeigen : TEXT mittels GD und transparentem Hintergrund erstellen
dakingno1 24-06-2010, 18:27 Hallo,
hat jemand eine Ahnung wie die Jungs von Frisr, Friseurbesuch - Gutschein Idee (http://www.gutscheinvordrucke.de/beauty-wellness/frisoer.html?custom=14000)
das Erstellen der transparenten gifs mit Text so sauber hinbekommen haben?
Bei mir ist die Qualität ganz schlecht wenn man dem Text eine andere Farbe als schwarz gibt.
dakingno1 24-06-2010, 18:29 Hier mein code:
$req = explode('|', $_REQUEST['r']);
$text = $req[0];
header ("Content-type: image/gif");
$font = getFont($req[2]);
$font_size = $req[1];
$tmpcolor = getColor($req[3]);
$tmp_image=@imagecreatefromgif('gfx/transparent.gif');
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);
//calculate the new width / height
$tmp = imagettfbbox($font_size,0,$font,$text);
$new_width = $tmp[2]+10;
$new_height = $font_size+5;
$new_image = imagecreate($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
$black = ImageColorAllocate($new_image, 0, 0,0);
$trans = ImageColortransparent($new_image,$black);
$color = ImageColorAllocate($new_image, trim($tmpcolor[0]), trim($tmpcolor[1]),
trim($tmpcolor[2]));
imagettftext($new_image, $font_size, 0, 0, $font_size, $color, $font, $text);
//Grab new image
imagegif($new_image);
imagedestroy($new_image);
imagedestroy($tmp_image);
AmicaNoctis 24-06-2010, 19:25 Hallo,
das GIF das du am Anfang lädst hat vermutlich eine ungünstige Palette, die eher auf Graustufen optimiert ist. Du brauchst dann eine, die deine Textfarbe in verschiedenen Helligkeitsabstufungen enthält. D. h. im Endeffekt, dass du die Palette jeweils wirst generieren müssen.
Gruß,
Amica
dakingno1 25-06-2010, 10:05 Hallo,
D. h. im Endeffekt, dass du die Palette jeweils wirst generieren müssen.
Gruß,
Amica
Hallo,
mit Grafiken kenne ich mich nicht ganz so gtu aus, kannst du mir bitte genauer erläutern was du mit Palette generieren meinst? Vielen Dank
AmicaNoctis 25-06-2010, 10:14 mit Grafiken kenne ich mich nicht ganz so gtu aus
Warum willst du dann unbedingt GIF haben? Nimm PNG und gut. Bessere Qualität, volle Alpha-Transparenz sprechen doch dafür.
dakingno1 25-06-2010, 10:46 Weil das Ergebnis genau so schlecht ist. Der Radn des textes wird nicht sauber übernommen. Das sieht man wenn das gif nicht auf weißem Hintergrund legt ganz gut: http://img534.imageshack.us/img534/3774/create.png
Hier der Code:
$req = explode('|', $_REQUEST['r']);
$text = $req[0];
header ("Content-type: image/png");
$font = getFont($req[2]);
$font_size = $req[1];
$tmpcolor = getColor($req[3]);
$tmp_image=@imagecreatefrompng('gfx/png_trans.png');
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);
//calculate the new width / height
$tmp = imagettfbbox($font_size,0,$font,$text);
$new_width = $tmp[2]+10;
$new_height = $font_size+5;
$new_image = imagecreate($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
$black = ImageColorAllocate($new_image, 0, 0,0);
//$trans = ImageColortransparent($new_image,$black);
$color = ImageColorAllocate($new_image, trim($tmpcolor[0]), trim($tmpcolor[1]),
trim($tmpcolor[2]));
imagettftext($new_image, $font_size, 0, 0, $font_size, $color, $font, $text);
//Grab new image
imagepng($new_image);
imagedestroy($new_image);
imagedestroy($tmp_image);
AmicaNoctis 25-06-2010, 11:00 Du musst
imagealphablending($img, false);
imagesavealpha($img, true);
setzen, bevor du auf das Bild malst oder schreibst. Warum lädst du eigentlich immer erstmal ein Bild?
Zum Thema Bounding Box habe ich hier noch etwas, das vielleicht interessant sein könnte: Warum imagettfbbox nichts taugt (http://amicanoctis.de/stuff/imagettfbbox/)
dakingno1 25-06-2010, 11:30 Meinst du warum ich den Text als Bild ausgeben will oder warum ich als erstes ein transparentes.gif lade?
Bei erstens liegt es daran weil ich bestimmte fonts ausgeben möchte und bei zweitens, weil die Funktion zum setzen von Farbe auf Transparenz ein noch schlechteres Ergebnis zurückliefert. Zumindest habe ich bisher keine bessere Methode gefunden um Text mit transparentem Hintergrund auszugeben.
Die beiden Zeilen haben leider nichst bewirkt. :(
AmicaNoctis 25-06-2010, 11:47 Hab mich vertan, der Wert bei imagealphablending muss true sein.
AmicaNoctis 25-06-2010, 11:57 Aus meiner Wühlkiste:
<?php
function getTypographicHeights ($pFont, $pSize) {
$bBox = imagettfbbox($pSize, 0, $pFont, "x");
$xLine = $bBox[1] - $bBox[7];
$bBox = imagettfbbox($pSize, 0, $pFont, "p");
$pLine = $xLine - $bBox[1] + $bBox[7];
$bBox = imagettfbbox($pSize, 0, $pFont, "k");
$kLine = $bBox[1] - $bBox[7];
$bBox = imagettfbbox($pSize, 0, $pFont, "H");
$hLine = $bBox[1] - $bBox[7];
$bBox = imagettfbbox($pSize, 0, $pFont, "ÁÂÃÄÅĂČ");
$accLine = $bBox[1] - $bBox[7];
return array($pLine, 0, $xLine, $hLine, $kLine, $accLine);
}
function getKerningOffset ($pFont, $pSize, $pText) {
$bBox = imagettfbbox($pSize, 0, $pFont, " ");
$sWidth = $bBox[2] - $bBox[0];
$bBox = imagettfbbox($pSize, 0, $pFont, " " . $pText);
$width = $bBox[2] - $bBox[0] - $sWidth;
$bBox = imagettfbbox($pSize, 0, $pFont, $pText);
$kerning = $bBox[2] - $bBox[0] - $width;
return $kerning;
}
$text = "dein Text";
$size = 50; // font size
$offset = 10; // amount of internal padding between the text and the edges of the image
$font = "georgiaz.ttf";// path to your font file
$bBox = imagettfbbox($size, 0, $font, $text);
$lines = getTypographicHeights($font, $size);
$kerning = getKerningOffset($font, $size, $text);
$baseLine = $lines[5];
$left = $bBox[0] + $offset;
$img = imagecreatetruecolor($bBox[2] - $bBox[0] + 2*$offset, $lines[5] - $lines[0] + 2*$offset);
imagealphablending($img, true);
imagesavealpha($img, true);
$cBackGround = imagecolorallocatealpha($img, 0, 0, 0, 127); // transparent background
$cText = imagecolorallocatealpha($img, 255, 0, 0, 64); // semi-transparent text color
imagefill($img, 0, 0, $cBackGround);
imagettftext($img, $size, 0, $left + $kerning, $offset + $baseLine, $cText, $font, $text);
header("Content-Type: image/png");
imagepng($img)
?>
dakingno1 25-06-2010, 12:17 Super Klasse!!!!!
1000 Dank!!!!!
:jo:
Das funzt perfekt nach einigen kleinen Anpassungen für die größe des Bildes. :)
|
|