有谁知道如何实现这一目标?
5 回答
您可以使用某种字体替换技巧,例如这里,或者您可以使用 PHP 版本的ImageMagick。
我从您的标签中假设您的意思是翻转 GD 图像。
你的意思是像旋转一样翻转吗?这可以使用imagerotate
:
使用以度为单位的给定角度旋转图像图像。
旋转中心是图像的中心,旋转后的图像可能与原始图像具有不同的尺寸。
或者你的意思是镜像图像?没有开箱即用的方法,但也许这个代码片段有帮助。(不过,它的性能不是很好,因为它逐个像素地复制。)
对于快速的高级图像编辑操作,ImageMagick是最好的工具。如果您在共享主机上,则需要由您的提供商安装它才能工作。
未经测试......但这似乎应该工作,由其他 gd 功能组成(也许很慢):
function flipImageHorizontal($im){
$width = imagesx($im);
$height = imagesy($im);
for($y = 0; $y < $height; $y++){ // for each column
for($x = 0; $x < ($width >> 1); $x++){ // for half the pixels in the row
// get the color on the left side
$rgb = imagecolorat($im, $x, $y);
$colors = imagecolorsforindex($im, $rgb);
$current_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
// get the color on the right side (mirror)
$rgb = imagecolorat($im, $width - $x, $y);
$colors = imagecolorsforindex($im, $rgb);
$mirror_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
// swap the colors
imagesetpixel($im, $x, $y, $mirror_color);
imagesetpixel($im, $width - $x, $y, $color);
}
}
}
function flipImageVertical($im){
$width = imagesx($im);
$height = imagesy($im);
for($x = 0; $x < $width; $x++){ // for each row
for($y = 0; $y < ($height >> 1); $y++){ // for half the pixels in the col
// get the color on the top
$rgb = imagecolorat($im, $x, $y);
$colors = imagecolorsforindex($im, $rgb);
$current_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
// get the color on the bottom (mirror)
$rgb = imagecolorat($im, $x, $height - $y);
$colors = imagecolorsforindex($im, $rgb);
$mirror_color = imagecolorallocate($im, $colors["red"], $colors["green"], $colors["blue"]);
// swap the colors
imagesetpixel($im, $x, $y, $mirror_color);
imagesetpixel($im, $x, $height - $y, $color);
}
}
}
因此,您可以使用bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
从文本字符串创建图像,然后通过我在上面编写的适当翻转函数运行它......
要在 PHP 中向现有图像添加垂直文本,请使用函数
imagettftext($im, 10, $angle, $x, $y, $black, $font, $text);
$angle = 90 时,文本将是垂直的。
例子:
http://www.php.net/manual/en/function.imagettfbbox.php#refsect1-function.imagettfbbox-returnvalues
暗示:
该示例使用 $angle = 45,因此图像上的文本是对角线
也许像这样简单?
function toVertical ($string)
{
foreach (str_split($string) as $letter)
{
$newStr.="$letter\n";
}
return $newStr;
}
function toHorizontal($string)
{
foreach(explode("\n",$string) as $letter)
{
$newStr.=$letter;
}
return $newStr;
}
$v = toVertical("This string should be printed vertically");
echo $v;
$h = toHorizontal($v);
echo $h;
---------- PHP Execute ----------
T
h
i
s
s
t
r
i
n
g
s
h
o
u
l
d
b
e
p
r
i
n
t
e
d
v
e
r
t
i
c
a
l
l
y
This string should be printed vertically
Output completed (0 sec consumed)