2013-03-19 45 views
0

这可能是一个时髦的问题,但我想知道是否有人可以想出一种方式如何把一大块html,扫描它的<img>标签,如果标签没有宽度+身高值适用于list($width, $height, $type, $attr);通过PHP扫描并编辑img宽度高度输出前

更详细地说,我有一个php页面,其中包含另一个页面,只有html。 我想在输出到浏览器之前更改html。

这是一个简化版本,我所期待的:某些输入后

<!DOCTYPE HTML> 
<html> 
<head> 
</head> 
<body> 
<div id="content"> 
<?php 
include_once("client-contributed-text-and-images.php"); 
?> 
</div> 
</body> 
</html> 

下面我想出了以下内容:

<!DOCTYPE HTML> 
<html> 
<head> 
</head> 
<body> 
<div id="content"> 
<?php 
$dom = new DOMDocument(); 
$dom->loadHTMLFile("client-contributed-text-and-images.php"); 

foreach ($dom->getElementsByTagName('img') as $item) { 

    $item->setAttribute('width', '100'); 
    echo $dom->saveHTML(); 
    exit; 
} 
?> 
</div> 
</body> 
</html> 

的问题是,它生成一个完整的HTML4文件在中间,虽然只改变了第一个img标签,并且看起来之后没有输出代码:

<!DOCTYPE HTML> 
<html> 
<head> 
</head> 
<body> 
<div id="content"> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> 
<html><body><img src="img1.jpg" width="100"><h1>header</h1> 
<p>some text</p> 
<a href="http://google.com">some link</a> 
<img src="img2.jpg"></body></html> 

所以我把目光转向并试图fopen()函数来代替,而得到它的部分工作:

<!DOCTYPE HTML> 
<html> 
<head> 
</head> 
<body> 
<div id="content"> 
<?php 
$root = realpath($_SERVER['DOCUMENT_ROOT']); 
$file = $root."/client-contributed-text-and-images.php"; 
$f = fopen($file, 'r'); 
$contents = fread($f, filesize($file)); 
fclose($f); 

$new_contents = str_replace("<img ", "<img width='100' height='100' ", $contents); 
echo $new_contents; 
?> 
</div> 
</body> 
</html> 

这些都给:

<!DOCTYPE HTML> 
<html> 
<head> 
</head> 
<body> 
<div id="content"> 
<img width='100' height='100' src="img1.jpg"> 
<h1>header</h1> 
<p>some text</p> 
<a href="http://google.com">some link</a> 
<img width='100' height='100' src="img2.jpg"></div> 
</body> 
</html> 

现在我只是需要一些帮助,首先要弄清楚如何实施list($width, $height, $type, $attr);包括正确的和高度(显然只有当它尚未设置)。

回答

1

是的,这是完全可能的。

  1. Use a DOM parser to load your HTML找到您的图片标签。
  2. Use cURL to download images(如果你还没有他们在本地)
  3. Use GD to get the image sizes
  4. 使用DOMDocument to modify the HTML
  5. Output modified HTML

请注意,所有这些都需要很长的处理时间。这可能不值得。至少,缓存结果。

+0

所以这样的事情? '<?php $ doc = new DOMDocument(); $ doc-> loadHTMLFile(“filename.html”); /*在这里改变img标签莫名其妙...... */ echo $ doc-> saveHTML(); ?>' – Paul 2013-03-19 19:34:20

+0

@Paul,确实如此。 – Brad 2013-03-19 19:46:23

+0

这是/ *更改img标签在这里莫名其妙...... * /部分我不确定如何去坚持新的代码:) – Paul 2013-03-19 19:58:15

0

你可以尝试

$url = 'http://yahoo.com'; 
$dom = new DOMDocument(); 
@$dom->loadHTMLFile($url); 

$imgs = $dom->getElementsByTagName("img"); 

foreach ($imgs as $img) { 
    $attrs = array(); 
    // only load large images 
    if ((int) $img->getAttribute("height") < 80) 
     continue; 

    for($i = 0; $i < $img->attributes->length; ++ $i) { 
     $node = $img->attributes->item($i); 
     $attrs[$node->nodeName] = $node->nodeValue; 
    } 
    print_r($attrs); 
} 

输出

Array 
(
    [src] => http://l3.yimg.com/nn/fp/rsz/031913/images/smush/ucf-thwarted_635x250_1363714489.jpg 
    [class] => fptoday-img 
    [alt] => Quick thinking helped thwart UCF massacre plan (AP) 
    [title] => Quick thinking helped thwart UCF massacre plan (AP) 
    [width] => 635 
    [height] => 250 
) 
Array 
(
    [src] => http://l.yimg.com/os/mit/media/m/base/images/transparent-95031.png 
    [style] => background-image:url('http://l2.yimg.com/ts/api/res/1.2/8hS1Q3v9rmaW8yI0eXEPHw--/YXBwaWQ9eWhvbWVydW47cT04NTtzbT0xO3c9MjUwO2g9MTU5/http://media.zenfs.com/en_us/News/Reuters/2013-03-19T120925Z_1_CBRE92I0XS100_RTROPTP_2_USA-SHOOTING-OHIO.JPG'); 
    [width] => 129 
    [height] => 82 
    [alt] => 
    [title] => 
    [class] => lzbg 
) 
Array 
(
    [src] => http://l.yimg.com/os/mit/media/m/base/images/transparent-95031.png 
    [style] => background-image:url('http://l3.yimg.com/ts/api/res/1.2/wcwLlp6sGVdOT7WXfkGEkQ--/YXBwaWQ9eWhvbWVydW47cT04NTtzbT0xO3c9MTg2O2g9MjUw/http://l.yimg.com/os/publish-images/lifestyles/2013-03-19/d9f10733-ee09-4e1f-a363-e3b9cd66078f_garygoldsmith.jpg'); 
    [width] => 82 
    [height] => 110 
    [alt] => 
    [title] => 
    [class] => lzbg 
) 


.......... 
+0

你有一个例子,我怎样才能得到它与编辑img标签拟合模型在我最初(修改)后的输出? – Paul 2013-03-19 19:50:53

相关问题