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