<commit_before><?php

$config['caching'] = true;

class Minotar {
	const DEFAULT_SKIN = 'char';

	public static $expires = 86400; // 24 hours in seconds


	public static function get($username) {
		$lowercase = strtolower($username);
		
		$skin = './minecraft/skins/'.$lowercase.'.png';
		if(file_exists($skin)) {
			if(time() - filemtime($skin) < self::$expires) return $lowercase;
			$cached = true;
		}

		$binary = self::fetch('http://s3.amazonaws.com/MinecraftSkins/'.$username.'.png');
		if($binary === false) {
			if($cached) return $lowercase;

			header('Status: 404 Not Found');
			return ($username == self::DEFAULT_SKIN)
				? $lowercase
				: self::get(self::DEFAULT_SKIN);
		}

		$img = new Imagick();
		$img->readImageBlob($binary);
		$img->stripImage(); // strip metadata
		$img->writeImage($skin);

		self::_getHelm($img)->writeImage('./minecraft/helms/'.$lowercase.'.png');
		self::_getHead($img)->writeImage('./minecraft/heads/'.$lowercase.'.png');
		self::_getPlayer($img)->writeImage('./minecraft/players/'.$lowercase.'.png');

		return $lowercase;
	}


	protected static function _getHead($skin) {
		return $skin->getImageRegion(8, 8, 8, 8);
	}

	protected static function _getHelm($skin) {
		$head = self::_getHead($skin);

		// detect helm
		$bg = $skin->exportImagePixels(0, 0, 1, 1, 'RGB', Imagick::PIXEL_CHAR);
		$pixels = $skin->exportImagePixels(40, 8, 8, 8, 'RGB', Imagick::PIXEL_CHAR);
		
		foreach(array_chunk($pixels, 3) as $pixel) {
			if($pixel === $bg) continue;

			// draw helm
			$helm = $skin->getImageRegion(8, 8, 40, 8);
			$head->compositeImage($helm, Imagick::COMPOSITE_DEFAULT, 4, 0);
			break;
		}

		return $head;
	}

	protected static function _getPlayer($skin) {
		$player = new Imagick();
		$player->newImage(16, 32, 'transparent', 'png');

		// draw helm
		$player->compositeImage(self::_getHelm($skin), Imagick::COMPOSITE_DEFAULT, 4, 0 );
		// draw body
		$part = $skin->getImageRegion(8, 12, 20, 20);
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 4, 8);
		// draw arms
		$part = $skin->getImageRegion(4, 12, 44, 20);
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 0, 8);
		$part->flopImage();
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 12, 8);
		// draw legs
		$part = $skin->getImageRegion(4, 12, 4, 20);
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 4, 20);
		$part->flopImage();
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 8, 20);
		
		return $player;
	}

<commit_msg>Enforce Imagick argument type<commit_after><?php

$config['caching'] = true;

class Minotar {
	const DEFAULT_SKIN = 'char';

	public static $expires = 86400; // 24 hours in seconds


	public static function get($username) {
		$lowercase = strtolower($username);
		
		$skin = './minecraft/skins/'.$lowercase.'.png';
		if(file_exists($skin)) {
			if(time() - filemtime($skin) < self::$expires) return $lowercase;
			$cached = true;
		}

		$binary = self::fetch('http://s3.amazonaws.com/MinecraftSkins/'.$username.'.png');
		if($binary === false) {
			if($cached) return $lowercase;

			header('Status: 404 Not Found');
			return ($username == self::DEFAULT_SKIN)
				? $lowercase
				: self::get(self::DEFAULT_SKIN);
		}

		$img = new Imagick();
		$img->readImageBlob($binary);
		$img->stripImage(); // strip metadata
		$img->writeImage($skin);

		self::_getHelm($img)->writeImage('./minecraft/helms/'.$lowercase.'.png');
		self::_getHead($img)->writeImage('./minecraft/heads/'.$lowercase.'.png');
		self::_getPlayer($img)->writeImage('./minecraft/players/'.$lowercase.'.png');

		return $lowercase;
	}


	protected static function _getHead(Imagick $skin) {
		return $skin->getImageRegion(8, 8, 8, 8);
	}

	protected static function _getHelm(Imagick $skin) {
		$head = self::_getHead($skin);

		// detect helm
		$bg = $skin->exportImagePixels(0, 0, 1, 1, 'RGB', Imagick::PIXEL_CHAR);
		$pixels = $skin->exportImagePixels(40, 8, 8, 8, 'RGB', Imagick::PIXEL_CHAR);
		
		foreach(array_chunk($pixels, 3) as $pixel) {
			if($pixel === $bg) continue;

			// draw helm
			$helm = $skin->getImageRegion(8, 8, 40, 8);
			$head->compositeImage($helm, Imagick::COMPOSITE_DEFAULT, 4, 0);
			break;
		}

		return $head;
	}

	protected static function _getPlayer(Imagick $skin) {
		$player = new Imagick();
		$player->newImage(16, 32, 'transparent', 'png');

		// draw helm
		$player->compositeImage(self::_getHelm($skin), Imagick::COMPOSITE_DEFAULT, 4, 0 );
		// draw body
		$part = $skin->getImageRegion(8, 12, 20, 20);
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 4, 8);
		// draw arms
		$part = $skin->getImageRegion(4, 12, 44, 20);
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 0, 8);
		$part->flopImage();
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 12, 8);
		// draw legs
		$part = $skin->getImageRegion(4, 12, 4, 20);
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 4, 20);
		$part->flopImage();
		$player->compositeImage($part, Imagick::COMPOSITE_DEFAULT, 8, 20);
		
		return $player;
	}


{'fraction_non_alphanumeric': 0.19302417756638923, 'fraction_numerical': 0.028141101862861673, 'mean_word_length': 4.479913137893594, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4644387', 'n_tokens_mistral': 2115, 'n_tokens_neox': 1835, 'n_words': 356}