我正在尝试从少数外部网站获取图片,因为我正在尝试为我的项目开发链接共享网站。我用curl将文件下载到本地服务器,但getimagesize仍然很慢(30秒)。我是php的新手任何帮助将不胜感激。 这是代码:
function check_url($value)
{
$value = trim($value);
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
$value = strtr($value, array_flip(get_html_translation_table(HTML_ENTITIES)));
$value = strip_tags($value);
$value = htmlspecialchars($value);
return $value;
}
$curl = curl_init();
$temp_file = tempnam(sys_get_temp_dir(), 'link');
$fp = fopen($temp_file, "w");
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_exec ($curl);
curl_close($curl);
///
$html = file_get_contents($temp_file);
fclose ($fp);
unlink($temp_file);
if($html) {
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');
//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
$description = $meta->getAttribute('content');
}
// fetch images
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $html, $img, PREG_PATTERN_ORDER);
$images_array = $img[1];
?>
<div class="images">
<?php
$k=1;
for ($i=0;$i<=sizeof($images_array);$i++)
{
if(@$images_array[$i])
{
if(@getimagesize(@$images_array[$i]))
{
list($width, $height, $type, $attr) = getimagesize(@$images_array[$i]);
if($width >= 80 && $height >= 80 ){
echo "<img src='".@$images_array[$i]."' width='100' id='".$k."' >";
$k++;
}
}
}
}
?>
<input type="hidden" name="total_images" id="total_images" value="<?php echo --$k?>" />
</div>
<div class="info">
<label class="title">
<?php echo @$title; ?>
</label>
<br clear="all" />
<label class="url">
<?php echo substr($url ,0,35); ?>
</label>
<br clear="all" /><br clear="all" />
<label class="desc">
<?php echo @$description; ?>
</label>
<br clear="all" /><br clear="all" />
<label style="float:left"><img src="prev.png" id="prev" alt="" /><img src="next.png" id="next" alt="" /></label>
<label class="totalimg">
Total <?php echo $k?> images
</label>
<br clear="all" />
</div>
<?php
} else {
echo "Please enter a valid url";
}
?>
答案 0 :(得分:0)
这并不奇怪。您正在遍历所有这些图像。下载这些图像然后获取它们的大小可能需要很长时间。
充其量,您可以异步加载这些图像并缓存结果。