Php Gd Sprite 创建

标签 php css gd sprite

此函数的目标是创建目录中所有 .png 的 Sprite 。我已经研究了一段时间,试图让它发挥作用。我检查了所有文件夹权限并确保 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);
    }

?>

最佳答案

试试这个:http://github.com/namics/php-spriter

您可以根据需要配置它: - 视网膜的多个 Sprite 图像 - 可编辑的 CSS/Less/Sass 模板 - 自动变化检测 - 麻省理工学院许可

关于Php Gd Sprite 创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926570/

相关文章:

php - 如何在 wordpress 主题 (mystile) 标题上添加图像和 Logo (psd)?

css - 为什么 'foo bar' 和 'foo > bar' 在 CSS 中具有相同的特异性?

javascript - 如何使此联系​​表具有互动性并反射(reflect)信息?

php - 使用 Liipimaginebundle 更新和删除缓存图像

html - 如何使用垂直字体创建垂直菜单?

php - 使用PHP GD创建具有不同字体的图像形式文本

php - 用于 PHP GD 图的平滑起伏不定的图像线

php - 在 PHP GD 中获取图像创建日期

php - Laravel 5.2 中无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]

css - html5-css3 中不显示下拉菜单