How to Rotate an Image With PHP
Rotating or swapping an image on a website can be done several different ways. With PHP can you change the angle of an image to a specified rotation (without needing a second image), or you can randomly rotate different images onto a web page every time the page loads. Either type of image rotation can be used to add dynamic elements that liven up a dull, static page.
Instructions
-
Rotate an Image to a Specific Angle
-
1
Open your web page editing software and create a PHP file to hold the PHP code.
- 2
-
-
3
Open your web page that will have this rotated image on it or create a new web page in your web editing software.
- 4
- 5
Rotate Random Images on a Web Page
-
6
Make a folder on your website to hold the images you want to rotate between each time the page loads.
-
7
Download the free Automatic Random Image Rotator PHP script by Dan P. Benjamin at A List Apart (see Resources) and save it has a PHP file with your web editing software.
-
8
Place this PHP file in the same folder as your images.
-
9
Open the web page you want to add randomly rotating images to in your web editing software.
-
10
Insert the image tag img src="path_to_images_folder/NAME_OF_SCRIPT.php" at the point on the page where you want the image to appear, replacing path_to_images_folder with the location of the folder of images and NAME_OF_SCRIPT with the file name you gave the PHP script in Step 2.
-
11
Save your changes, then load the folder of images (which contains the script) and the web page to your web server to test the page in your web browser. Click the browser's reload button to see the images rotate.
-
1
Tips & Warnings
You can combine Javascript commands with the PHP to rotate the angle of an image on mouse actions. The example here shows how to rotate images 90, 180 or 270 degrees. Information about rotating at other degree values can be found at discomoose.org (see link in Resources).
To run PHP scripts, your web server must have PHP installed. To use the imagerotate function, the GD libraries for PHP must be installed as well (which they usually are if PHP is available.