(PECL ps >= 1.1.0)
ps_translate — 设置平移
范例 #1 坐标系的平移
<?php
function rectangle($ps) {
ps_moveto($ps, 0, 0);
ps_lineto($ps, 0, 50);
ps_lineto($ps, 50, 50);
ps_lineto($ps, 50, 0);
ps_lineto($ps, 0, 0);
ps_stroke($ps);
}
$ps = ps_new();
if (!ps_open_file($ps, "translate.ps")) {
print "无法打开 PostScript 文件\n";
exit;
}
ps_set_info($ps, "Creator", "translate.php");
ps_set_info($ps, "Author", "Uwe Steinmann");
ps_set_info($ps, "Title", "平移示例");
ps_set_info($ps, "BoundingBox", "0 0 596 842");
$psfont = ps_findfont($ps, "Helvetica", "", 0);
ps_begin_page($ps, 596, 842);
ps_set_text_pos($ps, 100, 100);
ps_translate($ps, 500, 750);
rectangle($ps);
ps_translate($ps, -500, -750);
ps_setfont($ps, $psfont, 8.0);
ps_show($ps, "初始位置的文本");
ps_end_page($ps);
ps_begin_page($ps, 596, 842);
ps_set_text_pos($ps, 100, 100);
ps_save($ps);
ps_translate($ps, 500, 750);
rectangle($ps);
ps_restore($ps);
ps_setfont($ps, $psfont, 8.0);
ps_show($ps, "初始位置的文本");
ps_end_page($ps);
ps_delete($ps);
?>
上面的例子展示了两种可能的方法,可以在页面上的任何位置放置图形(在本例中只是一个矩形),而图形本身使用自己的坐标系。诀窍是在绘制矩形之前更改当前坐标系的原点。平移必须在绘制图形后撤销。
在第二页上,应用了一种略微不同且更优雅的方法。与其用 ps_translate() 的第二次调用撤销平移,不如在修改坐标系之前保存图形上下文,并在绘制矩形后恢复。