首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Imagettfbbox和imagettftext

Imagettfbbox和imagettftext
EN

Stack Overflow用户
提问于 2012-12-17 19:18:57
回答 1查看 2.5K关注 0票数 0

嘿,所有我想知道的地方,我错过了文本框的大小,其中的文字在里面?

代码语言:javascript
复制
$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);

输出图像如下所示:

代码语言:javascript
复制
--------------------------
|This is a test here here|blah
|                        |
|                        |--Image
|                      --+----Inside image
|                        |
--------------------------

注意,它不包含blah这个词,在这里,*在*图像本身后面。

它应该是这样的:

代码语言:javascript
复制
--------------------------
|This is a test here here|
|          blah          |
|                        |--Image
|                      --+----Inside image
|                        |
--------------------------

它定义文本框大小的位置遗漏了什么地方?

EN

回答 1

Stack Overflow用户

发布于 2012-12-17 19:43:50

使用最小数量的编辑,我为$grey得到一个未定义的变量。输出图像在这里,junk/testing.jpg

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

代码语言:javascript
复制
$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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13920663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档