Blogger,如何更改“随机”帖子的缩略图大小?

时间:2019-02-22 20:11:09

标签: javascript html widget blogger

Blogger ,如何调整随机发布

的此外部图片的大小
randompoststhumb = entry.content.$t.match(/\<img.+src\=(?:\"|\')(.+?)(?:\"|\')(?:.+?)\>/)[1];

喜欢在博客includable expr:下使用' resizeImage '功能:

<b:includable id='feedPostImage'>
  <div class='snippet-thumbnail-container'>
	  <a expr:href='data:post.link ?: data:post.url'>
		<img expr:src='resizeImage(data:post.featuredImage, 280, &quot;280:152&quot;)'/>
	  </a>
  </div>
</b:includable>

<div id='random-posts'>
<script type='text/javaScript'>
var randomposts_number = 10;
var randomposts_chars = 0;
var randomposts_details = 0;
var randomposts_comments = 'Comments';
var randomposts_commentsd = 'Comments Disabled';
var randomposts_current = [];
var total_randomposts = 0;
var randomposts_current = new Array(randomposts_number);

function randomposts(json) {
    total_randomposts = json.feed.openSearch$totalResults.$t
}
document.write('<script type=\"text/javascript\" src=\"/feeds/posts/default?alt=json-in-script&max-results=0&callback=randomposts\"><\/script>');

function getvalue() {
    for (var i = 0; i < randomposts_number; i++) {
        var found = false;
        var rndValue = get_random();
        for (var j = 0; j < randomposts_current.length; j++) {
            if (randomposts_current[j] == rndValue) {
                found = true;
                break
            }
        };
        if (found) {
            i--
        } else {
            randomposts_current[i] = rndValue
        }
    }
};

function get_random() {
    var ranNum = 1 + Math.round(Math.random() * (total_randomposts - 1));
    return ranNum
};
</script>
<script type='text/javaScript'> 
function random_posts(json) {
    for (var i = 0; i < randomposts_number; i++) {
      var entry = json.feed.entry[i];
      var randompoststitle = entry.title.$t;
		
        if ('content' in entry) {
            var randompostsnippet = entry.content.$t
        } else {
            if ('summary' in entry) {
                var randompostsnippet = entry.summary.$t
            } else {
                var randompostsnippet = "";
            }
        }
        randompostsnippet = randompostsnippet.replace(/<[^>]*>/g, "");
        if (randompostsnippet.length < randomposts_chars) {
            var randomposts_snippet = '<div class="random-summary">' + randompostsnippet + '</div>';
        } else {
            randompostsnippet = randompostsnippet.substring(0, randomposts_chars);
            var whitespace = randompostsnippet.lastIndexOf(" ");
            var randomposts_snippet = randompostsnippet.substring(0, whitespace) + "&#133;";
            randomposts_snippet = '<div class="random-summary">' + randomposts_snippet + '</div>';
        }
	  
        for (var j = 0; j < entry.link.length; j++) {
            if ('thr$total' in entry) {
                var randomposts_commentsnum = entry.thr$total.$t + ' ' + randomposts_comments
            } else {
                randomposts_commentsnum = randomposts_commentsd
            } 
			if (entry.link[j].rel == 'alternate') {
                var randompostsurl = entry.link[j].href;
                var randomposts_date = entry.published.$t;
				if ('media$thumbnail' in entry) 
				{
					var randompoststhumb = entry.media$thumbnail.url
				} 
				else if ( entry.content.$t.match(/\<img.+src\=(?:\"|\')(.+?)(?:\"|\')(?:.+?)\>/))
				{
				  // how to resize this external image ?
				     randompoststhumb = entry.content.$t.match(/\<img.+src\=(?:\"|\')(.+?)(?:\"|\')(?:.+?)\>/)[1];
				  // like using 'resizeImage' function in blogger includable expr:
				  // randompoststhumb = resizeImage(randompoststhumb, 280, &quot;280:152&quot;)
				} else {
					randompoststhumb = "http://3.bp.blogspot.com/-5SoVe1K6JSk/Utl0OOmucAI/AAAAAAAAF6E/hQghgD_EJdQ/s1600/no_thumb.png"
				}
            }
        }
        document.write('<div class="randomPost">');
        document.write('<a href="' + randompostsurl + '" rel="nofollow">' + '<span class="imgf"><span src="' + randompoststhumb + '"></span></span>' + randompoststitle + '</a>');
        if (randomposts_details) {
            document.write('<span><div class="random-info">' + randomposts_date.substring(8, 10) + '.' + randomposts_date.substring(5, 7) + '.' + randomposts_date.substring(0, 4) + ' - ' + randomposts_commentsnum) + '</div></span>'
        }
        if (randomposts_chars) {
			document.write(randomposts_snippet);
        }
		document.write('</div>');
    }
}
getvalue();
for (var i = 0; i < randomposts_number; i++) {
    document.write('<script type=\"text/javascript\" src=\"/feeds/posts/default?alt=json-in-script&start-index=' + randomposts_current[i] + '&max-results=1&callback=random_posts\"><\/script>')
}
</script>
</div>

1 个答案:

答案 0 :(得分:1)

在这种情况下,使用Google的即时图像调整大小服务将是更好的方法。正如https://stackoverflow.com/users/333272/carlo-zottmann中的Carlo Zottmann(https://czm.io/posts/2013/04/google-image-resizer/)所述,API如下-

  

基本URL

     

https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy

     

参数:

     
      
  • URL:原始图像URL

  •   
  • 容器:必须为“焦点”

  •   
  • 刷新:将其缓存到G的服务器上的时间(以秒为单位)

  •   
  • resize_w:宽度(以像素为单位)

  •   
  • resize_h:以像素为单位的高度

  •   
  • resize_ *个参数或仅一个。

  •   

您将需要更改以下代码块-

// how to resize this external image ?
randompoststhumb = entry.content.$t.match(/\<img.+src\=(?:\"|\')(.+?)(?:\"|\')(?:.+?)\>/)[1];
// like using 'resizeImage' function in blogger includable expr:

与-

// how to resize this external image ?
randompoststhumb = entry.content.$t.match(/\<img.+src\=(?:\"|\')(.+?)(?:\"|\')(?:.+?)\>/)[1];
randompoststhumb = 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?url=' + randompoststhumb + '&container=focus&resize_w=300&resize_h=300&refresh=2592000';
// like using 'resizeImage' function in blogger includable expr:

由于在这种情况下我们不能使用resizeImage运算符,因此您也可以使用相同的API来调整上传到Blogger的图像的大小。您将需要替换-

var randompoststhumb = entry.media$thumbnail.url

与-

var randompoststhumb = entry.media$thumbnail.url.replace("s72-c", "s0").replace("/default.jpg","/hqdefault.jpg");
randompoststhumb = 'https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?url=' + randompoststhumb + '&container=focus&resize_w=300&resize_h=300&refresh=2592000';

由于这是一个未公开的API,因此我们无法确定其将来的可用性(由于该事实与Google+社交网络有关,该社交网络将于2019年4月关闭)。即时调整图像大小的服务有多种选择,既有收费的(如https://kraken.io/docs/image-resizing)又有自托管的(如https://github.com/jimmynicol/image-resizer