Last active
July 25, 2024 09:31
-
-
Save VIRUXE/e4ae25ecf876d571b70c8201af3ab77a to your computer and use it in GitHub Desktop.
Resize and/or Reduce image quality to fit hondacrx.co.uk PFP upload specs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MAX_WIDTH', 160); | |
define('MAX_HEIGHT', 160); | |
$output_name = ''; | |
$output_full_path = ''; | |
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['image'])) { | |
$image_path = $_FILES['image']['tmp_name']; | |
echo "<p>File name: " . $_FILES['image']['name'] . "</p>"; | |
echo "<p>Original file size: " . filesize($image_path) . " bytes</p>"; | |
try { | |
[$width, $height, $type] = getimagesize($image_path) ?: throw new Exception('Invalid image file.'); | |
$img = match ($type) { | |
IMAGETYPE_JPEG => imagecreatefromjpeg($image_path), | |
IMAGETYPE_PNG => imagecreatefrompng($image_path), | |
IMAGETYPE_GIF => imagecreatefromgif($image_path), | |
default => throw new Exception('Unsupported image type.'), | |
}; | |
// In case the uploaded image is larger than the max dimensions | |
// Ratio is maintained. | |
if ($width > MAX_WIDTH || $height > MAX_HEIGHT) { | |
$ratio = min(MAX_WIDTH / $width, MAX_HEIGHT / $height); | |
$new_width = round($width * $ratio); | |
$new_height = round($height * $ratio); | |
$new_img = imagecreatetruecolor($new_width, $new_height); | |
imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); | |
imagedestroy($img); | |
$img = $new_img; | |
} | |
$img_name = pathinfo($_FILES['image']['name'], PATHINFO_FILENAME); | |
$output_path = sys_get_temp_dir(); | |
$output_name = $img_name . '.jpg'; | |
$output_full_path = $output_path . DIRECTORY_SEPARATOR . $output_name; | |
// Reduce the quality to the point where it's necessary | |
$quality = 100; | |
while (true) { | |
ob_start(); | |
imagejpeg($img, null, $quality); | |
$image_data = ob_get_contents(); | |
ob_end_clean(); | |
$image_size = strlen($image_data); // We size the buffer directly | |
echo "<p>Current File size: " . $image_size . " bytes</p>"; | |
if ($image_size < 21 * 1024) { // 21KB limit | |
file_put_contents($output_full_path, $image_data); | |
echo "<p>$output_name successfully compressed to JPG.</p>"; | |
break; | |
} else { | |
$quality -= 5; | |
if ($quality < 0) { | |
echo "<p>Error: Could not compress image to less than 21KB.</p>"; | |
break; | |
} else { | |
echo "<p>Lowered quality to: $quality%</p>"; | |
} | |
} | |
} | |
imagedestroy($img); | |
} catch (Exception $e) { | |
echo "<p>Error: " . $e->getMessage() . "</p>"; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>hondacrx compliant pfp</title> | |
</head> | |
<body> | |
<?php | |
if (!empty($output_name) && file_exists($output_full_path)) { | |
echo '<img src="data:image/jpeg;base64,' . base64_encode(file_get_contents($output_full_path)) . '">'; | |
unlink($output_full_path); | |
} | |
?> | |
<form action="" method="post" enctype="multipart/form-data"> | |
<label for="image">Choose an image:</label> | |
<input type="file" name="image" id="image" accept="image/*" required> | |
<button type="submit">Upload</button> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment