2013-04-15 20:37
浏览 30


I'm attempting to take the ImageResolver plugin and adapt it to work with a php array.

Stripping the code to this returns the image without a form:


    var url = $('#url').val();
    ImageResolver.resolve(url, function(image){
        if (image) {
            $('#result').html('<img src="' + image + '" alt="">');
        } else {
            $('#result').html('<h2>No image found</h2>');


I want to adapt it to work within a php foreach loop. results would be replaced on the next class='result' div. IE: after the page has loaded the urls from the query, the function will parse the url and return image link if one is found. I'm guessing I need to use (each) or this(), but I can't figure it out.

can someone point me in the right direction?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • duandanbeng1829 2013-04-16 00:12
            <script src="ImageResolver/URI.min.js"></script>
            <script src="ImageResolver/ImageResolver.js"></script>
            $javascriptarray = 'var urls = [';
            foreach (array('http://www.apple.com/','http://github.com/','http://www.test.com/') as $url) 
                if ($counter++ > 0) $javascriptarray .= ',';
                $javascriptarray .= '"'.$url.'"';
            $javascriptarray .= '];';   
    //The ImageResolver will try all the resolvers one after the other
    //in the order of their registration
    //Resolvers that guess the image URL
    ImageResolver.register(new FileExtensionResolver());
    ImageResolver.register(new ImgurPageResolver());
    ImageResolver.register(new NineGagResolver());
    ImageResolver.register(new InstagramResolver());
    //Resolvers that need extra ajax requests
    ImageResolver.register(new ImgurAlbumResolver());
    ImageResolver.register(new OpengraphResolver());
    ImageResolver.register(new WebpageResolver());
    //Some jQuery code to make the demo work
    //Use a crossdomain proxy (required by some plugins)
    $.ajaxPrefilter('text', function(options) {
        options.url = "http://furious-stream-4406.herokuapp.com?src=" + encodeURIComponent(options.url);
    var length = urls.length,
    url = null;
        for (var i = 0; i < length; i++) {
                url = urls[i];
                ImageResolver.resolve(url, function(image){
                if (image) {
                    $('#result').append('<img src="' + image + '" alt=""><br>');
                } else {
                    $('#result').append('<h2>No image</h2>');
                    //$('#result').append('<h2>No image found for ' + url + '</h2>');

    Watch out cause ImageResolver.resolve() works asynchrone you can get unexpected results. Call ImageResolver.resolve() again before the previous call has finished will change url in $('#result').append('<h2>No image found for ' + url + '</h2>'); to the url of your last call by example. To prevent this you need to initialize a new Resolver in the for-loop. see: Javascript prototypes and instance creation

    解决 无用
    打赏 举报

相关推荐 更多相似问题