Open In App

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:

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


Article Tags :