2

I am connecting to Active-Directory and getting the thumbnailPhoto attribute successfully.

I have stored the file in the DB using Base64 encoding which makes the result look like:

/9j/4AAQSkZJRgABAQEAYABgAAD/4RHoRXhpZgAATU0AKgAAAAgABQEyAAIAAAAUAA ...

(Full Base64 encoded string: http://pastebin.com/zn2wDEmd)

Using a simple Base64 Decoder and decoding the string into a binary file and rename that to jpeg and open with an image viewer (here: Irfan View) I get the correct picture - see yourself:

Picture

How do I achieve this through PHP - I have tried using:

<?php 

$data = '/9j/4A...'; //The entire base64 string - gives an error in dreamweaver

$data = base64_decode($data);

$fileTmp = imagecreatefromstring($data);

$newImage = imagecreatefromjpeg($fileTmp);

if (!$newImage) {
    echo("<img src=".$newImage."/>");
} 

?>

I'm just getting a blank page!

1
  • You worry too much: echo '<img src="data:image/jpg;base64,', $base64, '">'; - demo: codepad.viper-7.com/JQ8tyd Commented Oct 20, 2012 at 15:22

1 Answer 1

1

Your problem is that imagecreatefromstring() doesn't return a file, but rather an image in memory that should be output with the correct headers.

$data = base64_decode($data);

// Create image resource from your data string
$imgdata = imagecreatefromstring($data);

if ($imgdata) {
  // Send JPEG headers
  header("Content-type: image/jpeg");
  // Output the image data
  imagejpeg($imgdata);

  // Clean up the resource
  imagedestroy($imgdata);
  exit();
}
Sign up to request clarification or add additional context in comments.

4 Comments

here $imgdata is resource can we apply base64_decode to resouce i want it that way.
@yourkishore It isn't clear what you are asking. You have $imgdata as a GD image resource? You cannot base64 encode that directly - you would need to return the binary string from it to encode. Post a question with your specific problem.
Its clarified mate. my question is when imagecopymerge(stuff) will return a resource right ? how do we conver that resource to base64_decode.
Ah, I think you want this then use imagejpeg() (or whatever format) in conjunction with PHP's output buffering to capture the binary string data, then base64_encode() it. There's nothing there to base64_decode() because it hasn't been encoded.

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.