Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular js - isImage( ) - check if it's image by url

Is it possible to check if by a given url the image exists and it's an image resource ?

for example:

angular.isImage('http://asd.com/asd/asd.jpg')

Or it's just a stuff for the server side ?

NO JQUERY please i'm not using it

like image 975
itsme Avatar asked Mar 15 '14 11:03

itsme


People also ask

How do you check if a URL is an image?

To check if a url is an image, call the test() method on a regular expression that matches an image extension at the end of a string, e.g. . png or . jpg . The test() method will check if the url ends with an image extension and will return true if it does.

How can I check if image exists in assets folder using angular?

function imageExists(url, callback) { var img = new Image(); img. onload = function() { callback(true); }; img. onerror = function() { callback(false); }; img. src = url; } // Sample usage var imageUrl = 'http://www.google.com/images/srpr/nav_logo14.png'; imageExists(imageUrl, function(exists) { console.

How do I know if Angularjs is working?

I just opened the console by hitting F12 (or right-clicking and selecting Inspect element , then typed in angular . If you get the message Object {version: Object, callbacks: Object} , angular is loaded. If you get the message Uncaught ReferenceError: angular is not defined , angular is not loaded.


2 Answers

I think the best javascript approach would be to use HTMLImageElement object with deferred object:

function isImage(src) {

    var deferred = $q.defer();

    var image = new Image();
    image.onerror = function() {
        deferred.resolve(false);
    };
    image.onload = function() {
        deferred.resolve(true);
    };
    image.src = src;

    return deferred.promise;
}

Usage:

isImage('http://asd.com/asd/asd.jpg').then(function(test) {
    console.log(test);
});

Using HTMLImageElement gives you some benefits: not only it tests that the file is downloadable but also it is valid image resource that can be displayed by img tag.

I wrapped this code in simple service to make a test and it seems to work:

app.controller('MainCtrl', function($scope, Utils) {
    $scope.test = function() {
        Utils.isImage($scope.source).then(function(result) {
            $scope.result = result;
        });
    };
});

app.factory('Utils', function($q) {
    return {
        isImage: function(src) {
            // ... above code for isImage function
        }
    };
});

Demo: http://plnkr.co/edit/u5F6FfO3dEkNSMYV1amo?p=preview

like image 65
dfsq Avatar answered Oct 19 '22 03:10

dfsq


You can use ng-src

<img ng-src="" />

Another way is you check if the it exists using the http module.

var app = angular.module('myapp', []).run(function($http){
  $http.get('http://asd.com/asd/asd.jpg',
    //success
    function(data){

    };
});

Update:

HTML

<div ng-controller="Ctrl">
       <img ng-src="{{src}}" isImage />
</div>

JS

var app = angular.module('app', []);

app.directive('isImage', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            element.bind('load', function() {
                alert('image is loaded');
            });
        }
    };
});

app.controller('Ctrl', function($scope) {
    $scope.src ="http://asd.com/asd/asd.jpg";
});
like image 33
Thalaivar Avatar answered Oct 19 '22 03:10

Thalaivar