谷歌镜像 伍佰目录 短网址
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

PHP - Manual: imagepsbbox

来源:网络转载 浏览:31664次 时间:2023-11-28
imagepsencodefont » « imagepolygon
  • PHP 手册
  • 函数参考
  • 图像生成和处理
  • GD
  • GD 和图像处理 函数

imagepsbbox

(PHP 4, PHP 5)

imagepsbbox — 给出一个使用 PostScript Type1 字体的文本方框

说明

imagepsbbox ( string $text , resource $font , int $size ) : array imagepsbbox ( string $text , resource $font , int $size , int $space , int $tightness , float $angle ) : array

给出一个使用 PostScript Type1 字体的文本方框。

围绕文本范围的虚拟方框是用从字符度量学中的可用信息来计算的,不幸的是往往和实际上光栅生成的文本的结果有少许不同。如果角度为 0 度,(根据经验)文本在每个方向上都需要多 1 个像素。

参数

text

要写入的文本。

font_index

imagepsloadfont() 所返回的字体资源。

size

size 以像素表示。

space

可以用来改变字体中默认间距的值。此值将被加到通常的值上,可以为负值。 以字符间距单元表示,1 个单元为 1 em-square 的一千分之一。

tightness

tightness 可以控制字符之间的间距。此值将被加到通常字符宽度上,可以为负值。 以字符间距单元表示,1 个单元为 1 em-square 的一千分之一。

angle

angle 以角度表示。

返回值

本函数返回包含有下列单元的数组:

0 左下角的 X 坐标
1 左下角的 Y 坐标
2 右上角的 X 坐标
3 右上角的 Y 坐标

范例

Example #1 imagepsbbox() 用法

<?php
// 创建图像的句柄
$im = imagecreatetruecolor(200, 200);

// 分配颜色
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// 载入 PostScript 字体
$font = imagepsloadfont('font.pfm');

// 设置文本方框
$bbox = imagepsbbox('Sample text is simple', $font, 12);

// 定义 X 和 Y 
$x = ($bbox[2] / 2) - 10;
$y = ($bbox[3] / 2) - 10;

// 字体写入图像
imagepstext($im, 'Sample text is simple', $font, 12, $black, $white, $x, $y);

// 输出并释放内存
header('Content-type: image/png');

imagepng($im);
imagedestroy($im);
?>

注释

Note: 此函数仅在 PHP 编译时指定了 --with-t1lib[=DIR] 时可用。

参见

  • imagepstext() - 用 PostScript Type1 字体把文本字符串画在图像上
add a note

User Contributed Notes 4 notes

up down 0 eikie11 years ago I have a given image width and need to render a long string on that image. By using the following function I'm, able to get an array of strings which each will fit into the images width. It might need a lot of CPU time, but it's cool:

// Function to return an Array of Strings
// Arguments: String to be wrapped, maximum width in pixels of each line, font and fontsize
function imgwordwrap($s, $maxWidth, $font, $size) {
  // Make an empty ArrayList
  $a = array();
  $w = 0;    // Accumulate width of chars
  $i = 0;      // Count through chars
  $rememberSpace = 0; // Remember where the last space was
  // As long as we are not at the end of the String
  while ($i < strlen($s)) {
    // Current char
    $c = substr($s, $i, 1);
    $bb = imagepsbbox($c, $font, $size); // calculate width
    $w += abs($bb[3])-abs($bb[1]); // accumulate width
    if ($c == ' ') $rememberSpace = $i; // Are we a blank space?
    if ($w > $maxWidth) {  // Have we reached the end of a line?
      $sub = substr($s,0,$rememberSpace); // Make a substring
            // Chop off space at beginning
            if (substr($sub,0,1)==' ') $sub = substr($sub,1);
      // Add substring to the array
      $a[] = $sub;
      // Reset everything
      $s = substr($s,$rememberSpace);
      $i = 0;
      $w = 0;
    }
    else {
      $i++;  // Keep going!
    }
  }

  // Take care of the last remaining line
  trim($s);
  if (substr($s,0,1)==' ') $s = substr($s,1);
  $a[] = $s;

  return $a;
}
up down -1 honza dot bartos at gmail dot com12 years ago When using imagepsbbox, keep in mind, that meaning of y-coordinates is slightly different here. Y-coordinates returned by this function are related to the baseline of the text starting at point [0,0]. Positive values represent points ABOVE the baseline, negative values represent points BELOW the baseline. That is why the lower left y-coordinate is always smaller here than the upper right y-coordinate (these two coordinates are actualy values of metrics.descent and metrics.ascent - see T1Lib docs).

So when you want to place some text using coordinates of the top left corner (for example [100,100]), use this:

<?php

$x = 100;
$y = 100;
$text = "Dodge this";
$fontsize=18;
$font=imagepsloadfont("somefont.pfb");
list($lx,$ly,$rx,$ry) = imagepsbbox($text,$font,$fontsize);
imagepstext ($someimage, $text, $font, $fontsize, $somecolor, $somecolor, $x, $y + $ry);

?>

Hope it helps someone, I got stuck with this for a while.
up down -1 daniel at dantec dot NO_SPAM dot nl17 years ago When using imagepsbbox, you are probably trying to do something like creating a button with text, so that the button is large enough for the text...
Below is a very simple example of making a black button just big enough to display white text on it.

<?php

//if text is no variable set sample text
if (!$text)
    $text = "This is a sample text";
   
// set the font size
$fontsize=14;

// load the font to use
$font=ImagePsLoadFont("/fonts/ariam___.pfb");

//get the left lower corner and the right upper
list($lx,$ly,$rx,$ry) = imagepsbbox($text,$font,$fontsize,0,0,0);

// calculate the size of the text
$textwidth = $rx - $lx;
$textheight = $ry - $ly;

// make an image 40 pixels wider and 20 pixels higher than the text
$imh = $textheight + 20;
$imw = $textwidth + 40;
$im = imageCreate( $imw, $imh );

//define colors, first color is used as background color!
$black  = ImageColorAllocate ($im, 0, 0, 0);
$white = ImageColorAllocate ($im, 255, 255, 255);

//create the text (with the same parameters as imagepsbbox!)
ImagePSText ($im, "$text", $font, $fontsize, $white, $white, 20, 20,'','','',4);

//send the header
header("Content-type: image/jpeg");

// create the image
ImageJPEG ($im,"",100);

//destroy the image & font to free memory
Imagepsfreefont ( $font );
ImageDestroy ( $im );

?>
up down -1 eikie11 years ago in my code below, there is an error!

replace
$w += abs($bb[3])-abs($bb[1]); // accumulate width

with
$w += abs($bb[2])-abs($bb[0]); // accumulate width

also after
$bb = imagepsbbox($c, $font, $size); // calculate width

you can add this line because spaces make odd values...
if ($c == ' ' ) $bb = imagepsbbox('i', $font, $size);
add a note

官方地址:https://www.php.net/manual/en/function.imagepsbbox.php

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net