Skip to content
PHP Logo

What are some alternative methods or best practices for efficiently displaying images from LDAP in PHP?

Displaying images from LDAP in PHP can be done efficiently by retrieving the image data as a base64 encoded string from LDAP and then embedding it directly into the HTML image tag. This method eliminates the need to save the image as a file on the server and improves performance by reducing file I/O operations.

php
<?php
// Connect to LDAP server and retrieve image data
$ldapconn = ldap_connect("ldap.example.com");
$ldapbind = ldap_bind($ldapconn, "cn=admin,dc=example,dc=com", "password");
$result = ldap_search($ldapconn, "dc=example,dc=com", "(uid=johndoe)", array("thumbnailPhoto"));
$entry = ldap_get_entries($ldapconn, $result);

// Encode image data to base64
$imageData = $entry[0]["thumbnailphoto"][0];
$imageDataEncoded = base64_encode($imageData);

// Display image in HTML
echo '<img src="data:image/jpeg;base64,' . $imageDataEncoded . '" alt="User Image">';
?>