Created
July 9, 2025 05:42
-
-
Save asifulmamun/35d30cf214403ea8863395c6cf486991 to your computer and use it in GitHub Desktop.
Huffman Coding | Greedy Algorithm | DSA | PHP
This file contains hidden or 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 | |
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['image'])) { | |
$image = $_FILES['image']['tmp_name']; | |
$imageData = file_get_contents($image); | |
$base64 = base64_encode($imageData); | |
$data = $base64; | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet"> | |
</head> | |
<body class="bg-gray-100 text-gray-900"> | |
<form method="post" enctype="multipart/form-data" class="flex flex-col items-center p-4 bg-white shadow-md rounded-lg"> | |
<input type="file" name="image" accept="image/*" class="mb-4 p-2 border border-gray-300 rounded"> | |
<button type="submit" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-700">Upload Image</button> | |
</form> | |
<?php | |
$data = gzcompress($base64); | |
?> | |
<?php | |
class HuffmanNode { | |
public $char; | |
public $freq; | |
public $left; | |
public $right; | |
public function __construct($char, $freq, $left = null, $right = null) { | |
$this->char = $char; | |
$this->freq = $freq; | |
$this->left = $left; | |
$this->right = $right; | |
} | |
} | |
// frequency count | |
function calculateFrequency($text) { | |
return array_count_values(str_split($text)); | |
} | |
// Huffman Tree create | |
function buildHuffmanTree($freq) { | |
$heap = new SplPriorityQueue(); | |
foreach ($freq as $char => $f) { | |
$heap->insert(new HuffmanNode($char, $f), -$f); | |
} | |
while ($heap->count() > 1) { | |
$left = $heap->extract(); | |
$right = $heap->extract(); | |
$newNode = new HuffmanNode(null, $left->freq + $right->freq, $left, $right); | |
$heap->insert($newNode, -$newNode->freq); | |
} | |
return $heap->extract(); | |
} | |
// Huffman code generation | |
function generateCodes($node, $prefix = "", &$codes = []) { | |
if ($node == null) return; | |
if ($node->char !== null) { | |
$codes[$node->char] = $prefix; | |
} | |
generateCodes($node->left, $prefix . "0", $codes); | |
generateCodes($node->right, $prefix . "1", $codes); | |
} | |
// encode | |
function encode($text, $codes) { | |
$encodedText = ""; | |
foreach (str_split($text) as $char) { | |
$encodedText .= $codes[$char]; | |
} | |
return $encodedText; | |
} | |
// decode | |
function decode($encodedText, $tree) { | |
$decodedText = ""; | |
$node = $tree; | |
foreach (str_split($encodedText) as $bit) { | |
$node = ($bit == '0') ? $node->left : $node->right; | |
if ($node->char !== null) { | |
$decodedText .= $node->char; | |
$node = $tree; | |
} | |
} | |
return $decodedText; | |
} | |
// test | |
$text = $data; | |
$freq = calculateFrequency($text); | |
$tree = buildHuffmanTree($freq); | |
$codes = []; | |
generateCodes($tree, "", $codes); | |
$encodedText = encode($text, $codes); | |
$decodedText = decode($encodedText, $tree); | |
// echo "Original Text: $text\n"; | |
// echo "Encoded Text: $encodedText\n"; | |
// echo "Decoded Text: $decodedText\n"; | |
?> | |
<div class="flex flex-col items-center p-4 bg-white shadow-md rounded-lg"> | |
<div class="break-words"> | |
<?php echo $encodedText; ?> | |
</div> | |
</div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment