note 46512 added to function.imagestring
| From: | brooks dot boyd at gmail dot com | Date: | Wed, 13 Oct 2004 19:35:07 +0000 |
| Subject: | note 46512 added to function.imagestring | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78510@lists.php.net to get a copy of this message | ||
Drawing a string as an image is a handy way to disguise an eMail address so spam sniffers can't
get it as easily. The only catch to creating a dynamic image with your eMail in it is the eMail to
be displayed must be passed via the query string to enable static HTML to use it. So, the eMail must
be encrypted slightly in order to not defeat the purpose of not typing your eMail address outright.
I wrote the following script to do so:
Save the following as email.php
<?php
if ($_GET['addr'] != "") {
$msg = $_GET['addr'];
$msg = preg_replace("/\[dot]/",".",$msg);
$msg = preg_replace("/\[at]/","@",$msg);
$final = "";
for ($i=0; $i<=strlen($msg); $i++) {
$final .= substr($msg, strlen($msg)-$i, 1);
}
$msg = $final;
$char_width = 8;
$char_height = 17;
$padding = 3;
$width = $padding*2+strlen($msg)*$char_width;
$height = +$padding*2+$char_height;
$im = imagecreatetruecolor($width,$height);
imagealphablending($im, FALSE);
imagesavealpha($im, TRUE);
$bg = imagecolorallocatealpha($im, 255, 255, 0, 100);
$text = imagecolorallocatealpha($im, 0, 0, 0, 0);
imagefilledrectangle ($im, 0, 0, $width, $height, $bg); # Make transparent
imagestring($im, 4, $padding, $padding, $msg, $text);
} else {
$im = imagecreatetruecolor(1,1);
imagealphablending($im, FALSE);
imagesavealpha($im, TRUE);
$bg = imagecolorallocatealpha($im, 255, 0, 0, 125);
imagefilledrectangle ($im, 0, 0, 1, 1, $bg); # Make transparent
}
header('Content-type: image/jpg');
imagepng($im);
imagedestroy($im);
?>
If the script is called without an eMail address, it outputs a 2x2 transparent image.
To call the script to generate the eMail "user@home.com", the HTML tag would be:
<img src="email.php?addr=moc[dot]emoh[at]resu">
To 'encrypt' the eMail address to pass to the script, write the address backwards and
replace "." with "[dot]" and "@" with "[at]". It's not
the most ironclad protection, but it thwarts most casual eMail sniffers.
----
Manual Page -- http://www.php.net/manual/en/function.imagestring.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46512
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46512&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46512&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46512&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46512&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46512&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46512&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46512&report=yes
Search -- http://master.php.net/manage/user-notes.php