function scaleNearestNeighbor(&$image, &$newImage) { $imageInfo = getImageInfo($image); $colors = array(); for ($y = 0; $y < $imageInfo['height']; $y++) { for ($x = 0; $x < $imageInfo['width']; $x++) { $color = imagecolorsforindex($image, imagecolorat($image, $x, $y)); if ($color['alpha'] < 0x7f) { $hex = ownDecHex($color['red']) . ownDecHex($color['green']) . ownDecHex($color['blue']); if (!isset($colors[$hex])) { $colors[$hex] = imagecolorallocatealpha($newImage, $color['red'], $color['green'], $color['blue'], 0x0); } imagefilledrectangle($newImage, intval($x * 4), intval($y * 4), intval($x * 4 + 4), intval($y * 4 + 4), $colors[$hex]); } } } }
*/ require_once './lib.php'; $sprites = glob('./sprites/*.png'); if (!is_dir('./negative')) { mkdir('./negative'); } foreach ($sprites as $sprite) { $image = imagecreatefrompng($sprite); $imageInfo = getImageInfo($image); $newImage = imagecreatetruecolor($imageInfo['width'], $imageInfo['height']); $colors = array(); echo 'Turning sprite ' . basename($sprite, '.png') . ' to negative' . "\n"; imagealphablending($newImage, false); imagesavealpha($newImage, true); imagefill($newImage, 0, 0, imagecolorallocatealpha($newImage, 0xff, 0xff, 0xff, 0x7f)); for ($y = 0; $y < $imageInfo['height']; $y++) { for ($x = 0; $x < $imageInfo['width']; $x++) { $color = imagecolorsforindex($image, imagecolorat($image, $x, $y)); if ($color['alpha'] < 0x7f) { $hex = ownDecHex($color['red']) . ownDecHex($color['green']) . ownDecHex($color['blue']); if (!isset($colors[$hex])) { $colors[$hex] = imagecolorallocatealpha($newImage, 0xff - $color['red'], 0xff - $color['green'], 0xff - $color['blue'], 0x0); } imagesetpixel($newImage, $x, $y, $colors[$hex]); } } } imagepng($newImage, './negative/' . basename($sprite), 9); imagedestroy($image); imagedestroy($newImage); }