I have this text simulator that allows the visitor to choose their specific desired font and the number of lines they desire. This functionality would be similar to previewing on ThingsRemembered.com so you can see what the item looks like before you purchase it. It works well, however certain special characters do not work in the simulator. These are as follows:
# - Does not appear
& - truncates this and anything after it
+ - Does not appear
\ - Does not appear
' - Erases entire line
I presume I need to escape these characters and replace with their HTML friendly equivalents; does anyone have an example of how this is done?
<?php
//creates a image handle
$image = imagecreate( 700, 70 );
if(!empty($_GET["bgcolor"])){
$background = imagecolorallocate( $image,0, 0, 0);
}
else {
$background = imagecolorallocate( $image,255, 255, 255);
}
//GET COLORS FROM POST AND SPLIT INTO RGB FORMAT
$color = $_GET["color"];
$pieces = explode("-", $color);
//COLORS
$color = imagecolorallocate($image, hexdec("0x".$pieces[0].""), hexdec("0x".$pieces[1].""), hexdec("0x".$pieces[2].""));
$font = 'fonts/'.$_GET["font"].'';
$fontSize = "25";
$fontRotation = "0";
$str = utf8_encode_mix($_GET["name"]);
// char code replacements
$tb = imagettfbbox(25, 0, $font, $str);
$x = ceil((700 - $tb[2]) / 2);
ImageTTFText($image, $fontSize, $fontRotation, $x, 50, $color, $font, $str);
header("Content-Type: image/PNG");
ImagePng ($image);
imagedestroy($image);
function utf8_encode_mix($input, $encode_keys=false)
{
if(is_array($input))
{
$result = array();
foreach($input as $k => $v)
{
$key = ($encode_keys)? utf8_encode($k) : $k;
$result[$key] = utf8_encode_mix( $v, $encode_keys);
}
}
else
{
$result = utf8_encode($input);
}
return $result;
}
?>
The user's input is parsed by passing the text from the calling PHP page into this PHP code. The input string is passed to this routine as a query string variable; this was not my design, rather something I inherited.
Special thanks to user @dorad for helping me get this working correctly. This is the code that actually works. I suspect the original version I had was coded for an earlier version of PHP which will not work correctly for special characters in newer PHP versions. So without further ado, this is the final code that works:
Calling page index2.php
You can use the following PHP to convert:
The
ord
function converts a character into an ASCII code (numeric). You can find a list of the characters and their numeric values in ASCII here: http://www.asciitable.com/A function to convert all your special chars:
You should also have a look on
htmlentities
:Use
#
where you want a #&
where you want a &+
where you want a +\
where you want a \