<commit_before><?


if (!function_exists("imagerotate")) {
	function imagerotate($src_img, $angle) {
		$src_x = imagesx($src_img);
		$src_y = imagesy($src_img);
		if ($angle == 180) {
			$dest_x = $src_x;
			$dest_y = $src_y;
		}
		elseif ($src_x <= $src_y) {
			$dest_x = $src_y;
			$dest_y = $src_x;
		}
		elseif ($src_x >= $src_y) {
			$dest_x = $src_y;
			$dest_y = $src_x;
		}

		$rotate = imagecreatetruecolor($dest_x,$dest_y);
		imagealphablending($rotate, false);

		switch ($angle) {
		case 270:
			for ($y = 0; $y < ($src_y); $y++) {
				for ($x = 0; $x < ($src_x); $x++) {
					$color = imagecolorat($src_img, $x, $y);
					imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
				}
			}
			break;
		case 90:
			for ($y = 0; $y < ($src_y); $y++) {
				for ($x = 0; $x < ($src_x); $x++) {
					$color = imagecolorat($src_img, $x, $y);
					imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
				}
			}
			break;
		case 180:
			for ($y = 0; $y < ($src_y); $y++) {
				for ($x = 0; $x < ($src_x); $x++) {
					$color = imagecolorat($src_img, $x, $y);
					imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
				}
			}
			break;
		default:
			$rotate = $src_img;
		}
		return $rotate; 
	}
}

function pass_file_and_exit($file) {
	preg_match("'^(.*)\.(gif|jpe?g|png|thm)$'i", $file, $e);
	switch (strtolower($e[2])) {
	case 'jpg':
	case 'jpeg':
	case 'thm':
		header("Content-type: image/jpeg");
		break;
	case 'gif':
		header("Content-type: image/gif");
		break;
	case 'png':
		header("Content-type: image/png");
		break;
	default:
		exit;
		break;
	}
	$fp = fopen($file, "r");
	fpassthru($fp);
	fclose($fp);
	exit;
}

$max_size = 800;

$size = $_GET['s'];

$resample = $_GET['r'];

$image = $_GET['i'];

if (isset($_GET['u']))
	$user = $_GET['u'];

$album = dirname($image);

# passtrhru unsized?
if ($size == 0) {
	$o = "/home/" . $user . "/album/" . $image;
	if (file_exists($image))
		pass_file_and_exit($image);
}

if ($size > $max_size)
	$size = $max_size;

$cache_base = "/var/cache/httpd";
$cache_path = $cache_base . "/" . $album;
$cache_file = $cache_path . "/" . "x" . $size . "-" . basename($image);

if (!is_dir($cache_path))
	mkdir($cache_path);

header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($image)).' GMT', true, 200);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (80640 * 180)) . ' GMT', true, 200);

preg_match("'^(.*)\.(gif|jpe?g|png|thm)$'i", $image, $ext);

# fetch the (already rotated) cached file if present
if (file_exists($cache_file))
	pass_file_and_exit($cache_file);


$exif = exif_read_data($image, 0, true);
$o = 0;
if (is_array($exif)) {
	if (array_key_exists('IFD0', $exif)) {
		if (array_key_exists('Orientation', $exif['IFD0'])) {
			$o = $exif['IFD0']['Orientation'];
		}
	}
}

switch (strtolower($ext[2])) {
case 'jpg':
case 'jpeg':
case 'thm':
	$im  = imagecreatefromjpeg($image);
	header("Content-type: image/jpeg");
	break;
case 'gif':
	$im  = imagecreatefromgif($image);
	header("Content-type: image/gif");
	break;
case 'png' :
	$im  = imagecreatefrompng($image);
	header("Content-type: image/png");
	break;
default:
	exit;
	break;
}

list($width_orig, $height_orig) = getimagesize($image);

$ratio_orig = $width_orig / $height_orig;

if ($ratio_orig > 1) {
	$width = $size;
	$height = $size / $ratio_orig;
} else {
	$width = $size * $ratio_orig;
	$height = $size;
}

$save = imagecreatetruecolor($width, $height);
<commit_msg>Fix image passthru generation for not-yet-cached files.<commit_after><?


