0
我使用DOM製作PHP腳本來自動調整圖片的大小。該腳本的作品,但我有一個問題,當我試圖封裝一個調整後的圖像<a ...>
和</a>
之間(以顯示正常的大小在燈箱)。使用PHP添加指向元素的鏈接DOM
問題是調整大小的圖像顯示在$ html輸出的結尾,這是不正確的位置。請問我做錯了什麼?
這裏是我的代碼:
$dom = new DOMDocument();
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$max_width = 530;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$img_width = $image->getAttribute('width');
$img_height = $image->getAttribute('height');
if($img_width > $max_width) {
//Scale
$scale_factor = $max_width/$img_width;
$new_height = floor($img_height * $scale_factor);
//Set new attributes
$image->setAttribute('width', $max_width);
$image->setAttribute('height', $new_height);
//Add Link
$Zoom = $dom->createElement('a');
$Zoom->setAttribute('class', 'zoom');
$Zoom->setAttribute('href', $src);
$dom->appendChild($Zoom);
$Zoom->appendChild($image);
}
}
感謝您的幫助!
非常感謝它的作品!我只注意到我曾經嘗試過這兩行,但是順序錯誤(我非常接近)。謝謝 ! – SuN 2011-02-01 08:00:33