Imagettfbbox和imagettftext

时间:2012-12-17 19:18:57

标签: php imagettftext

嘿,我想知道我在哪里错过文本框里面文本框的大小?

$img = imagecreatefromjpeg("Desert.jpg"); //http://s7.postimage.org/ceb440itn/Desert.jpg
$width = imagesx($img);
$height = imagesy($img);

$new_width = $width;
$new_height = $height;

$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Create some colors
$black = imagecolorallocate($tmp_img, 65, 173, 196);
// Define Font
$font = 'ARIAL.TTF';
// The text to draw
$text = 'This is a test here here blah';
//font size
$size = 75;
// Compute size of text and get the x and y for centering the text into the image
$box = imagettfbbox( $size, 0, $font, $text );   
$x = 5;//($width - $box[2]) / 2 - 5;
$y = 75;//($height - $box[5]) / 2;

// Add some shadow to the text
imagettftext($tmp_img, $size, 0, $x+1, $y-1, $grey, $font, $text);

// Add the text
imagettftext($tmp_img, $size, 0, $x, $y, $black, $font, $text);

imagejpeg($tmp_img, "testing.jpg", 100);

imagedestroy($img);
imagedestroy($tmp_img);

输出图像如下所示:

--------------------------
|This is a test here here|blah
|                        |
|                        |--Image
|                      --+----Inside image
|                        |
--------------------------

请注意,在此处 * *图像本身之后,它不包含 blah 这个词。

这应该是这样的:

--------------------------
|This is a test here here|
|          blah          |
|                        |--Image
|                      --+----Inside image
|                        |
--------------------------

我在哪里定义文本框的大小?

1 个答案:

答案 0 :(得分:1)

使用最少的编辑次数,我得到一个$ grey的未定义var。输出图像在http://www.laprbass.com/RAY_junk/testing.jpg

所以我的下一个问题是,是吗?我没有Arial字体。这是你看到的吗?     

$desert = 'http://s7.postimage.org/ceb440itn/Desert.jpg';

$img = imagecreatefromjpeg($desert); //http://s7.postimage.org/ceb440itn/Desert.jpg
$width = imagesx($img);
$height = imagesy($img);

$new_width = $width;
$new_height = $height;

$tmp_img = imagecreatetruecolor($new_width, $new_height);
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Create some colors
$black = imagecolorallocate($tmp_img, 65, 173, 196);

// Define Font
$font = 'ARIAL.TTF';
$font = 'fonts/verdana.ttf';

// The text to draw
$text = 'This is a test here here blah';

//font size
$size = 22;

// Compute size of text and get the x and y for centering the text into the image
$box = imagettfbbox( $size, 0, $font, $text );   
$x = 5;//($width - $box[2]) / 2 - 5;
$y = 75;//($height - $box[5]) / 2;

// Add some shadow to the text
imagettftext($tmp_img, $size, 0, $x+1, $y-1, $grey, $font, $text);

// Add the text
imagettftext($tmp_img, $size, 0, $x, $y, $black, $font, $text);

imagejpeg($tmp_img, "RAY_junk/testing.jpg", 100);

imagedestroy($img);
imagedestroy($tmp_img);