if (!function_exists("imagerotate")) {
	function imagerotate($src_img, $angle) {
		$src_x = imagesx($src_img);
		$src_y = imagesy($src_img);
		if ($angle == 180) {
			$dest_x = $src_x;
			$dest_y = $src_y;
		}
		elseif ($src_x <= $src_y) {
			$dest_x = $src_y;
			$dest_y = $src_x;
		}
		elseif ($src_x >= $src_y) {
			$dest_x = $src_y;
			$dest_y = $src_x;
		}

		$rotate = imagecreatetruecolor($dest_x,$dest_y);
		imagealphablending($rotate, false);

		switch ($angle) {
		case 270:
			for ($y = 0; $y < ($src_y); $y++) {
				for ($x = 0; $x < ($src_x); $x++) {
					$color = imagecolorat($src_img, $x, $y);
					imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
				}
			}
			break;
		case 90:
			for ($y = 0; $y < ($src_y); $y++) {
				for ($x = 0; $x < ($src_x); $x++) {
					$color = imagecolorat($src_img, $x, $y);
					imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
				}
			}
			break;
		case 180:
			for ($y = 0; $y < ($src_y); $y++) {
				for ($x = 0; $x < ($src_x); $x++) {
					$color = imagecolorat($src_img, $x, $y);
					imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
				}
			}
			break;
		default:
			$rotate = $src_img;
		}
		return $rotate; 
	}
}

function pass_file_and_exit($file) {
	preg_match("'^(.*)\.(gif|jpe?g|png|thm)$'i", $file, $e);
	switch (strtolower($e[2])) {
	case 'jpg':
	case 'jpeg':
	case 'thm':
		header("Content-type: image/jpeg");
		break;
	case 'gif':
		header("Content-type: image/gif");
		break;
	case 'png':
		header("Content-type: image/png");
		break;
	default:
		exit;
		break;
	}
	$fp = fopen($file, "r");
	fpassthru($fp);
	fclose($fp);
	exit;
}

$max_size = 800;

$size = $_GET['s'];

$resample = $_GET['r'];

$image = $_GET['i'];

if (isset($_GET['u']))
	$user = $_GET['u'];

$album = dirname($image);

# passtrhru unsized?
$obj = "/home/" . $user . "/album/" . $image;
if ($size == 0) {
	if (file_exists($obj))
		pass_file_and_exit($obj);
}

if ($size > $max_size)
	$size = $max_size;

$cache_base = "/var/cache/httpd";
$cache_path = $cache_base . "/" . $album;
$cache_file = $cache_path . "/" . "x" . $size . "-" . basename($image);

if (!is_dir($cache_path))
	mkdir($cache_path);

header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($obj)).' GMT', true, 200);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (80640 * 180)) . ' GMT', true, 200);

preg_match("'^(.*)\.(gif|jpe?g|png|thm)$'i", $image, $ext);

# fetch the (already rotated) cached file if present
if (file_exists($cache_file))
	pass_file_and_exit($cache_file);


$exif = exif_read_data($obj, 0, true);
$o = 0;
if (is_array($exif)) {
	if (array_key_exists('IFD0', $exif)) {
		if (array_key_exists('Orientation', $exif['IFD0'])) {
			$o = $exif['IFD0']['Orientation'];
		}
	}
}

switch (strtolower($ext[2])) {
case 'jpg':
case 'jpeg':
case 'thm':
	$im  = imagecreatefromjpeg($obj);
	header("Content-type: image/jpeg");
	break;
case 'gif':
	$im  = imagecreatefromgif($obj);
	header("Content-type: image/gif");
	break;
case 'png' :
	$im  = imagecreatefrompng($obj);
	header("Content-type: image/png");
	break;
default:
	exit;
	break;
}

list($width_orig, $height_orig) = getimagesize($obj);

$ratio_orig = $width_orig / $height_orig;

if ($ratio_orig > 1) {
	$width = $size;
	$height = $size / $ratio_orig;
} else {
	$width = $size * $ratio_orig;
	$height = $size;
}

$save = imagecreatetruecolor($width, $height);

{'fraction_non_alphanumeric': 0.2297395605994471, 'fraction_numerical': 0.013676705950822057, 'mean_word_length': 3.7570934256055364, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, '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': '1906923', 'n_tokens_mistral': 3284, 'n_tokens_neox': 2958, 'n_words': 614}