我正在尝试编写一个程序,该程序将从网页中抓取一堆图像并找出哪些图像是最大的。需要关于重新关联数组值与原始键的建议
到目前为止,我已经拍摄了图像,将它们放入数组中,使用getimagesize()函数来确定高度。然后我把所有的高度放到另一个数组中,并按相反的顺序排列它们以获得最大的数组。到现在为止还挺好。
我现在的问题是,我必须找到一种方法来重新关联最大的图像与其初始图像链接。我想过可能会运行最初的代码来从网页上再次获取图像。然后比较我用来确定最大图像的阵列中第一个值和第二个图像,但这看起来像是浪费了带宽,我感觉有一种更简单的方法将高度值与它的初始图像。我对吗?
<?php
$url = 'http://lockerz.com/s/104049300';
// Fetch page
$string = FetchPage($url);
// Regex that extracts the images (full tag)
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
$img_tag_array = $out[0];
echo "<pre>"; print_r($img_tag_array); echo "</pre>";
// Regex for SRC Value
$image_regex_src_url = '/<img[^>]*'.
'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex_src_url, $string, $out, PREG_PATTERN_ORDER);
$images_url_array = $out[1];
$image_heights_array = array();
foreach ($images_url_array as $imagelink)
{
if (substr($imagelink,0,7)=="http://")
{
$getheight = getimagesize($imagelink);
array_push($image_heights_array,"$getheight[1]");
}
}
rsort($image_heights_array);
echo "<pre>"; print_r($image_heights_array); echo "</pre>";
// Fetch Page Function
function FetchPage($path)
{
$file = fopen($path, "r");
if (!$file)
{
exit("The was a connection error!");
}
$data = '';
while (!feof($file))
{
// Extract the data from the file/url
$data .= fgets($file, 1024);
}
return $data;
}
?>
你的句子太长了。请使用句号和大写字母。 – 2011-05-24 14:45:20
@Tomalak Geret'kal请学习阅读更快,屏住呼吸更长的时间:-P – martynthewolf 2011-05-24 14:52:30
@martswite:Heh:P – 2011-05-24 15:15:36