9951 explained code solutions for 126 technologies


php-gdHow to rotate an image


<?php

$file = '/var/www/examples/heroine.png';
$im = imagecreatefrompng($file);

$c_black = imageColorAllocate($im, 0,0,0);
$imr = imagerotate( $im, 90, $c_black );

imagePng($imr, '/tmp/image.png');ctrl + c
$file

image to rotate

imagecreatefrompng

creates GD image object from given PNG image

imageColorAllocate

creates color object to later use in image

imagerotate

rotates given image and returns new gd image object

90

rotate image by 90 degrees

$c_black

color to use for background (if necessary)

imagePng

saves image in PNG format to the given path


How to rotate an image, php gd

Usage example

<?php

$file = '/var/www/examples/heroine.png';
$im = imagecreatefrompng($file);

$c_black = imageColorAllocate($im, 0,0,0);
$imr = imagerotate( $im, 90, $c_black );

imagePng($imr, '/tmp/image.png');