note 89932 added to function.imagechar
| From: | liam dot wiltshire at lineone dot net | Date: | Sun, 29 Mar 2009 23:11:43 +0000 |
| Subject: | note 89932 added to function.imagechar | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152589@lists.php.net to get a copy of this message | ||
A quick function to automatically generate a multi line image from a string, with the image size
automatically calculated from the string itself.
<?PHP
function multilineimage($string){
// Probably not the best way of handling newlines, but bar OS9, doesn't really cause a problem
$string = str_replace("\r","",$string);
$string = explode("\n",$string);
$maxlen = 0;
foreach ($string as $str){
if (strlen($str) > $maxlen){
$maxlen = strlen($str);
}
}
// Set font size
$font_size = 4;
// Create image width dependant on width of the string
$width = imagefontwidth($font_size)*$maxlen;
// Set height to that of the font
$height = imagefontheight($font_size) * count($string);
// Create the image pallette
$img = imagecreate($width,$height);
// Grey background
$bg = imagecolorallocate($img, 205, 255, 255);
// White font color
$color = imagecolorallocate($img, 0, 0, 0);
$ypos = 0;
foreach ($string as $str){
$len = strlen($str);
for($i=0;$i<$len;$i++){
// Position of the character horizontally
$xpos = $i * imagefontwidth($font_size);
// Draw character
imagechar($img, $font_size, $xpos, $ypos, $str, $color);
// Remove character from string
$str = substr($str, 1);
}
$ypos = $ypos + imagefontheight($font_size);
}
// Return the image
header("Content-Type: image/gif");
imagegif($img);
// Remove image
imagedestroy($img);
}
multilineimage("This is an image
This is line 2\nLine 3
Line 4");
?>
----
Server IP: 92.48.74.199
Probable Submitter: 81.149.85.32
----
Manual Page -- http://www.php.net/manual/en/function.imagechar.php
Edit -- https://master.php.net/note/edit/89932
Del: integrated -- https://master.php.net/note/delete/89932/integrated
Del: useless -- https://master.php.net/note/delete/89932/useless
Del: bad code -- https://master.php.net/note/delete/89932/bad+code
Del: spam -- https://master.php.net/note/delete/89932/spam
Del: non-english -- https://master.php.net/note/delete/89932/non-english
Del: in docs -- https://master.php.net/note/delete/89932/in+docs
Del: other reasons-- https://master.php.net/note/delete/89932
Reject -- https://master.php.net/note/reject/89932
Search -- https://master.php.net/manage/user-notes.php