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!

hakre
  • 193,403
  • 52
  • 435
  • 836
Omar Mir
  • 1,500
  • 1
  • 19
  • 39

1 Answers1

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();
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • here $imgdata is resource can we apply base64_decode to resouce i want it that way. – yourkishore Jan 03 '14 at 13:22
  • @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. – Michael Berkowski Jan 03 '14 at 14:21
  • Its clarified mate. my question is when imagecopymerge(stuff) will return a resource right ? how do we conver that resource to base64_decode. – yourkishore Jan 03 '14 at 15:25
  • Ah, I think [you want this then](http://stackoverflow.com/questions/1206884/php-gd-how-to-get-imagedata-as-binary-string) 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. – Michael Berkowski Jan 03 '14 at 15:28