spriter('*.png', 'hires.png', 0); < ?php function spriter($dir = '*.png', $dest = 'sprite.png', $spacing = 0) { // define icons sizes $icon_width = 32; $icon_height = 32; // start height of my sprite canvas $height = 0; // 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); }