【发布时间】:2010-10-20 18:13:56
【问题描述】:
如何在所有浏览器中显示垂直文本(旋转 90 度)?
(来源:sun.com)
【问题讨论】:
-
说这话很愚蠢;它是一种编程语言,并且和大多数语言一样,可以访问图像修改功能......
标签: browser cross-browser text-rotation
如何在所有浏览器中显示垂直文本(旋转 90 度)?
(来源:sun.com)
【问题讨论】:
标签: browser cross-browser text-rotation
该问题与服务器端语言无关。如果垂直渲染的文本不再是文本而是图像时没有问题,请选择solution provided by tharkun。否则,在表示层有办法做到这一点。
首先,(目前)有一个仅限 IE 的解决方案,即part of the CSS3 standard。你可以check it live。
p {
writing-mode: tb-rl;
}
CSS3 text module 还指定了一些properties for text orientation。
其他人也这样做with SVG。
【讨论】:
我认为您不能使用 PHP/HTML/CSS 旋转文本。但是您可以使用包含垂直文本的GD 创建图像。
例子:
header ("Content-type: image/png");
// imagecreate (x width, y width)
$img_handle = @imagecreatetruecolor (15, 220) or die ("Cannot Create image");
// ImageColorAllocate (image, red, green, blue)
$back_color = ImageColorAllocate ($img_handle, 0, 0, 0);
$txt_color = ImageColorAllocate ($img_handle, 255, 255, 255);
ImageStringUp ($img_handle, 2, 1, 215, $_GET['text'], $txt_color);
ImagePng ($img_handle);
ImageDestroy($img_handle);
【讨论】:
function verticletext($string)
{
$tlen = strlen($string);
for($i=0;$i<$tlen;$i++)
{
$vtext .= substr($string,$i,1)."<br />";
}
return $vtext;
}
你去没有回声
【讨论】:
其他浏览器也可以旋转文本。
CSS:
/*Safari*/
-webkit-transform: rotate(-90deg);
/*Firefox*/
-moz-transform: rotate(-90deg);
/*Opera*/
-o-transform: rotate(-90deg);
/*IE*/
writing-mode: tb-rl;
filter: flipV flipH;
【讨论】:
如果表头行太长,我使用下面的函数。它非常有用,因为它易于使用、速度快,而且您不必计算文本的高度和宽度。那些 css 噱头是行不通的。
#######################################################
# convert text to image and embed it to html
# uses /tmp as a cache to make it faster
# usage: print imagetext("Hello my friend");
# Created by Ville Jungman, GPL-licenced, donated by www.varuste.net
function imagetext($text,$size = 10,$color = array(253,128,46)){
$dir = "/tmp/tekstit";
$filename = "$dir/" . base64_encode($text);
if(!file_exists($filename)){
$font = "/usr/share/fonts/truetype/freefont/FreeSans.ttf";
$box = imagettfbbox($size,90,$font,$text);
$w = -$box[4] - 1;
$h = -$box[3];
$im = imagecreatetruecolor($w,$h);
$white = imagecolorallocate($im,$color[1],$color[2],$color[3]);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
imagecolortransparent($im,$white);
imagefilledrectangle($im, 0, 0, $size, 99, $white);
imagettftext($im,$size,90,$size,$h,$black,$font,$text);
@mkdir($dir);
imagepng($im,$filename);
imagedestroy($im);
}
$data = base64_encode(file_get_contents($filename));
return "<img src='data:image/png;base64,$data'>";
}
【讨论】:
This thread 建议您可以将文本写入图像,然后旋转图像。
在 IE 上似乎可行,但在其他浏览器上则不行,因此它可能是 IE6 的小胜利之一 =)
【讨论】:
imagettftext 应该做到这一点。
【讨论】:
据我所知,无法使用 CSS 获取垂直文本,因此这意味着旋转后的文本必须在图像中。使用 PHP 的 libgd 接口生成输出图像文件非常简单。
但是请注意,这意味着使用一个脚本来生成图像,而另一个脚本来生成周围的网页。您通常不能(内联数据:尽管有 URI)让一个脚本生成多个页面组件。
【讨论】:
使用 raphaeljs 它也适用于 IE 6
【讨论】:
function verticletext($string)
{
$tlen = strlen($string);
for($i=0;$i<$tlen;$i++)
{
echo substr($string,$i,1)."<br />";
}
}
【讨论】: