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.

    john1704 wrote:

    I looked up the function imagecreatefromstring, and it said it accepted a string that looks something like this:

    Not quite; you'll note from the example that it has to be base64 decoded first (the encoding is only there so that it would make the source code legible - no-one wants to look at raw binary!). This is why in your code you're base64-encoding the file contents and then immediately base64-decoding it again - $filestream3 is going to be bit-for-bit identical to $filestream and the base64 stuff serves no purpose.

    But here's the question: what's a ".bin" file? The ".bin" extension tends to be used to represent arbitrary binary data when the person who created it couldn't think of a better name. Certainly I've never come across an image file format that uses that extension. Even if there is such an extension, there's no guarantee that the GD library that implements imagecreatefromstring will recognise it (apparently it recognises JPEG, PNG, GIF, WBMP, and GD2 files).

      Weedpacket;10889878 wrote:

      Not quite; you'll note from the example that it has to be base64 decoded first (the encoding is only there so that it would make the source code legible - no-one wants to look at raw binary!). This is why in your code you're base64-encoding the file contents and then immediately base64-decoding it again - $filestream3 is going to be bit-for-bit identical to $filestream and the base64 stuff serves no purpose.

      But here's the question: what's a ".bin" file? The ".bin" extension tends to be used to represent arbitrary binary data when the person who created it couldn't think of a better name. Certainly I've never come across an image file format that uses that extension. Even if there is such an extension, there's no guarantee that the GD library that implements imagecreatefromstring will recognise it (apparently it recognises JPEG, PNG, GIF, WBMP, and GD2 files).

      I have this application that saves image files with a .bin extension; they are also commonly used with cell phones. I just thought it would be a cool little app where people can convert their phone pictures/video game screenshots into .jpg or .png images.

      So, you say that GD mostly likely will not recognize the binary data? I have a source file written in C for this kind of program, but I thought it would be cool to have an online version (I don't understand the C source file, anyway). I guess I'll have to do a bit more research on this.

        Is this really a conversion? If I read it right your image is actually never a "bin" file, you just name it that way. If so that is why the process fails.

        If you really do have an image and are converting it through another process to bin, why can't you just use the same process in reverse plus a few alternatives to pick up the other commonly used picture formats (like Photoshop or other image editing software does)?

        Here is a good thread on doing it with VB, a solid start regardless of you language of choice. Google seems a strong resource for cracking this conversion bits.

        http://www.vbforums.com/showthread.php?t=514815

          john1704 wrote:

          I have this application that saves image files with a .bin extension; they are also commonly used with cell phones.

          Doesn't really say what format they are 🙂 Besides, my cellphone uses JPEG and GIF for its images.

          Knowing which application this is might help, as might attaching an example of such a file here. Then we'd have a better idea of what these files actually are.

            Write a Reply...