Voici un petit bout de code jQuery bien utile pour convertir tout lien pointant vers une image à une miniature de celle-ci pointant vers l’originale. Très utile pour un blogue ou un forum!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
//set image max width
imgMaxWidth = 150;
//define regex pattern to detect images
regexImg = /^http://(.)+.(jpg|jpeg|gif|png)$/gi;
//parse links
$('DIV.activity a').each(function(){
//if a link points to an image
if($(this).attr('href').match(regexImg)) {
//add classes
$(this).addClass('fetchedImgLink');
//edit target
$(this).attr('target', '_blank');
//show image instead of text
$(this).html("< img class='fetchedImg' src='" + $(this).attr('href') + "' />");
//check for size
var currentImg = $(this).find('img');
var iw = currentImg.width();
var ih = currentImg.height();
var ratio = (ih!=0) ? iw/ih : 1;
//resize if needed
if(iw > imgMaxWidth) {
currentImg.width(imgMaxWidth);
currentImg.height(ih - ((iw-imgMaxWidth) / ratio));
}
}
});















