1

I want to display a user profile image if it exists; else show default font awesome icon. The profile picture name is an unique number and the extensions can be of following types (jpg, jpeg, png, gif and etc).

What i have done so far

<div ng-repeat="user in users">
    <p> {{user.name}} </p>
    <img ng-src="{{('/media/upload/' + user.id + '.jpg')}}" onerror="this.src='/media/upload/default.jpeg'" height="80px" width="80px"/>
</div>

Right now it only shows image with an ext .jpg.

How can i create a custom filter to check if image is available in following extensions (jpg, jpeg, png, gif)and show font awesome icon if it doesn't?

Any help and feedback are appreciated!

Thanks!

1

1 Answer 1

-1

You don't need any filter in order to achieve that, just make a default src:

<img ng-src="{{('/media/upload/' + user.id + '.jpg')}}" src="/media/upload/default.jpeg" height="80px" width="80px"/>

The ngSrc directive whould replace the source only if available

In order to test all possible extensions, I would write a directive:

app.directive('multiSrc', function($q) {
  return {
    restrict: 'A',
    scope: { user: '=' },
    link: function(scope, element) {

      console.log(scope.user);

      var image = new Image();
      var _ext = ['jpg', 'jpeg', 'png', 'gif'];
      function _loadExt(index) {
        if(index >= _ext.length) {
          return;
        }
        _tryLoading(_ext[index])
        .then(function(successImage) {
          element[0].src = successImage.src;
        }, function() {
         return _loadExt(index + 1);
        });
      }
      function _tryLoading(extension) {
        var _defer = $q.defer();
        image.src = '/media/upload/' + scope.user.id + '.' + extension;
        image.onerror = function() {
          _defer.reject();
        };
        image.onload = function() {
          _defer.resolve(angular.copy(image));
        }
        return _defer.promise;
      }
      return _loadExt(0);
   }
 };
});

Edit

This is showing one way to implement it. But it IS NOT best practice at all.

The way this should be done, is when the user upload an image, save the path in the database (within the user object usually), then just request the registered path.

Sign up to request clarification or add additional context in comments.

10 Comments

Well you're repeating me! How can i check if image exist in extensions such as: jpg, jpeg, png, gif and etc? The default src is not an optimal solutions!
Thanks Kobi! How to use this directive in template?
<img multi-src user="user" src="/media/upload/default.jpeg" height="80px" width="80px"/>
+ scope.user.id + can't read property of undefined! Pretty close! Thanks @Kobi Cohen
I don't know why it's undefined, here is a working plunkr embed.plnkr.co/hbMprRxYk8xCdgeSAvjH. also, I've edited the answer after debugging the code. take a look
|

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.