note 87759 modified in function.imagestring by danbrown

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
I created an alternative using the function imagechar to create a string of an image.  The below function below was used to create an image the same height and width of the text string.  It is used on my website to mask users email addresses.

<?PHP

// Set your string somehow
$string = '[email protected]';

// Set font size
$font_size = 4;

// Create image width dependant on width of the string
$width  = imagefontwidth($font_size)*strlen($string);
// Set height to that of the font
$height = imagefontheight($font_size);
// Create the image pallette
$img = imagecreate($width,$height);
// Grey background
$bg    = imagecolorallocate($img, 25, 25, 25);
// White font color
$color = imagecolorallocate($img, 255, 255, 255);
// Length of the string
$len = strlen($string);
// Y-coordinate of character, X changes, Y is static
$ypos = 0;
// Loop through the string
for($i=0;$i<$len;$i++){
    // Position of the character horizontally
	$xpos = $i * imagefontwidth($font_size);
    // Draw character
	imagechar($img, $font_size, $xpos, $ypos, $string, $color);
	// Remove character from string
    $string = substr($string, 1);    
    
}
// Return the image
header("Content-Type: image/gif");
imagegif($img);
// Remove image 
imagedestroy($img);

?>

--was--
I created an alternative using the function imagechar to create a string of an image.  The below function below was used to create an image the same height and width of the text string.  It is used on my website (http://www.beatspot.co.uk) to mask users email addresses.

<?PHP

// Set your string somehow
$string = '[email protected]';

// Set font size
$font_size = 4;

// Create image width dependant on width of the string
$width  = imagefontwidth($font_size)*strlen($string);
// Set height to that of the font
$height = imagefontheight($font_size);
// Create the image pallette
$img = imagecreate($width,$height);
// Grey background
$bg    = imagecolorallocate($img, 25, 25, 25);
// White font color
$color = imagecolorallocate($img, 255, 255, 255);
// Length of the string
$len = strlen($string);
// Y-coordinate of character, X changes, Y is static
$ypos = 0;
// Loop through the string
for($i=0;$i<$len;$i++){
    // Position of the character horizontally
	$xpos = $i * imagefontwidth($font_size);
    // Draw character
	imagechar($img, $font_size, $xpos, $ypos, $string, $color);
	// Remove character from string
    $string = substr($string, 1);    
    
}
// Return the image
header("Content-Type: image/gif");
imagegif($img);
// Remove image 
imagedestroy($img);

?>

http://php.net/manual/en/function.imagestring.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.