imagerotate() returns a new GD image. Destroy both images when you no longer need them.
<?php
$source = imagecreatefromjpeg('photo.jpg');
$background = imagecolorallocate($source, 255, 255, 255);
$rotated = imagerotate($source, 30, $background);
imagejpeg($rotated, 'rotated.jpg', 90);
imagedestroy($rotated);
imagedestroy($source);
?>Current example first; the original tutorial examples below are retained and cleaned up for continued learning.
<?php
header ("Content-Type: image/jpeg");
$file_name='../images/top2.jpg'; // Image collected
$angle='180'; // Angle of rotation is set
$img_source = imagecreatefromjpeg($file_name); // Image created
$img_rotated = imagerotate($img_source, $angle, 0); // Rotate the image
imagejpeg($img_rotated); // Output Displayed
imagedestroy($img_source); // Free the memory
imagedestroy($img_rotated); // Free the memory
?>
Above code is saved as gd-imagerotate-demo.php and displayed in this page, HTML code is here.
<img src=gd-imagerotate-demo.php>
imagerotate($source_image,$angle,$bg_color,$int_transparent)
$source_image : Image resource created before$angle: Rotational angle in degree $bg_color: Color of the uncovered zone after rotation $int_transparent: Transparent colors are ignored if value is given and not 0header ("Content-Type: image/jpeg");
$file_name='../images/top2.jpg';
$angle='45'; // Angle of rotation in degree
$img_source = imagecreatefromjpeg($file_name);
$bg_color = ImageColorAllocate ($img_source, 4, 20, 204);
// Rotate
$img_rotated = ImageRotate($img_source, $angle, $bg_color, 0);
// Output Display
imagejpeg($img_rotated);
// Free the memory
imagedestroy($img_source);
imagedestroy($img_rotated);
Above code is saved as gd-imagerotate-demo2.php and displayed in this page, HTML code is here.
<img src=gd-imagerotate-demo2.php>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.