Basic PHP Image Rotation


/ Published in: PHP
Save to your folder(s)

simple image rotation script; save as rotate.php in folder with images, call to display


Copy this code and paste it in your HTML
  1. //save in folder with images, refer in scriptAdd the script like you would add an image.
  2. //<img src=”path-to-your-images/rotate.php”>
  3. //images/home-page/rotate.php
  4. $folder = '.';
  5.  
  6. $extList = array();
  7. $extList['gif'] = 'image/gif';
  8. $extList['jpg'] = 'image/jpeg';
  9. $extList['jpeg'] = 'image/jpeg';
  10. $extList['png'] = 'image/png';
  11.  
  12. $img = null;
  13.  
  14. if (substr($folder,-1) != '/') {
  15. $folder = $folder.'/';
  16. }
  17.  
  18. if (isset($_GET['img'])) {
  19. $imageInfo = pathinfo($_GET['img']);
  20. if (
  21. isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
  22. file_exists( $folder.$imageInfo['basename'] )
  23. ) {
  24. $img = $folder.$imageInfo['basename'];
  25. }
  26. } else {
  27. $fileList = array();
  28. $handle = opendir($folder);
  29. while ( false !== ( $file = readdir($handle) ) ) {
  30. $file_info = pathinfo($file);
  31. if (
  32. isset( $extList[ strtolower( $file_info['extension'] ) ] )
  33. ) {
  34. $fileList[] = $file;
  35. }
  36. }
  37. closedir($handle);
  38.  
  39. if (count($fileList) > 0) {
  40. $imageNumber = time() % count($fileList);
  41. $img = $folder.$fileList[$imageNumber];
  42. }
  43. }
  44.  
  45. if ($img!=null) {
  46. $imageInfo = pathinfo($img);
  47. $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
  48. header ($contentType);
  49. readfile($img);
  50. } else {
  51. if ( function_exists('imagecreate') ) {
  52. header ("Content-type: image/png");
  53. $im = @imagecreate (100, 100)
  54. or die ("Cannot initialize new GD image stream");
  55. $background_color = imagecolorallocate ($im, 255, 255, 255);
  56. $text_color = imagecolorallocate ($im, 0,0,0);
  57. imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color);
  58. imagepng ($im);
  59. }
  60. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.