PHP | imagerotate() Function

The imagerotate() function is an inbuilt function in PHP which is used to rotate an image with a given angle in degrees. The rotation center of the image is center.

Syntax:

resource imagerotate( $image, $angle, $bgd_color, $ignore_transparent = 0 ) 

Parameters: This function accepts four parameters as mentioned above and described below:

  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $angle: This parameter holds the rotation angle in degrees. The rotation angle is used to rotate an image in anticlockwise direction.
  • $bgd_color: This parameter holds the background color of uncovered zone after rotation.
  • $ignore_transparent: If this parameter set and non-zero then transparent colors are ignored.

Return Value: This function returns an image resource for the rotated image on success, or False on failure.

Below programs illustrate the imagerotate() function in PHP:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    
// Assign image file to variable 
$image_name
     
// Load image file 
$image = imagecreatefrompng($image_name);  
  
// Use imagerotate() function to rotate the image
$img = imagerotate($image, 180, 0);
  
// Output image in the browser 
header("Content-type: image/png"); 
  
imagepng($img); 
  
?> 

chevron_right


Output:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
    
// It create the size of image or blank image. 
$image = imagecreatetruecolor(500, 300); 
     
// Set the background color of image. 
$bg = imagecolorallocate($image, 205, 220, 200); 
     
// Fill background with above selected color. 
imagefill($image, 0, 0, $bg); 
    
// Set the color of an ellipse. 
$col_ellipse = imagecolorallocate($image, 0, 102, 0); 
     
// Function to draw the filled ellipse. 
imagefilledellipse($image, 250, 150, 400, 250, $col_ellipse); 
  
// Use imagerotate() function to rotate the image
$img = imagerotate($image, 90, 0);
  
// Output image in the browser 
header("Content-type: image/png"); 
  
imagepng($img); 
  
?> 

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.imagerotate.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.