asp正则html的图片,对图自动缩放大小
2018-09-06 12:13
  下面这个是比较不错的一个
复制代码 代码如下:
FunctionFormatImg2(content)
dimre 
Setre=newRegExp 
re.IgnoreCase=true 
re.Global=True 
re.Pattern=(script) 
Content=re.Replace(Content,script) 
re.Pattern=<img.[^>]*src(=)(.[^>]*)> 
Content=re.replace(Content,<imgsrc=$2style=cursor:pointeralt=点此在新窗口浏览图片onclick=javascript:window.open(this.src);onload=javascript:resizepic(this)border=0/>) 
setre=nothing 
FormatImg=content 
endfunction
上面有点不好的就是对于图片中的宽度和高度都不存在了
复制代码 代码如下: 
Functiongetphoto(strHTML) 
DimobjRegExp,Match,Matches 
SetobjRegExp=NewRegexp 
objRegExp.IgnoreCase=True 
objRegExp.Global=True 
objRegExp.Pattern=<img.+?> 
tp= 
SetMatches=objRegExp.Execute(strHTML) 
ForEachMatchinMatches 
tp=tp&Match.value 
exitfor 
Next 
getphoto=tp 
SetobjRegExp=Nothing 
EndFunction 
下面的代码时进行图片按比例缩放 
复制代码 代码如下: 
functionResizeImage(imageid,limitWidth,limitHeight) 
{ 
varimage=newImage(); 
image.src=imageid.src; 
if(image.width<=0&&image.height<=0)return; 
if(image.width/image.height>=limitWidth/limitHeight) 
{ 
if(image.width>limitWidth) 
{ 
imageid.width=limitWidth; 
imageid.height=(image.height*limitWidth)/image.width; 
} 
} 
elseif(image.height>limitHeight) 
{ 
imageid.height=limitHeight; 
imageid.width=(image.width*limitHeight)/image.height; 
} 
if(imageid.parentElement.tagName!=A) 
{ 
imageid.onclick=function(){window.open(this.src);} 
imageid.style.cursor=hand; 
} 
} 
window.onload=InitImages; 
functionInitImages() 
{ 
//图片的约束宽度和高度 
varmaxWidth=100; 
varmaxHeight=100; 
varimgs=document.getElementsByTagName(img); 
for(vari=0;i<imgs.length;i++) 
{ 
varimg=imgs; 
if(img.width>maxWidthimg.height>maxHeight) 
ResizeImage(img,maxWidth,maxHeight); 
} 
}