asp正則html的圖片,對圖自動縮放大小

來源:互聯網
上載者:User

下面這個是比較不錯的一個 複製代碼 代碼如下:Function FormatImg2(content)
dim re
Set re=new RegExp
re.IgnoreCase =true
re.Global=True
re.Pattern="(script)"
Content=re.Replace(Content,"script")
re.Pattern="<img.[^>]*src(=| )(.[^>]*)>"
Content=re.replace(Content,"<img src=$2 style=""cursor: pointer"" alt=""點此在新視窗瀏覽圖片"" onclick=""javascript:window.open(this.src);"" onload=""javascript:resizepic(this)"" border=""0""/>")
set re = nothing
FormatImg = content
end function

上面有點不好的就是對於圖片中的寬度和高度都不存在了複製代碼 代碼如下:Function getphoto(strHTML)
Dim objRegExp, Match, Matches
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "<img.+?>"
tp=""
Set Matches = objRegExp.Execute(strHTML)
For Each Match in Matches
tp=tp & Match.value
exit for
Next
getphoto=tp
Set objRegExp = Nothing
End Function

下面的代碼時進行圖片按比例縮放 複製代碼 代碼如下:function ResizeImage(imageid,limitWidth,limitHeight)
{
var image = new Image();
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;
}
}
else if(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;
function InitImages()
{
//圖片的約束寬度和高度
var maxWidth = 100;
var maxHeight = 100;
var imgs = document.getElementsByTagName("img");
for(var i=0; i < imgs.length; i++)
{
var img = imgs;
if(img.width>maxWidth||img.height>maxHeight)
ResizeImage(img, maxWidth, maxHeight);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.