GithubHelp home page GithubHelp logo

spritesheet's Introduction

Sprite Creator

The Sprite Creator is a PHP script that generates a sprite image and corresponding CSS stylesheet from a folder of source images. It allows you to create a single image sprite that combines multiple images into one, reducing the number of HTTP requests and improving page load times. The generated CSS stylesheet provides the necessary background positions for each image within the sprite.

Requirements

  • PHP 5.6 or later

Usage

  1. Place the source images in the specified folder ($p['sourceImagesFolder']).
  2. Set the desired sprite width and height ($p['spriteWidth'] and $p['spriteHeight']).
  3. Specify the output directory for the sprite image and CSS file ($p['writeSpriteFilesPublic']).
  4. Define the CSS class name ($p['cssClass']) for the sprite image.
  5. Run the create_sprites() function and pass the parameters array ($p).
$p = [];
$p['cssClass'] = 'right-sprite';                  // CSS Class name
$p['spriteWidth'] = 32;                           // Cropped Sprite width (cover crop)
$p['spriteHeight'] = 32;                          // Cropped Sprite height (cover crop)
$p['writeSpriteFilesPublic'] = 'example-output';  // Relative output directory (no slashes)
$p['sourceImagesFolder'] = 'example-input';       // Relative images input directory (no slashes) (recursive)
$p['outputUrlSlugs'] = 'assets/css/sprites';      // Output url path (no slashes) (relative)

echo create_sprites($p);
php .\right-sprite.php

The function will generate a sprite image and corresponding CSS file in the specified output directory. The CSS file will contain the necessary CSS rules for displaying the individual images within the sprite. Functions

create_sprites($p)

This function generates the sprite image and CSS file.

Parameters:

cssClass (optional): The CSS class name for the sprite image. Default: right-sprite.
spriteWidth (optional): The width of each individual image in the sprite. Default: 200.
spriteHeight (optional): The height of each individual image in the sprite. Default: 200.
writeSpriteFilesPublic (required): The output directory for the sprite image and CSS file.
sourceImagesFolder (required): The folder containing the source images.
outputUrlSlugs (required): The output URL path.

Generates CSS and Sprite Image (see example-output directory)

example-output/right-sprite.css
example-output/right-sprite.jpg

Returns:

The URL of the generated sprite image file.

CSS Generation

      .right-sprite { 
        background: url("right-sprite.png");
        background-size: 544px 512px;
      }
      
      .right-sprite.spr-example-1-png { 
        background-position: -0px -0px; 
      }
      
      .right-sprite.spr-example-2-png { 
        background-position: -32px -0px; 
      }

Classnames are based on the input file

preg_replace(['/[^a-zA-Z0-9]+/', '/-+/'], ['-', '-'], strtolower($filename));

Caching

As a large directory opf fullsize images might take a while to process, the script creates a sprite sized thumbnail for faster reprocessing - these are placed in the [script directory]/cropped/file-name-200x200.png

Made for this SO question: https://stackoverflow.com/questions/19229123/css-sprites-for-dynamic-images-using-php

spritesheet's People

Contributors

andyg2 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.