How to use high resolution photos in text posts in Tumblr?

喜欢而已 提交于 2019-12-06 14:16:10

Use {block:Posts inlineMediaWidth="1280"} and {/block:Posts} instead of {block:Posts} and {/block:Posts} to get "1280px" wide images (when available) inside Text post body and Captions of other posts. The width you set also affects things like videos.

The reason you code did not work for inline images added via "HTML" editor; most probably because the class tmblr-full is only given to the figure containing the image uploaded via "Rich text" editor, Tumblr does that.

Note: You did not post the complete HTML of your posts. Here I am assuming the reason you are trying jQuery is because you are not aware of what I just said above.

Tumblr Theme Documentation

OK I think I have something working (although it is slightly hacky).

I am using this function:

var fullImg = $('.post img');
fullImg.each(function(){
    var img_url = $(this).attr("src");
    var normal_width = parseInt($(this).attr("width"));
    var new_url = img_url.replace(400, 1280);
    $(this).attr( "width", 1280);
    $(this).attr( "src", new_url);
});

I tested this in jsfiddle using my own tumblr, so I had to change and tweak a few things.

Explanation.

Firstly as we are calling the same selector several times I have cached mine in a variable which I can refer to later (quicker and more maintainable).

var fullImg = $('.post img');

Use a similar variable for your selector $('.tmblr-full img').

Then the each function iterates over every instance of .post img rather than replacing the content for the first one with the same image.

Also I am not using a hard coded width or height property in my theme, so I cannot get the image width property. Mine are resized to 400px, so I am passing that as the parameter to replace, but ideally this should be caught in a variable. I did try to do this on the src but I could not get it to work (using split and substring).

You also need to keep in mind that if no version of the 1280px image exists, this function will still run and attempt to replace the code, so you may need some sort of test in place to check the src attribute first. Something like

if(img_url.match('400')){ // then execute the function ...

Here is a jsfiddle: https://jsfiddle.net/lharby/pwgqgvac/

EDIT:

I've managed to store the image size in a variable so it should not matter if it is a different number.

var fullImg = $('.post img');
fullImg.each(function(){
    var img_url = $(this).attr("src");
    var normal_width = parseInt($(this).attr("width"));
    var url_removed = img_url.split(".")[3]; // split the url at the "." and get the 3rd index
    url_removed = url_removed.substr(length -3); // get the last 3 chars eg 400
    var new_url = img_url.replace(url_removed, 1280);
    $(this).attr( "width", 1280);
    $(this).attr( "src", new_url);
});

I have updated the fiddle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!