This article describes how to use PHP to generate a sprite synthetic graph based on the images in the folder. For more information, see
The code is as follows:
$ Srcdir = './image /';
$ Prefix = "pic11 _";
$ Dst = "image ";
$ Imagedir = scandir ($ srcdir );
Array_shift ($ imagedir );
Array_shift ($ imagedir );
$ Width = 0;
$ Height = 0;
Foreach ($ imagedir as $ key => $ value ){
$ Picinfo = getimagesize ($ srcdir. $ value );
$ Width = $ picinfo [0] + $ width;
If ($ height <$ picinfo [1]) {
$ Height = $ picinfo [1];
}
}
$ Image = imagecreatetruecolor ($ width, $ height );
Imagesavealpha ($ image, true );
$ Color = imagecolorallocatealpha ($ image, 0,127 );
Imagefill ($ image, 0, 0, $ color );
$ Width = 0;
$ Height = 0;
$ Css = "";
Foreach ($ imagedir as $ key => $ value ){
$ Picinfo = getimagesize ($ srcdir. $ value );
$ Im = imagecreatefrompng ($ srcdir. $ value); // create an image
Imagecopymerge ($ image, $ im, $ width, 0, 0, 0, $ picinfo [0], $ picinfo [1], 100 );
$ Picname = pathinfo ($ srcdir. $ value );
$ Css = ". ". $ prefix. $ picname ['filename']. "{height :". $ picinfo [0]. "px; width :". $ picinfo [1]. "px; background-position :-". $ width. "px 0px ;}". $ css;
$ Width = $ width + $ picinfo [0];
Imagedestroy ($ im); // destroy the image
}
$ Css = $ css. "[class * =". $ prefix. "] {background-image: url('image.png ');}}";
$ Css = $ css. ".". $ prefix. "{background-image: url('image.png ') ;}"; // Compatible with ie series
File_put_contents ("./" .20.dst.'.css ', $ css );
Imagepng ($ image, "./" .20.dst.'.png ');
Imagedestroy ($ image );
?>