首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ajax图像采集器

Ajax图像采集器
EN

Stack Overflow用户
提问于 2011-04-20 16:49:48
回答 1查看 601关注 0票数 0

我需要一个图像采集器。我指的是一个像Digg一样的图像采集器,它可以搜索其他页面(包括youtube,普通网站,经济学家,...whatever),获得合适大小的图像,如果我选择它,我可以上传到我的服务器上。

有没有人知道这个插件?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-20 18:27:34

我不知道有什么现成的图书馆。但我曾经需要一种快速的方法来检索页面上的“主图像”。我最好的猜测是只获取最大的文件大小。我使用PHP SimpleHTMLDom库来方便地访问站点的<img>标记。

现在,这是代码的主要部分,它返回给定页面的最大图像文件的URL。

希望你能在此基础上再接再新。

代码语言:javascript
运行
复制
// Load the remote document
$html = file_get_html($url);

$largest_file_size=0;
$largest_file_url='';

// Go through all images of that page
foreach($html->find('img') as $element){
    // Helper function to make absolute URLs from relative
    $img_url=$this->InternetCombineUrl($url,$element->src);
    // Try to get image file size info from header:
    $header=array_change_key_case(get_headers($img_url, 1));
    // Only continue if "200 OK" directly or after first redirect:
    if($header[0]=='HTTP/1.1 200 OK' || @$header[1]=='HTTP/1.1 200 OK'){
        if(!empty($header['content-length'])){
            // If we were redirected, the second entry is the one.
            // See http://us3.php.net/manual/en/function.filesize.php#84130
            if(!empty($header['content-length'][1])){
                $header['content-length']=$header['content-length'][1];
            }
            if($header['content-length']>$largest_file_size){
            $largest_file_size=$header['content-length'];
            $largest_file_url=$img_url;
            }
        }else{ 
            // If no content-length-header is sent, we need to download the image to check the size
            $tmp_filename=sha1($img_url);
            $content = file_get_contents($img_url);
            $handle = fopen(TMP.$tmp_filename, "w");
            fwrite($handle, $content);
            fclose($handle);
            $filesize=filesize(TMP.$tmp_filename);
            if($filesize>$largest_file_size){
            $largest_file_size=$filesize;
            $largest_file_url=$img_url;
            unlink(TMP.$tmp_filename);
            }
        }
    }
}
return $largest_file_url;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5727743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档