О компании Менеджмент Переводы Программирование Робототехника Все проекты Контакты
Админка
пожалуйста подождите

// returns the HTML-formatted string with images which width
// is bigger, than the max value, specified, replaced with
// <а href="to full image"><img width='maxwidth' height='proportional' src='to full image' /></а>
// the smaller images are left unchanged
function smallDownImages( input, maxImageWidth )
// searching for images in the source
var inputLeft = input;
var output = "";
while ( true )
{
// searching for the img tag start
var leftI = inputLeft.search( /<img[^>] >/mi );
// checking, if there is something to do
if ( leftI < 0 )
{
output = inputLeft;
break;
};

// adding the part before the image to the output
if ( leftI > 0 )
output = inputLeft.slice( 0, leftI-1 );

// the part of the input, untreated is:
inputLeft = inputLeft.slice( leftI );

// searching for the end of the image
var rightI = inputLeft.search( />/mi );
// checking, if there is a error in tags
if ( rightI < 0 )
{
output = inputLeft;
break;
};

// obtaining the image tag properties
var imageTag = inputLeft.slice( 0, rightI 1 );
var width = parseInt( imageTag.match( /width\s*=\s*[\'\"](\d )/mi )[1] );
var height = parseInt( imageTag.match( /height\s*=\s*[\'\"](\d )/mi )[1] );
var src = imageTag.match( /src\s*=\s*[\'\"]([^\'\"] )/mi )[1];
// scaling, if required
if ( width > maxImageWidth )
{
var scale = 0.5 maxImageWidth / width - 0.5;
width = Math.floor( scale * width 0.5 );
height = Math.floor( scale * height 0.5 );
var imageTag = "<a href=\"" src "\">"
imageTag.replace( /(width\s*=\s*[\'\"])(\d )/mi, "$1" width )
.replace( /(height\s*=\s*[\'\"])(\d )/mi, "$1" height )
"";
};
// and adding to the output
output = imageTag;

// the part of the input, untreated is:
inputLeft = inputLeft.slice( rightI 1 );
};

// done
return output;
}
 
 
 
Языки
Темы
Copyright © 1999 — 2023
Зетка Интерактив