Php Gd精灵创建

Php Gd精灵创建,php,css,gd,sprite,Php,Css,Gd,Sprite,此函数的目标是创建目录中所有.png的精灵。我已经看了一段时间试图让它工作。我已检查了所有文件夹权限,并确保启用了gd库。有什么建议吗 <?php function spriter($dir = 'thumbs/*.png', $dest = 'thumbs/sprite.png', $spacing = 1) { // define icons sizes $icon_width = 32; $icon_height = 32;

此函数的目标是创建目录中所有.png的精灵。我已经看了一段时间试图让它工作。我已检查了所有文件夹权限,并确保启用了gd库。有什么建议吗

 <?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);
    }

试试这个:

您可以根据需要对其进行配置: -视网膜的多幅sprite图像 -可编辑的CSS/Less/Sass模板 -自动变化检测
-麻省理工学院许可证

它怎么不起作用?你得到了什么错误?它没有给出错误,只是没有输出png。