Ajax image grabber
I need an image grabber. By that I mean a Digg like image grabber that can search other pages (including youtube, normal websites, economist,...whatever), get the images that are of decent sizes and if I select it, I can upload it to my server.
Does anyone know 开发者_如何学Cof a plugin for this?
Thanks.
I don't know about any off-the-shelf library. But I once needed a quick way to retrieve the "main image" off a page. My best guess was to just get the largest in file size. I was using the PHP SimpleHTMLDom library for easy access to the site's <img>
tags.
Now, here's the main part of the code, that returns the URL of the biggest image file for a given page.
Hope you can build on that.
// 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;
精彩评论