note 50779 added to ref.image
| From: | andreaskalschatgmxdotde at osu1 dot php dot net | Date: | Wed, 09 Mar 2005 18:24:54 +0000 |
| Subject: | note 50779 added to ref.image | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-86163@lists.php.net to get a copy of this message | ||
You get some trouble if you create an image by using the function imagettfbbox(). The x value is
correct but the y value doesn't consider that a text can have more than one line.
The second problem is to find the start position when you want to use imagettftext(), especially by
using an angle > 90°.
My solution uses some params to generate an image with any text. The image size is as small as
possible, but you can add a margin without problems:
params:
$text > the string you want to write
$format > output format "jpeg" or "png"
$quality > jpeg quality, from 1 to 100
$font > font path and file
$size > font size (px)
$angle > angle of text in degrees
$r > font color: red part
$g > font color: green part
$b > font color: blue part
$bgr > background color: red part
$bgg > bg color: green part
$bgb > bg color: blue part
$margin > a margin between text and border of pic
code:
<?
if ($text != '')
{
list(,,$w) = imagettfbbox($size, 0, $font, $text);
$lines = count(explode("\n", $text));
// get image size (not exactly)
$angle_rad = $angle / 180 * pi();
$h = 2 * $size * $lines;
$x = round($h * abs(sin($angle_rad)) + $w * abs(cos($angle_rad))) + $size;
$y = round($h * abs(cos($angle_rad)) + $w * abs(sin($angle_rad))) + $size;
// get start point for text
$run_y = - $lines * $size + 1.5 * $size;
$s_x = round($x / 2 - ($w / 2) * cos($angle_rad) + $run_y * sin($angle_rad));
$s_y = round($y / 2 + ($w / 2) * sin($angle_rad) + $run_y * cos($angle_rad));
// create image
$im = imagecreatetruecolor($x, $y);
@imageantialias($im, true);
$bg = imagecolorallocate ($im, $bgr, $bgg, $bgb);
$tx = imagecolorallocate ($im, $r, $g, $b);
#imagecolortransparent ($im, $bg);
imagefill($im, 0, 0, $bg);
// put text
imagettftext ($im, $size, $angle, $s_x, $s_y, $tx, $font, $text);
// minimize borders
// left
for ($x = 0; $x < imagesx($im); $x++)
{
for ($y = 0; $y < imagesy($im); $y++)
{
if (imagecolorat($im, $x, $y) != $bg) break(2);
}
}
$left = $x;
// right
for ($x = imagesx($im) - 1; $x >= 0; $x--)
{
for ($y = 0; $y < imagesy($im); $y++)
{
if (imagecolorat($im, $x, $y) != $bg) break(2);
}
}
$right = $x;
// top
for ($y = 0; $y < imagesy($im); $y++)
{
for ($x = 0; $x < imagesx($im); $x++)
{
if (imagecolorat($im, $x, $y) != $bg) break(2);
}
}
$top = $y;
// bottom
for ($y = imagesy($im) - 1; $y >= 0; $y--)
{
for ($x = 0; $x < imagesx($im); $x++)
{
if (imagecolorat($im, $x, $y) != $bg) break(2);
}
}
$bottom = $y;
// add margin
$cut = imagecreatetruecolor($right - $left + 1 + $margin * 2, $bottom - $top + 1 + $margin * 2);
$bg_ = imagecolorallocate ($cut, $bgr, $bgg, $bgb);
imagefill($cut, 0, 0, $bg_);
imagecopy ($cut, $im, $margin, $margin, $left, $top, $right - $left + 1, $bottom - $top + 1);
// output
if ($format == 'jpeg')
{
header("Content-type:image/jpeg");
imagejpeg($cut,false,$quality);
}
elseif ($format == 'png')
{
header("Content-type:image/png");
imagepng($cut);
}
}
?>
you can download an interface soon: http://aka-fotos.de/scripts.php
----
Manual Page -- http://www.php.net/manual/en/ref.image.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50779
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50779&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50779&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50779&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50779&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50779&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50779&report=yes
Search -- http://master.php.net/manage/user-notes.php