Question

Does anyone know how to achieve this?

Was it helpful?

Solution

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.

OTHER TIPS

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

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)

To add vertical text to an existing image in PHP, use function

imagettftext($im, 10, $angle, $x, $y, $black, $font, $text);

With $angle = 90, the text will be vertical.

Example:

http://www.php.net/manual/en/function.imagettfbbox.php#refsect1-function.imagettfbbox-returnvalues

Hint:

The example uses $angle = 45, so text is diagonal on the image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top