誰かがこれを達成する方法を知っていますか?
5 に答える
私はあなたのタグからあなたがGD画像を反転させることを意味していると仮定しています。
回転のように反転するという意味ですか?それは以下を使用して行うことができますimagerotate
:
指定された角度(度単位)を使用して画像画像を回転します。
回転の中心は画像の中心であり、回転した画像は元の画像とは異なる寸法になる場合があります。
それとも、画像をミラーリングするという意味ですか?すぐに使用できる方法はありませんが、このコードスニペットが役立つかもしれません。(ただし、ピクセルごとにコピーするため、パフォーマンスはあまり高くありません。)
高速で高度な画像編集操作を行うには、ImageMagickが最適なツールです。共有ホスティングを使用している場合、機能するにはプロバイダーがインストールする必要があります。
ここのようなフォント置換のトリックを使用することも、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)