本文word版下載:http://www.web126.com/upload/edit_pic/20090806101735.doc
本文txt版下載:http://www.web126.com/upload/edit_pic/20090806102141.txt
一次做新聞系統時(shí)因為要固定新聞頁(yè)圖片的大小并且要加鏈接,因此就去網(wǎng)上找,結果還真找到了,首頁(yè)把新頁(yè)實(shí)現代碼貼出來(lái):
$as_message = preg_replace(\'/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i\', "<a href=\"$1\" target=_blank title=點(diǎn)擊放大圖片><img src=\"$1\" border=0 onload=\"javascript:if(this.width>screen.width-600)this.width=screen.width-600\" onmousewheel=\"return bbimg(this)\" ></a>", $as_message);
$as_message = preg_replace(\'/<IMG.+SRC=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i\', "<a href=\"$1\" target=_blank title=點(diǎn)擊放大圖片><img src=\"$1\" border=0 onload=\"javascript:if(this.width>screen.width-600)this.width=screen.width-600\" onmousewheel=\"return bbimg(this)\" ></a>", $as_message);
另一種固定圖片的js方法:
<table width="90%" border="0" cellpadding="0" cellspacing="0" id="abc">
<tr>
<td> </td>
</tr>
</table>
<script>
var abc=document.getElementById("abc");
var imgs=abc.getElementsByTagName("img");
for (var i=0,g;g=imgs[i];i++)
g.onload=function(){if (this.width>600){this.width=600}else{if (this.height>600)this.height=600}}
</script>
參考如下:
因有一項目新聞發(fā)布系統,數據庫內容字段中存儲的是原圖的路徑(當然還有其他文字內容啦,內容里插圖時(shí),存的是圖片路徑),但前臺想使用縮略圖,琢磨1小時(shí)余,得到以下結果,可解決問(wèn)題(取img標簽會(huì )了,取別的標簽任意屬性自然也會(huì )了):
<?php
/*正則取圖片img標記中的任意屬性*/
$word = \'<p height="22" align="cenetr">111 22</p> <img height="60" src="/upload/images/aaa.jpg" width=100 style=><div style="float:left;">中國人</div>\';
//取width
preg_match(\'/<img.+(width=\"?\d*\"?).+>/i\',$word,$matches);
echo $matches[1];
//取height
preg_match(\'/<img.+(height=\"?\d*\"?).+>/i\',$word,$matches);
echo $matches[1];
//取src
preg_match(\'/<img.+src=\"?(.+\.(jpg|gif|bmp|bnp|png))\"?.+>/i\',$word,$matches);
echo $matches[1];
/*正則替換去掉或改變圖片img標記中的任意屬性***************************************************************/
$str = \'<p height="22" align="cenetr">111 22</p> <img height="60" src="/upload/images/aaa.jpg" width=100 style=><div style="float:left;">中國人</div>
<p height="22" align="cenetr">31313 224344</p> <img src="/upload/images/bbb.jpg" height="60" width=100 style=><div style="float:left;">1212121</div>\';
//改變src屬性(此處將原來(lái)的src="/upload/images/bbb.jpg"改變?yōu)?/font>src="/upload/_thumbs/Images/bbb.jpg")
print preg_replace(\'/(<img.+src=\"?.+)(images\/)(.+\.(jpg|gif|bmp|bnp|png)\"?.+>)/i\',"\${1}_thumbs/Images/\${3}",$str);
/*改變src屬性,
此處將原來(lái)的src="/upload/images/bbb.jpg"改變?yōu)閟rc="/upload/_thumbs/Images/bbb.jpg",并舍棄寬和高
(比如你想在前臺顯示縮略圖,但數據庫中存儲的是原圖的路徑。為什么要舍棄寬高??你縮略圖啊!還是原圖的寬高,會(huì )怎樣???)
*/
print preg_replace(\'/(<img).+(src=\"?.+)images\/(.+\.(jpg|gif|bmp|bnp|png)\"?).+>/i\',"\${1} \${2}_thumbs/Images/\${3}>",$str);
?>
聯(lián)系客服