How to flip image horizontaly and vertically with php

Go To StackoverFlow.com

3

I search the web for this and i can't find what i need.

I have an image (in or outside server) and i need to flip the image horizontaly or vertically with php, and show it like this:

<?
$img = $_GET['img'];
header('Content-type: image/png');
/*
do the flip work
*/
imagepng($img, NULL);
imagedestroy($tmp_img);
?>

How can i do it? Thank you all.

2012-04-03 21:32
by Pedro Soares
Anyone remeber the Upsidedownternet? :- - ghoti 2012-04-03 21:34
You didn't search very hard: https://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=php+gd+flip#sclient=psy-ab&hl=en&q=php%20gd%20flip%20vertically&oq=php%20gd%20flip%20vert&aq=1w&aqi=q-w2&aql=1&gsl=serp.11.1.33i21l2.6417l7753l0l9632l7l7l0l0l0l0l94l391l7l8l0.efis.1.&pbx=1&bav=on.2,or.rgc.rpw.rcp.,cf.osb&fp=8a49b837bfe4a3e5&biw=1680&bih=935&pf=p&pdl=30 - BenOfTheNorth 2012-04-03 21:38
Well i did search and i found something like this link but ´imagesx()´and/or imagesy()´were not working, but i fixed it with ´list($w, $h, $type, $attr) = getimagesize($image);´ but then ´imagecopy()´ also did not wotk it gives me an error ´PHP Warning: Wrong parameter count for imagecopyresampled() in /home2/inplenon/public_html/test/t-shirt/index.php - Pedro Soares 2012-04-03 21:49
The problem is $img is not an image handle, it's (likely) a string - Jon Grant 2012-04-03 22:07


10

You can also achieve this with the imagecopy family of functions if you don't happen to have ImageMagick available. See this example:

function ImageFlip ( $imgsrc, $mode )
{

    $width                        =    imagesx ( $imgsrc );
    $height                       =    imagesy ( $imgsrc );

    $src_x                        =    0;
    $src_y                        =    0;
    $src_width                    =    $width;
    $src_height                   =    $height;

    switch ( $mode )
    {

        case '1': //vertical
            $src_y                =    $height -1;
            $src_height           =    -$height;
        break;

        case '2': //horizontal
            $src_x                =    $width -1;
            $src_width            =    -$width;
        break;

        case '3': //both
            $src_x                =    $width -1;
            $src_y                =    $height -1;
            $src_width            =    -$width;
            $src_height           =    -$height;
        break;

        default:
            return $imgsrc;

    }

    $imgdest                    =    imagecreatetruecolor ( $width, $height );

    if ( imagecopyresampled ( $imgdest, $imgsrc, 0, 0, $src_x, $src_y , $width, $height, $src_width, $src_height ) )
    {
        return $imgdest;
    }

    return $imgsrc;

}
2012-04-03 21:45
by Jon Grant
´PHP Warning: imagecopyresampled(): supplied argument is not a valid Image resource´ it gives me this error. Also ´imagesx()´and/or imagesy()´were not working, but i fixed it with ´list($w, $h, $type, $attr) = getimagesize($image);´...any idea - Pedro Soares 2012-04-03 21:57
What exactly are you sending in the query string with your $img = $_GET['img']; ... you need to load the image into a proper image handle. Possibly imagecreatefrompng if you have a filename. Otherwise if you are trying to upload a file, look into the PHP file upload handling documentation - Jon Grant 2012-04-03 22:06
I'll check it thank you - Pedro Soares 2012-04-03 22:15
you'r right my mistake. I was perform the function incorrectly. Thank you very much, and to you all - Pedro Soares 2012-04-04 11:21


5

Using ImageMagick and the flipImage() and flopImage() methods, the following example is from devzone.zend.com:

<?php
try {
  // initialize object
  $image = new Gmagick();

  // read image file
  $image->readImage('gallery/original.jpg');

  // flip image vertically
  $image->flipImage();

  // write new image file
  $image->writeImage('gallery/new_1.jpg');

  // revert
  $image->flipImage();

  // flip image horizontally
  $image->flopImage();

  // write new image file
  $image->writeImage('gallery/new_2.jpg');

  // free resource handle
  $image->destroy();
} catch (Exception $e) {
  die ($e->getMessage());
}
?>

With the following results:

enter image description here

2012-04-03 21:39
by ghoti
i was trying not to use ImageMagick...isn't there a another way - Pedro Soares 2012-04-03 21:50
You didn't mention that restriction in your question. There's always another way - ghoti 2012-04-03 23:49


2

For PHP >= 5.5 you can use the imageflip GD native function.

2014-12-17 22:28
by Victor BV