0

嘿,我想知道我在哪里缺少文本所在的文本框的大小?

$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
|                        |
--------------------------

我在哪里错过了它定义文本框大小的地方?

4

1 回答 1

1

使用最少数量的编辑,我得到一个未定义的 $grey 变量。输出图像在这里 http://www.laprbas.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);
于 2012-12-17T19:43:50.597 回答