I am trying to display an image linked to a user that is logged in based on a Joomla site.
The PHP code I am using is as follows:
<?php
$user = JFactory::getUser();
$username = $user->username;
echo '<img src="$user->picture" />'
;
?>
During my testing and troubleshooting, I tested without the image source tag and I received the call back from my database table labeled Images/LOGO.jpg which displayed as a string on my front end.
when I use the Image source again and replace $user->picture with the same string from the DB column, the image returns 100%.
By leaving the code as per above the page loads successfully but no image is returned.
Thanks,
The Bug is quite obvious....echo '<img src="$user->picture" />'
Did you notice anything unusual in the Snippet above?
Here is a Hint: Try this: <?php $var="test"; echo '$var'; ?>
What did you get?
Sure you got it all back as is: VERBATIM - $var
instead of the expected test.
So here's the Catch:
Any variable within Single Quotes in PHP would NOT expand/evaluate to a Value...
You'd just get the Variable back: Verbatim
Rather do it like so:
<?php
$user = JFactory::getUser();
$username = $user->username;
// NOTICE THE DOUBLE QUOTES SURROUNDING THE "<img... />" TAG
// THOUGH NOT SO IMPORTANT, BUT NOTICE ALSO THE BRACES AROUND
// {$user->picture}
echo "<img src='{$user->picture}' />";
By the way; in Joomla, you could get the User-Avatar like so:
<?php
// USE jimport TO PULL IN THE PROFILE PICTURE CLASS
jimport('profilepicture.profilepicture');
// GET THE USER OBJECT LIKE YOU ALREADY DID
$user = JFactory::getUser();
// CREATE A NEW INSTANCE OF THE ProfilePicture CLASS PASSING IT THE USER ID
$userPix = new ProfilePicture($user->get('id'));
// ECHO OUT THE ENTIRE HTML FOR THE USER PROFILE PICTURE
echo $userPix->toHTML();
Cheers & Good-Luck, Mate! 💪☝️✌️