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.

Things You'll Need

  • Web page editing software
  • Image files
  • Web server with PHP installed
Show More

Instructions

  1. 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
      PHP code to rotate 90 degrees

      Copy the code from this illustration into the PHP file and customize the PHP code to include the name of your image file that will be rotated and the angle to rotate: 90, 180 or 270.

    • 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
      Web page code with PHP IMG tag

      Insert an image tag with the PHP program name in place of the image file name. The code will look like: <img src="rotate_image.php" alt="Rotating Image" name="ROTATE_ME" id="ROTATE_ME" />. Place this in the HTML code where you want the image to appear. Save changes.

    • 5
      Image rotation in browser

      Upload the image file, the web page and the PHP file to your web server and preview in your web browser.

    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.

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.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured