How To Flip Image In PHP

In this article, we are going to learn about how to flip an image in PHP.

In this article, I have shown the Image Flip functionality using GD( Graphics Draw ) extension. So first of all you need to check whether the extension has been enabled or not.

If you are not aware of it. I will explain each step on how to enable it or you can follow the steps to check if it’s already enabled.

Notes: Your Xampp has not installed a GD. So here are a defined few steps for how to install GD (Graphics Draw) library.

Step- 1: First of all, stop an Apache and MySQL of Xampp control panel.

Step-2: Then Go to in xampp\php directory and find a php.ini file.

Step-3: Open php.ini file and search gd into a file.

Step-4: You get an extension of gd like (;extension=gd), need to remove (;) this.

Here is uncomment an extension=gd image.

Step-5: Now start an Apache and MySQL of Xampp control panel.

Now, open Xampp info and you can see the gd is enabled in your Xampp info.

Now create an image folder and add images to it.

Create an index.php file and add the below code in the file.

imgflip() :

The imgflip() function is used to flip an image using a given mode.

Syntax:

bool imageflip($image, $mode)

Parameters:

image: An image resource created using imagecreatefromjpeg() or imagecreatefrompng().

mode: The flip mode. Here are the defined modes:

IMG_FLIP_VERTICAL – Flips the image vertically.

IMG_FLIP_HORIZONTAL – Flips the image horizontally.

IMG_FLIP_BOTH – Flips the image both vertically and horizontally.

You can see the original image.

Now we are going to flip the image.

IMG_FLIP_VERTICAL Mode:

The following example of flipping an image vertically:

<?php 
  $filename = 'image/bird.jpg';
  header('Content-type: image/jpeg');
  $image = imagecreatefromjpeg($filename);
  imageflip($image, IMG_FLIP_VERTICAL);
  imagejpeg($image); 
  imagedestroy($image);
?>

Output:

IMG_FLIP_HORIZONTAL Mode:

The following example of flipping an image horizontally:

<?php 
  $filename = 'image/bird.jpg';
  header('Content-type: image/jpeg');
  $image = imagecreatefromjpeg($filename);
  imageflip($image, IMG_FLIP_HORIZONTAL);
  imagedestroy($image); 
  imagejpeg($image); 
  imagedestroy($image);
?>

Output:

IMG_FLIP_BOTH Mode:

The following example of flipping both vertically and horizontally:

<?php 
  $filename = 'image/bird.jpg';
  header('Content-type: image/jpeg');
  $image = imagecreatefromjpeg($filename);
  imageflip($image, IMG_FLIP_BOTH);
  imagedestroy($image); 
  imagejpeg($image);
  imagedestroy($image);
?>

Output:

Thank You, hope you guys found something useful.🙂

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories