Does anyone know how to achieve this?
I am assuming from your tags that you mean to flip a GD image.
Do you mean flip as in rotate? That can be done using imagerotate
:
Rotates the image image using the given angle in degrees.
The center of rotation is the center of the image, and the rotated image may have different dimensions than the original image.
Or do you mean mirror an image? There's no method for that out of the box, but maybe this code snippet helps. (It not very performant, though, because it copies pixel by pixel.)
For fast advanced image editing operations, ImageMagick is the best tool around. If you are on shared hosting, it needs to be installed by your provider to work.
Maybe something as simple as this?
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)
You can use some kind of font-substitution trickery like here, or you can use the PHP version of ImageMagick.
Untested... but this seems like it ought to work, composed of other gd functions (maybe slowly):
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);
}
}
}
So you could use bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
to create an image from a text string, then run it through the appropriate flip function that I've written above...