• PHP + Images
    7 replies, posted
I got this code: [php] <?php $im = imagecreatefrompng("rank.png"); if(!$im) { die("Image template could not be load!"); } if(empty($_GET['text'])) { echo "<html><head><title>DRPG Rank Generator v0.3</title></head>"; echo "<form method=\"get\" action=\"rank.php\">"; echo "Your text:<input type=\"text\" size=\"16\" maxlength=\"16\" name=\"text\"><br>"; echo "<input type=\"submit\"><br>"; echo "</form>"; echo "</html>"; exit; } if(strlen($_GET['text']) > 16) { die("Incorrect text length! Text is limited to 16 chars."); } $color = imagecolorallocate($im, 0, 0, 0); $width = imagesx($im); $height = imagesy($im); $text = $_GET['text']; $font = 5; $horizontalTextPos = $width/5; $verticalTextPos = $height/5; imagestring($im,$font,$horizontalTextPos,$verticalTextPos,$text,$color); Header("Content-type: image/png"); imagepng($im); imagedestroy($im); ?> [/php] [img]http://drpg.netne.net/rank.png[/img] The template [img]http://drpg.netne.net/rank.php?text=Hai+FP[/img] My problem is, the template is transparent in some parts, after writing the text it fucks up. Also note they greyiness around the red logo. How to fix this? Second question is: how do i change the font? [editline]17th January 2011[/editline] ... The php file just went offline because it's getting reviewed by admins. [editline]17th January 2011[/editline] But for some reason the png is still showing, on the same page.
Site is back up
I am thinking it has something to do with imagecreatefrompng which makes the png's background color black. I am not really sure though.
[URL=http://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using-phps-gdlib-imagecopyresampled/32302#32302]This may help you.[/URL]
[QUOTE=deadeye536;27479370][URL=http://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using-phps-gdlib-imagecopyresampled/32302#32302]This may help you.[/URL][/QUOTE] [img]http://drpg.netne.net/rank.php?text=Yes+this+should[/img] [img]http://drpg.netne.net/rank.php?text=fix+the+lack+of[/img] [img]http://drpg.netne.net/rank.php?text=transparency[/img]
Okay thanks, and how would i set a ttf to be used as font?
[QUOTE=Goz3rr;27481056]Okay thanks, and how would i set a ttf to be used as font?[/QUOTE] [url]http://php.net/manual/en/function.imagettftext.php[/url]
I tried to use that but ended up having no image being outputted at all. [editline]17th January 2011[/editline] Does the ttf have to be in the same folder as the png? [editline]17th January 2011[/editline] Just FYI, The current code with tranparency fixed is: [php] <?php $im = imagecreatefrompng("rank.png"); imagealphablending($im, false); imagesavealpha($im, true); if(!$im) { die("Image template could not be load! (0x01)"); } if(empty($_GET['text'])) { echo "<html><head><title>DRPG Rank Generator v0.3</title></head>"; echo "<form method=\"get\" action=\"rank.php\">"; echo "Your text:<input type=\"text\" size=\"16\" maxlength=\"16\" name=\"text\"><br>"; echo "<input type=\"submit\"><br>"; echo "</form>"; echo "</html>"; exit; } if(strlen($_GET['text']) > 16) { die("Incorrect text length! (0x02)"); } $color = imagecolorallocate($im, 0, 0, 0); $width = imagesx($im); $height = imagesy($im); $text = $_GET['text']; $font = 15; $fontfile = "./font.otf"; $horizontalTextPos = $width/5; $verticalTextPos = $height/5; //imagestring($im,$font,$horizontalTextPos,$verticalTextPos,$text,$color); imagettftext ($im,$font,0,$horizontalTextPos,$verticalTextPos,$color,$fontfile,$text); Header("Content-type: image/png"); imagepng($im); imagedestroy($im); ?> [/php] [editline]17th January 2011[/editline] Also the font is a OTF instead of a TTF :| Can i still use the same function? [del]But now the image breaks[/del] Forgot a semicolon :P
Sorry, you need to Log In to post a reply to this thread.