note 80461 added to function.imagestring
| From: | Tobias at osu1 dot php dot net | Date: | Thu, 17 Jan 2008 09:40:17 +0000 |
| Subject: | note 80461 added to function.imagestring | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135161@lists.php.net to get a copy of this message | ||
I've taken the code of Epidemiah and improved the code so that the function now can handle
generic position commands (as e.g. left / bottom) or int-values as left/top position of the text to
write (with positive values used as left/top position and negative from right/bottom).
So e.g. imageWriteString($img, $font, $text, $color, -10,-10); writes the text so that there is 10
px space to right and bottom border of image.
Hope this is useful. Could be optimized and checked in detail, but works fine for me.
Tobias
<?php
/**
* write text at a specified position of the image
* @source: http://de.php.net/manual/en/function.imagestring.php#80186
*
* @param &$img; reference to image
* @param $font font to use
* @param $text text to write
* @param $color color to use
* @param $position_x var if numeric: write starting at position x (if x >= 0), otherwise from
right border; possible string values: left(5px from left border), right(5px from right border),
center(default)
* @param $position_y var if numeric: write starting at position y (if y >= 0), otherwise from
bottom; possible string values: top(5px from top border), bottom(5px from bottom border),
center(default)
* @return void
*/
function imageWriteString(&$img, $font, $text, $color, $position_x = 'center',
$position_y = 'center') {
// initialize internal variables
$x = null;
$y = null;
if($font < 0 || $font > 5){ $font = 0; }
$num = array(array(4.6, 6),
array(4.6, 6),
array(5.6, 12),
array(6.5, 12),
array(7.6, 16),
array(8.5, 16));
$width = ceil(strlen($text) * $num[$font][0]);
$height = $num[$font][1] + 2;
// handle position x
if (is_string($position_x)) {
switch($position_x) {
case 'left':
$position_x = 5;
break;
case 'right':
$position_x = -5;
break;
default:
case 'center':
$x = (imagesx($img) - $width - 8) / 2;
break;
}
}
if (!isset($x)) {
if(is_numeric($position_x)) {
if ($position_x >= 0) {
$x = $position_x; // left
} else {
$x = imagesx($img) - $width + $position_x - 8; // right
}
} else {
$x = (imagesx($img) - $width - 8) / 2; // default / error value: center
}
}
// handle position y
if (is_string($position_y)) {
switch($position_y) {
case 'top':
$position_y = 5;
break;
case 'bottom':
$position_y = -5;
break;
default:
case 'center':
$y = (Imagesy($img) - ($num[$font][1] + 2))/2;
break;
}
}
if (!isset($y)) {
if (is_numeric($position_y)) {
if ($position_y >= 0) {
$y = $position_y; // top
} else {
$y = Imagesy($img) - $height + $position_y; // bottom
}
} else {
$y = (Imagesy($img) - ($num[$font][1] + 2))/2; // default / error value: center
}
}
imagestring($img, $font, $x, $y, $text, $color);
}
?>
----
Server IP: 217.160.72.57
Probable Submitter: 87.193.143.147
----
Manual Page -- http://www.php.net/manual/en/function.imagestring.php
Edit -- https://master.php.net/note/edit/80461
Del: integrated -- https://master.php.net/note/delete/80461/integrated
Del: useless -- https://master.php.net/note/delete/80461/useless
Del: bad code -- https://master.php.net/note/delete/80461/bad+code
Del: spam -- https://master.php.net/note/delete/80461/spam
Del: non-english -- https://master.php.net/note/delete/80461/non-english
Del: in docs -- https://master.php.net/note/delete/80461/in+docs
Del: other reasons-- https://master.php.net/note/delete/80461
Reject -- https://master.php.net/note/reject/80461
Search -- https://master.php.net/manage/user-notes.php