Hello everyone (and please excuse me if I don't use the correct terminology),
I was attempting to make a PHP program that converts an uploaded .bin file to a .jpg or .png file. Problem is that I'm lost and confused. I came very close to solving the problem; I made a program that took the file and opened it and read it. Then, I used the base64_encode() function to encode it, then the base64_decode() function to decode it.
Here's the form:
<form action="ssbbconverter.php" method="POST" enctype="multipart/form-data">
<p>File: <input type="file" name="bin"></p>
<p>PNG: <input type="radio" name="type" value="png" CHECKED> - JPG: <input type="radio" name="type" value="jpg"></p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
Here's the program:
<?php
$type = $_POST['type'];
$file = $_FILES['bin']['tmp_name'];
$fp = fopen($file, "r");
$filestream = fread($fp, filesize($file));
$filestream2 = base64_encode($filestream);
$filestream3 = base64_decode($filestream2);
$stream = imagecreatefromstring($filestream);
if ($stream !== false) {
if ($type == 'png') {
header("Content-type: image/png");
imagepng($stream);
}
else if ($type == 'jpg') {
header("Content-type: image/jpg");
imagejpg($stream);
}
imagedestroy($stream);
}
else {
echo "An error occured.";
}
?>
I looked up the function imagecreatefromstring, and it said it accepted a string that looks something like this:
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
Interestingly, I printed out my string from the bin file (the variable $filestream2) and it looked similar as the example. I then decoded it like the example said, and tried to create an image from that string. Unfortunately, PHP says that "Data is not in a recognized format."
So, is it possible to even convert .bin files to .jpg and .png files in the first place? Can you convert the encoded to string so it would resemble a .png or .jpg file, if possible? If so, how would you do it?
Thanks.