Page 1 of 1

HTML to PHP GD

Posted: Sat Jun 17, 2006 2:25 pm
by R4000
Hello agian,

Just a small request (well, i think its small... too hard for me, but i wouldnt think its that hard)

Would it be possible for any of you to help me create something that would parse SIMPLE HTML code like:

Code: Select all

<font color="blue"><B>bold</B>not<I>italic</I></font>
or if it would be easyer:

Code: Select all

[c=3](r)·#peter·#corcoran(r)·#i·#am·#joining·#the·#army·#(r)[/c=3]
colors = [c=COLOR_CODE][/c=COLOR_CODE]
bold = ·#
emoticon = (r)

and turn it into a simple string.

imagehtml(resource destImage, string toParse);

or something?

Posted: Sat Jun 17, 2006 2:31 pm
by tecktalkcm0391
Why don't you try reading this: viewtopic.php?t=49545

Posted: Sat Jun 17, 2006 2:38 pm
by R4000
how does that generate an image?
I dont care what format the thing accepts, i can make my own BBCode parser (piece of <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> :P)


i just need something that will accept something in either BBCode or HTML code and build an image out of it.

it doesnt even have to be bb/html code, just some sort of format that will bold/italic/color and replace (r) with a small picutre of a rainbow :P

Posted: Sat Jun 17, 2006 7:43 pm
by R4000
Dont worry bout it anymore guys, sorted it out... ill post my code soon.
thanks :)

Posted: Sat Jun 17, 2006 8:22 pm
by R4000
This script parses:

Code: Select all

[c=3](r)·#peter·#corcoran(r)·#i·#am·#joining·#the·#army·#(r)[/c=3]·0
[Music] The Barndance Boys - Yippie i oh (Spray Remix)
Online
1616760868
and turns it into a pretty image like:
Image
(not dynamic, because i havnt got a public host)

To use this yourself; You will need MSN messenger and a Web2Messenger invite.

Code: Select all

<?php
// Color support removed, because i didnt have the RGB values, and i like black more :P
// only (r) works...
$normTextFont = "E:\\Windows\\Fonts\\verdana.ttf";
$boldTextFont = "E:\\Windows\\Fonts\\verdanab.ttf";
$textSize = 10;
$content = file_get_contents("http://www.web2messenger.com/status/R4000.txt");
$content = explode("\n", $content);
$text = $content[0];
$text = str_replace("[c=","·+",$text);
$text = str_replace("[/c=","·-",$text);
$text = str_replace("]","",$text);
$text = str_replace("Â","",$text);
$psm = $content[1];
$psm = str_replace("[]","",$psm);
$psm = str_replace("[Music]"," (Music)",$psm);
$status = $content[2];
$status = substr($status, 0, strlen($status)-1);


/// end config
$img = imagecreatefrompng("bg.png");
$white = imagecolorallocate($img, 255, 255, 255);
$text = str_replace("(r)","·@r",$text);
$text = explode("·",$text);
$sections = array();
$bold = false;
$color = array(0, 0, 0);
foreach($text as $section){
	$command = $section{0};
	$sectionText = $section;
	$emote = false;
	switch($command){
		case "+":
			$n = $section{1};
			// Color support removed, because i didnt have the RGB values, and i like black more :P
			//$color = array($colors[$n][0], $colors[$n][1], $colors[$n][2]);
			$sectionText = substr($section, 2);
			break;
		case "-":
			// Color support removed, because i didnt have the RGB values, and i like black more :P
			//$color = array(0, 0, 0);
			$sectionText = substr($section, 2);
			break;
		case "@":
			$emote = $section{1};
			$sectionText = "     " . substr($section, 2);
			break;
		case "#":
			$sectionText = substr($section, 1);
			if($bold == false) { $bold = true; } else { $bold = false; }
			break;
		case "0":
			$sectionText = substr($section, 1);
			break;
	}
	if($sectionText){
		$sections[] = array('color' => $color, 'bold' => $bold, 'text' => $sectionText, 'emote' => $emote);
	}
}
$width = 60;
$height = 55;
$maxWidth = 450;
$maxWidthPSM = 60;
foreach($sections as $section){
	$bold = $section['bold'];
	$color = imagecolorallocate($img, $section['color'][0], $section['color'][1], $section['color'][2]);
	$text = $section['text'];
	$emote = $section['emote'];
	if($emote){
		$copyimg = imagecreatefromgif("emotes/" . $emote . ".gif");
		imagecopy($img, $copyimg, $width+3, $height-14, 0, 0, 19, 19);
	}
	if($bold == true) $textFont = $boldTextFont;
	if($bold == false) $textFont = $normTextFont;
	$size = imagettfbbox($textSize, 0, $textFont, $text);
	if($width + $size[2] + 2 > $maxWidth) $text = "...";
	ImageTTFText($img, $textSize, 0, $width, $height, $color, $textFont, $text);
	$width += $size[2] + 2;
	unset($color);
}
$color = imagecolorallocate($img, 0, 0, 0);
if(strlen($psm) > $maxWidthPSM-strlen($status)-3) { $psm = substr($psm, 0, $maxWidthPSM-strlen($status)-3) . "..."; }
ImageTTFText($img, $textSize-2, 0, 60, 70, $color, $normTextFont, "(" . trim($status) . ")".$psm);
ImageTTFText($img, 6, 0, 335, 105, $color, $normTextFont, "Generated by W2M and R4000");
header("Content-type: image/png");
imagepng($img);
?>