views:

371

answers:

4

Does anyone know how to achieve this?

+1  A: 

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.

Pekka
Thanks. My solution of this problem in three steps: 1. flip (mirror) image; 2. write text; 3. flip image again;ImageMagick is very powerful tool, but can't find easier way isung it.
Anatoliy
A: 

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)
Byron Whitlock
+1  A: 

You can use some kind of font-substitution trickery like here, or you can use the PHP version of ImageMagick.

John at CashCommons
+1  A: 

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...

vicatcu
good for whole image flipping, but my goal is rotate only text :)
Anatoliy
@Anatoliy, you mean you want to identify text in an image and horizontally/vertically mirror it? Or do you want to overlay mirrirored text onto an existing image?
vicatcu
This code not working. Good code here: http://www.roscripts.com/snippets/show/55
Anatoliy
@vicatcu, yep, I need to write mirrired text onto an existing image. Thanks for your participation, already found solution: flip image area where text is placed, than write text onto this area, then flip image area back again.
Anatoliy