Php Gd精灵创作

The goal of this function is to create a sprite of all the .pngs in a directory. I have been looking at this for some time trying to get it to work. I have checked all the folder permissions and made sure that the gd library is enabled. Any suggestions?

 <?php
    function spriter($dir = 'thumbs/*.png', $dest = 'thumbs/sprite.png', $spacing = 1) {

        // define icons sizes
        $icon_width = 32;
        $icon_height = 32;

        // start height of my sprite canvas
        $height = 100;

        // select all the icons and read theri height to build our canvas size.
        foreach (glob($dir) as $file) {
            list($w, $h) = getimagesize($file);
            // make sure out icon is a 32px sq icon
            if ($h == $icon_height)
                $height += ($h + $spacing);
        }

        // double our canvas height to allow for a gray-scale versions.
        $height = ($height * 2);

        // create our canvas
        $img = imagecreatetruecolor($icon_width, $height);
        $background = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $background);
        imagealphablending($img, false);
        imagesavealpha($img, true);

        // start placing our icons from the top down.
        $pos = 0;
        foreach (glob($dir) as $file) {
            $tmp = imagecreatefrompng($file);
            if (imagesy($tmp) == $icon_height) {
                imagecopy($img, $tmp, 0, $pos, 0, 0, $icon_width, $icon_height);
                $pos += ($icon_height + $spacing);
            }
            imagedestroy($tmp);
        }

        // place all of our icons on again, but this time convert them to gray-scale
        foreach (glob($dir) as $file) {
            $tmp = imagecreatefrompng($file);
            if (imagesy($tmp) == $icon_height) {
                imagefilter($tmp, IMG_FILTER_GRAYSCALE);
                imagecopy($img, $tmp, 0, $pos, 0, 0, $icon_width, $icon_height);
                $pos +=  ($icon_height + $spacing);
            }
            imagedestroy($tmp);
        }

        // create our final output image.
        imagepng($img, $dest);
    }

?>

Try this: http://github.com/namics/php-spriter

You can configure it to your needs: - Multiple sprite images for retina - Editable CSS/Less/Sass templates - automatic change detection - MIT licensed