PHP imageflip() Tutorial

Udemy Generic 728x90

Definition:

Flip an image using the given mode i.e horizontal,vertical or both.This function is introduced in PHP 5.5 version.

Syntax:

bool imageflip ( resource$image , int$mode )

Parameters:

  • image - An image resource that is  is returned by one of the image creation function such as imagecreatetruecolor.
  • mode - This parameter is used to flip the image in different modes such as Horizontal,Vertical or Both.

Type of modes:

There are three types of flip modes.

  • IMG_FLIP_HORIZONTAL    :- Flips the given image horizontally.
  • IMG_FLIP_VERTICAL         :- Flips the given image vertically.
  • IMG_FLIP_BOTH                :- Flips the given image both horizontally and vertically.

Description

This function contains two parameter one contains image resource and its flip mode.It flips the given image according to the given mode such as horizontal,vertical or both and returns TRUE on success and FALSE on failure.

Example 1:

Flips the given image vertically

<br />
&lt;?php<br />
// Image File name<br />
 $filename = ‘jasmine.jpg';<br />
 // Content type<br />
 header('Content-type: image/jpg’);<br />
 // Create a new image from file<br />
 $img = imagecreatefromjpeg($filename);<br />
 // Flips it vertically<br />
 imageflip($img, IMG_FLIP_VERTICAL);<br />
 // Output<br />
 imagejpeg($img);<br />
 imagedestroy($img);<br />
?&gt;<br />

Original Image

jasmine.jpg

jasmine.jpg

Output

jasmine_vert_flip

Example 2:

Flips the given image horizontally

<br />
&lt;?php<br />
 // Image file name<br />
 $filename = ‘jasmine.jpg';<br />
 // Content type<br />
 header('Content-type: image/jpg');<br />
 // Create a new image from file<br />
 $img = imagecreatefromjpeg($filename);<br />
 // Flip it horizontally<br />
 imageflip($img, IMG_FLIP_HORIZONTAL);<br />
 // Output<br />
 imagejpeg($img);<br />
 imagedestroy($img);<br />
?&gt;<br />

Original Image:

jasmine.jpg

jasmine.jpg

Output:

jsamine_hori_flip

Example 3:

Flips the given image both horizontally and vertically

<br />
&lt;?php<br />
      // Image file name<br />
     $filename = 'jasmine.jpg';<br />
      // Content type<br />
     header('Content-type: image/jpg');<br />
     // Create a new image from file<br />
     $img = imagecreatefromjpeg($filename);<br />
     // Flip it Both vertically and horizontally<br />
     imageflip($img, IMG_FLIP_BOTH);<br />
     // Output<br />
     imagejpeg($img);<br />
     imagedestroy($img);<br />
?&gt;<br />

Original Image:

jasmine.jpg

jasmine.jpg

Output:

jasmine_both_flip

Example 4:

Create an image and then flips it horizontally.

&lt;?php<br />
     //Create a blank image of specified size<br />
     $image = imagecreate(200, 70);<br />
     // Allocate a color for an image<br />
     $background = imagecolorallocate($image, 100, 0,   0);<br />
     $color      = imagecolorallocate($image, 200, 100, 0);<br />
     //Draw a string horizontally<br />
     imagestring($image, 5, 10, 20, &quot;SEE HORIZONTAL FLIP&quot;, $color);<br />
     // set content-type<br />
     header(&quot;Content-Type: image/jpeg&quot;);<br />
     //Flips the image horizontally<br />
     imageflip($image,IMG_FLIP_HORIZONTAL);<br />
     // show Output<br />
     imagejpeg($image);<br />
     imagedestroy($image);<br />
?&gt;<br />

Original Image:
horizontalimg

Output:

horizontalflip

Conclusion:

This function is easy to use and it reduces the line of code that we were using in earlier versions to flip an image and increases the code readability.One important thing needs to remember before using this function- PHP should be compiled with the bundled version of the GD library.    

Udemy Generic 728x90

Spread the word. Share this post!