【发布时间】:2010-02-19 20:56:34
【问题描述】:
有谁知道如何做到这一点?
【问题讨论】:
标签: php text image-processing gd flip
有谁知道如何做到这一点?
【问题讨论】:
标签: php text image-processing gd flip
我从您的标签中假设您的意思是翻转 GD 图像。
你的意思是翻转就像旋转吗?这可以使用imagerotate 来完成:
使用给定的角度旋转图像图像。
旋转中心是图像的中心,旋转后的图像可能与原始图像有不同的尺寸。
或者您的意思是镜像图像?没有开箱即用的方法,但 this 代码 sn-p 可能会有所帮助。 (不过,它的性能不是很好,因为它逐个像素地复制。)
对于快速的高级图像编辑操作,ImageMagick 是最好的工具。如果您使用的是共享主机,则需要由您的提供商安装它才能工作。
【讨论】:
您可以使用某种字体替换技巧,例如 here,或者您可以使用 PHP 版本的 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)
【讨论】: