1

I have a program that convert image file to binary and also it converts the binary data to image file . I have done the first, I could convert the image file to binary . But the second one not done yet.

How to convert and save binary data to image file

I am checking this in PHP .Please help me

2
  • What do you mean by "binary"? Can you describe your problem little better? Commented Jan 16, 2012 at 10:39
  • I've used file_get_contents and convert it to string Commented Jan 16, 2012 at 11:08

3 Answers 3

4

Try the imagecreatefromstring method, which is documented here.

Sign up to request clarification or add additional context in comments.

5 Comments

"It Works!" . One more question Can I convert video files same in the way .How can I achieve that ?
@Rinto what the mean by convert video files? string to video ?
"Thanks". Yes the same way I used for images(string to image). I am getting videos as string data from the other non PHP application. I need to save it as video files in my server and then stream it in video player. Any ideas ?
@Rinto there is no direct function in php to do this? for this you have to search for some 3rd party libraries? I suggest you to make a new question for this to get help from other experts
why are you quoting yourselv?
2

You can use below code with option of saving an image and resizing saved png image without losing its transparent/white effect :

$data = 'binary data of image';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
// assign new width/height for resize purpose
$newwidth = $newheight = 50;
// Create a new image from the image stream in the string
$thumb = imagecreatetruecolor($newwidth, $newheight); 

if ($im !== false) {

    // Select the HTTP-Header for the selected filetype 
    #header('Content-Type: image/png'); // uncomment this code to display image in browser

    // alter or save the image  
    $fileName = $_SERVER['DOCUMENT_ROOT'].'server location to store image/'.date('ymdhis').'.png'; // path to png image
    imagealphablending($im, false); // setting alpha blending on
    imagesavealpha($im, true); // save alphablending setting (important)

    // Generate image and print it
    $resp = imagepng($im, $fileName);

    // resizing png file
    imagealphablending($thumb, false); // setting alpha blending on
    imagesavealpha($thumb, true); // save alphablending setting (important)

    $source = imagecreatefrompng($fileName); // open image
    imagealphablending($source, true); // setting alpha blending on

    list($width, $height, $type, $attr) = getimagesize($fileName);
    #echo '<br>' . $width . '-' . $height . '-' . $type . '-' . $attr . '<br>';

    imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    $newFilename = $_SERVER['DOCUMENT_ROOT'].'server location to store image/resize_'.date('ymdhis').'.png';
    $resp = imagepng($thumb,$newFilename);

    // frees image from memory
    imagedestroy($im);
    imagedestroy($thumb);

}
else {
    echo 'An error occurred.';
}

Same way, we can do for JPEG, JPG and GIF format of images.

Hope it helps to people here!

Comments

0

You would convert your binary stream (of i assume r/g/b values) back into decimal notation and write the pixel to the image you are creating using imagesetpixel().

One of the few reasons to change your image data into a binary stream is to hide additional data in the lower order of binary bits during steganography - altering the color of each pixel at a level not discernable to the human eye.

Check http://www.php.net/manual/en/function.imagesetpixel.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.