10

I've set up a system to display everyone's name, email address and phone number from Active Directory however I can't get the 'thumbailPhoto' to work.

I have searched around on the internet but haven't been able to find if this is possible or at the very least what format is returned from Active Directory.

I am currently using the adldap class so if it is possible to use this that would be ideal.

Thanks in advance.

Edit:

I can retrieve the data in the thumbnailPhoto attribute and if I dump them straight to the browser I get something like this:

ÿØÿàJFIFððÿá PExifII*bh~†(2Ži‡¢XCanonCanon EOS 5D Mark IIIðð2013:05:19 17:35:31š‚à‚è"ˆ'ˆ 0230ð’ ’ ’ (’0’8’ ’ ’@‘’11’’11 0100 ÿÿ¢H¢P¢¤¤¤¤ 2013:04:17 11:44:522013:04:17 11:44:52H¹o@B¬ † è»dnäWµ˜:̦®(¶’ HHÿØÿàJFIFÿÛC $.' ",#(7),01444'9=82<.342ÿÛC 2!!22222222222222222222222222222222222222222222222222ÿÀ–d"ÿÄ ÿĵ}!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ ÿĵw!1AQaq"2B‘¡±Á #3RðbrÑ $4á%ñ&'()

That isn't all of it but it is a very long string, I am presuming is some sort of binary string?

Sean
  • 269
  • 1
  • 6
  • 16
  • I assume that you can retrieve the content of the 'thumbnailPhoto'. Have you analysed what image-format the content is? – heiglandreas Jun 05 '13 at 14:02
  • I have added a snippet of what is returned. In regards to image format I don't know what Active Directory does to the image when a user adds one. I don't know if it remains in the format they upload or if it is processed in some way. – Sean Jun 05 '13 at 15:01

3 Answers3

24

This seems to be a JPEG-File, so you should be able to send that data together with the appropriate mime-type to the browser. It should be possible to output that image with something like:

<img src="data:image/jpeg;base64,<?php echo base64_encode($imageString); ?>"/>

But it might also be possible to save files of any image format into that thumbnailPhoto attribute. Therefore, I would put the content into a temporary file that will then be served directly from the server. You will need to pass the file through finfo to get the correct mime-type.

So you might do something like this:

$tempFile = tempnam(sys_get_temp_dir(), 'image');
file_put_contents($tempFile, $imageString);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime  = explode(';', $finfo->file($tempFile));
echo '<img src="data:' . $mime[0] . ';base64,' . base64_encode($imageString) . '"/>';
simlev
  • 919
  • 2
  • 12
  • 26
heiglandreas
  • 3,803
  • 1
  • 17
  • 23
  • Thank you so much for this, I tweaked the code slightly as I knew it would be an image but this definitely solved my problem and works great. – Sean Jun 06 '13 at 08:53
  • For what its worth, I used this solution to resolve mime type, but was able to skip writing to file by using `$finfo->buffer($binary)` – Pete Apr 04 '18 at 22:48
6

Try the code below. It is an adaptation of the answer above.

<?php $result = ldap_search($ad , $dn , $filter, $attributes); $aduser = ldap_get_attributes($ad, ldap_first_entry($ad,$result)); ?>

<img src="data:image/jpeg;base64,<?php echo base64_encode($aduser['thumbnailPhoto'][0]); ?>" />
Tomiwa Adefokun
  • 287
  • 4
  • 7
0

when you store the photo data into ldap i.e. "jpegphoto" attribute it should be done by using encode base64. Reading the attribute is already decoded on fly. Hence I would use something less modified code

$tempFile = tempnam(sys_get_temp_dir(), 'image');
file_put_contents($tempFile, $imageString);
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime  = explode(';', $finfo->file($tempFile));
header("Content-Type: $mime");
echo $imageString;

This is direct php image write example you can directly use it under tag img i.e.

<img src="example.php" />
rmil
  • 76
  • 2