Feb 15

This is a rather simple way to create an image from a string. You can use this for whatever you want to, but my thought would be for posting e-mail addresses or maybe a CAPTCHA of some sort. You could do all sorts of stuff with this, like base64ing it or encrypting it with AES. All you need to do is upload this file in a new folder and name it index.php, then create an .htaccess file and you're done. You can now just link to it like this:

<img src="http://yoursite.com/folder/this is your string">

index.php:

<?php
$fontnum = 5; // 1-5
$max_str_length = 100; // maximum length of one line of text
header("Content-type: image/png"); // tell the browser this is an image
if(@$_SERVER['REQUEST_URI']){
	$str = urldecode(str_replace(dirname($_SERVER['PHP_SELF']) . "/", "", $_SERVER['REQUEST_URI'])); // get the string
}
$str = chunk_split($str, $max_str_length); // split up the text into chunks based on max length
$str = substr(str_replace("\r", "", $str),0, -1); // replace \r (fucks things up)
$lines = count(explode("\n", $str)); // find the number of lines
$height = imagefontheight($fontnum)*count(explode("\n", $str)); // determine height
$width = imagefontwidth($fontnum) * ($lines <= 1 ? strlen($str) : $max_str_length); // determine width
$i = imagecreatetruecolor($width, $height); // create the base image
$str = explode("\n", $str); // create the string
for($x=0;$x<$lines;$x++){
	imagestring($i,$fontnum,0,(imagefontheight($fontnum)*$x),$str[$x],imagecolorallocate($i, 255, 255, 255)); // add each line to the image
}
imagepng($i); // output the image
?>

.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

Example of your new script

Enjoy :)

  • Digg
  • StumbleUpon
  • del.icio.us
  • Reddit

3 Responses

  1. d0ct0r Says:

    Nice, son!

  2. G-Brain Says:

    And then you could add a logging function…

  3. admin Says:

    Of course ;)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.