Thursday, February 19, 2026
HomeLanguagesPHP | imagerotate() Function

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:




<?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); 
  
?> 


Output:

Program 2:




<?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); 
  
?> 


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS