Random Image Script: Modern Alternatives

The original Random Image displayed rotating banners and photos from a directory. Today, JavaScript carousels and image APIs offer dynamic, animated solutions with better performance.

Image Display

JavaScript Image Carousels

Swiper

Modern mobile touch slider with hardware accelerated transitions. Framework agnostic with support for React, Vue, and Angular. Includes lazy loading, virtual slides, and parallax effects.

Free MIT License Touch support All frameworks
Splide

Lightweight, flexible slider with no dependencies. Written in TypeScript with full type support. Features multiple slides, thumbnails, and video integration.

Free MIT License ~29KB TypeScript
Glide.js

Dependency-free ES6 slider with modular architecture. Small footprint with keyboard navigation and responsive breakpoints. Supports autoplay and multiple transition effects.

Free MIT License ~23KB
Flickity

Touch-enabled, responsive carousel with physics-based animation. Includes accessible controls, auto-play, and lazy loading. Works great for gallery and product images.

Commercial license Physics animations Accessible

Random Image APIs

Unsplash API

Beautiful, free images from professional photographers. High-quality photos with random endpoint, search by topic, and curated collections. Perfect for hero images and backgrounds.

Free tier (50 req/hr) Production ready High quality
https://source.unsplash.com/random/800x600
https://source.unsplash.com/random/800x600?nature
Lorem Picsum

Easy placeholder images for development and testing. Simple URL-based API with no authentication required. Specify dimensions, blur, and grayscale effects directly in the URL.

Free No API key URL-based
<img src="https://picsum.photos/400/300?random=1">
<img src="https://picsum.photos/400/300?blur=2">
Pexels API

Free stock photos and videos API. Extensive library with search, curated collections, and trending content. Includes attribution data and multiple image sizes.

Free (200 req/hr) Photos + Videos
Placekitten

Placeholder images featuring adorable kittens. Simple URL-based service perfect for development and testing. Brings joy to your mockups.

Free Cats!
<img src="https://placekitten.com/400/300">

Simple JavaScript Solution

Random Image from Array
const images = [
  '/images/banner1.jpg',
  '/images/banner2.jpg',
  '/images/banner3.jpg',
  '/images/banner4.jpg'
];

const randomIndex = Math.floor(Math.random() * images.length);
document.getElementById('banner').src = images[randomIndex];

// Or with CSS background:
document.body.style.backgroundImage = `url('${images[randomIndex]}')`;

// Preload images for smooth experience:
images.forEach(src => {
  const img = new Image();
  img.src = src;
});
Random Image from Directory (PHP)
<?php
$imageDir = 'images/banners/';
$images = glob($imageDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];
?>
<img src="<?php echo htmlspecialchars($randomImage); ?>" alt="Random banner">
Weighted Random Selection
const images = [
  { src: '/images/featured.jpg', weight: 5 },
  { src: '/images/banner1.jpg', weight: 1 },
  { src: '/images/banner2.jpg', weight: 1 }
];

// Featured image gets 5/7 = 71% chance
const totalWeight = images.reduce((sum, img) => sum + img.weight, 0);
let random = Math.random() * totalWeight;

for (const img of images) {
  if (random < img.weight) {
    document.getElementById('banner').src = img.src;
    break;
  }
  random -= img.weight;
}

Modern Use Cases

Banner Rotation

Rotate advertising banners with weighted selection for premium advertisers.

Background Variety

Show different hero images on each page load to keep design fresh.

Product Showcase

Display random featured products or deals on homepage.

Photo Gallery

Show random images from photography portfolio or travel gallery.

Feature Comparison

Feature Random Image CGI Modern Solutions
TransitionsNoneCSS/JS animations
Lazy loadingNoBuilt-in
Touch/SwipeNoYes
ResponsiveNoYes
ThumbnailsNoYes
AutoplayPage refreshContinuous
Server loadEach requestClient-side
CachingLimitedBrowser cache
Image effectsNoZoom, blur, parallax

Recommendation

Full-featured: Swiper

Lightweight: Splide or Glide.js

Simple rotation: Vanilla JavaScript

API images: Unsplash

Placeholders: Lorem Picsum

NPM Install

npm install swiper
npm install @splidejs/splide
npm install @glidejs/glide
npm install flickity

Performance Tips

  • Use WebP format for smaller files
  • Implement lazy loading
  • Optimize image dimensions
  • Enable browser caching
  • Use CDN for delivery
All Examples Random Image Overview