<commit_before><?
include "../include.php";
include "$site_root/$admin_folder/pages/includes/security.php";

// Make sure content folder exists
$check_folder = "$site_root/$typset_settings->content_folder";
if (!is_dir($check_folder)) mkdir($check_folder);

// Image data
$image_data = $_POST['image_data'];
$p = strpos($image_data, ',');
$image_data = substr($image_data, $p + 1);
$image_data = base64_decode($image_data);

// Get other posted data
$image_name = $_POST['image_name'];
$image_width = $_POST['image_width'];
$image_height = $_POST['image_height'];
if (isset($_POST['thumb_width'])) $thumb_width = $_POST['thumb_width'];
if (isset($_POST['thumb_height'])) $thumb_height = $_POST['thumb_height'];
if (isset($_POST['thumb'])) $thumb = $_POST['thumb'];
if (isset($_POST['old_file'])) $old_file = $_POST['old_file'];

// Get file name info
$file_info = pathinfo($image_name);
$filename = $file_info['filename'];
$extension = strtolower($file_info['extension']);
if ($extension === "jpeg") $extension = "jpg";

// Assign new filename
$random = rand(10,99);
$filename = $typset->urn($filename) . "-$random";

// Get file destination
$target_path = "$site_root/$typset_settings->content_folder/$filename.$extension";

// Upload File
if (!file_put_contents($target_path, $image_data)):
	$typset->respond(array(
		"status" => "error",
		"message" => "Error: File couldn\'t be uploaded. Error # " . $_FILES['upload']['error']
	));
endif;
	
// Get original image size
$image = $target_path;
$image_stats = GetImageSize($image); 
$original_width = $image_stats[0]; 
$original_height = $image_stats[1];
$original_pixels = $original_width * $original_height;

// Resize
if ($original_width > $image_width or $original_height > $image_width):
	$typset->resize_image(array(
		"original" => $image,
		"destination" => $image, 
		"width" => $image_width,
		"height" => $image_height
	));
endif;

// Create thumbnail
if (isset($thumb)):
	$target_path_thumb = "$site_root/$typset_settings->content_folder/$filename-thumb.$extension";
	$typset->resize_image(array(
		"original" => $image,
		"destination" => $target_path_thumb, 
		"width" => $thumb_width,
		"height" => $thumb_height
	));
endif;

// Erase old files
if (isset($old_file)):
	$old_image = "$site_root/$typset_settings->content_folder/$old_file";
	if (is_file($old_image)) unlink($old_image);
	$old_thumb = "$site_root/$typset_settings->content_folder/" . $typset->thumb($old_file);
	if (is_file($old_thumb)) unlink($old_thumb);<commit_msg>convert uploaded files back to original format<commit_after><?
include "../include.php";
include "$site_root/$admin_folder/pages/includes/security.php";

// Make sure content folder exists
$check_folder = "$site_root/$typset_settings->content_folder";
if (!is_dir($check_folder)) mkdir($check_folder);

// Image data
$image_data = $_POST['image_data'];
$p = strpos($image_data, ',');
$image_data = substr($image_data, $p + 1);
$image_data = base64_decode($image_data);

// Get other posted data
$image_name = $_POST['image_name'];
$image_width = $_POST['image_width'];
$image_height = $_POST['image_height'];
if (isset($_POST['thumb_width'])) $thumb_width = $_POST['thumb_width'];
if (isset($_POST['thumb_height'])) $thumb_height = $_POST['thumb_height'];
if (isset($_POST['thumb'])) $thumb = $_POST['thumb'];
if (isset($_POST['old_file'])) $old_file = $_POST['old_file'];

// Get file name info
$file_info = pathinfo($image_name);
$filename = $file_info['filename'];
$extension = strtolower($file_info['extension']);
if ($extension === "jpeg") $extension = "jpg";

// Assign new filename
$random = rand(10,99);
$filename = $typset->urn($filename) . "-$random";

// Get file destination
$target_path = "$site_root/$typset_settings->content_folder/$filename.$extension";

// Upload File
if (!file_put_contents($target_path, $image_data)):
	$typset->respond(array(
		"status" => "error",
		"message" => "Error: File couldn\'t be uploaded. Error # " . $_FILES['upload']['error']
	));
endif;
	
// Get original image size
$image = $target_path;
$image_stats = GetImageSize($image); 
$original_width = $image_stats[0]; 
$original_height = $image_stats[1];
$original_pixels = $original_width * $original_height;

// Convert back to actual file format
$destination_image = imagecreatefrompng($image);
if ($extension === "jpg"):
	imagejpeg($destination_image, $image, $typset_settings->image_quality);
elseif ($extension === "gif"):
	imagegif($destination_image, $image);
elseif ($extension === "png"):
	imagepng($destination_image, $image);
endif;
imagedestroy($destination_image);

// Resize
/*
if ($original_width > $image_width or $original_height > $image_width):
	$typset->resize_image(array(
		"original" => $image,
		"destination" => $image, 
		"width" => $image_width,
		"height" => $image_height
	));
endif;
*/

// Create thumbnail
if (isset($thumb)):
	$target_path_thumb = "$site_root/$typset_settings->content_folder/$filename-thumb.$extension";
	$typset->resize_image(array(
		"original" => $image,
		"destination" => $target_path_thumb, 
		"width" => $thumb_width,
		"height" => $thumb_height
	));
endif;

// Erase old files
if (isset($old_file)):
	$old_image = "$site_root/$typset_settings->content_folder/$old_file";
	if (is_file($old_image)) unlink($old_image);
	$old_thumb = "$site_root/$typset_settings->content_folder/" . $typset->thumb($old_file);
	if (is_file($old_thumb)) unlink($old_thumb);
{'fraction_non_alphanumeric': 0.1906701221769715, 'fraction_numerical': 0.0033320992225101815, 'mean_word_length': 6.642149929278642, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, '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': '4412516', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1835, 'n_words': 355}