2

I'm trying to get images from flickr using the Flickr API and I'm having trouble figuring out how to get only unique images. I've already reviewed the arguments for the specific method that I'm using and here's what I came up with:

<?php
class Flickr {

    private $flickr_key;
    private $flickr_secret;
    private $format = 'json';

    public function __construct( $flickr_key, $flickr_secret ) {

        $this->flickr_key = $flickr_key;
        $this->flickr_secret = $flickr_secret;
    }


    public function searchPhotos( $query = '', $tags = '' ) {

        $urlencoded_tags = array(); 

        $tags_r = explode(',', $tags);
        foreach($tags_r as $tag){
            $urlencoded_tags[] = urlencode($tag);
        }

        $url = 'http://api.flickr.com/services/rest/?';
        $url .= 'method=flickr.photos.search';
        $url .= '&text=' . urlencode($query);
        $url .= '&tags=' . implode(',', $urlencoded_tags);
        $url .= '&sort=relevance';
        $url .= '&safe_search=1';
        $url .= '&content_type=4';
        $url .= '&api_key=' . $this->flickr_key;
        $url .= '&format=' . $this->format;
        $url .= '&per_page=10';
        $url .= '&media=photos';
        $url .= '&privacy_filter=1';

        $result = @file_get_contents( $url );


        $json = substr( $result, strlen( "jsonFlickrApi(" ), strlen( $result ) - strlen( "jsonFlickrApi(" ) - 1 );

        $photos = array();
        $data = json_decode( $json, true );
        if($data['stat'] != 'fail'){
            $photos = $data['photos']['photo'];
            return $photos;
        }else{
            return false;
        }
    }
}

And I'll just call it in like:

$flickr = new Flickr($flickr_key, $flickr_secret);

$query = 'Kaspersky Internet Security 2013';
$tags = 'software';

$results = $flickr->searchPhotos($query, $tags);
foreach($results as $img){
    $src = "http://farm" . $img['farm'] . ".static.flickr.com/" . $img['server'] . '/' . $img['id'] . '_' . $img['secret'] . '_m.jpg';
?>
  <img src="<?php echo $src; ?>"/>
<?php
}

The problem here is that I'm getting duplicate images from time to time. I also tried using the phpflickr library. But I'm still having the same issues:

$flickr = new phpFlickr($api_key);

$args = array(
    'text' => 'Kaspersky Internet Security 2013',
    'tags' => 'software',
    'per_page' => '10',
    'safe_search' => '1',
    'content_type' => '4',
    'media' => 'photos',
    'sort' => 'relevance',
    'privacy_filter' => '1'
);

$results = $flickr->photos_search($args);

$hashes = array();
$sources = array();

$images = $results['photo'];

foreach($images as $img){

    $src = "http://farm" . $img['farm'] . ".static.flickr.com/" . $img['server'] . '/' . $img['id'] . '_' . $img['secret'] . '_m.jpg';

    $current_hash = sha1_file($src);


    if(!in_array($current_hash, $hashes)){
?>
    <img src="<?php echo $src; ?>" alt="">
<?php       
    }

    $hashes[] = $current_hash;
}

As you can see from the above code I've used sha1_file method to compare the hashes of each of the images returned from flickr. But that's a big performance hit:

without sha1_file: 0.81311082839966
with sha1_file: 6.8974900245667 

Any ideas what else can I do to prevent flickr from returning duplicates? As you can see I'm only returning 10 images and that's all I need. I've also tried to add as many arguments that matches my needs but still no luck. Thanks in advance!

user225269
  • 10,743
  • 69
  • 174
  • 251
  • Any update or more information on this? I have returned 85,763 photos but only 4280 of them are unique. According to the JSON for my call there should be >500,000 unique. – ow3n Dec 23 '13 at 05:31

1 Answers1

1

try on API explorer, will this give you the same result?

I tried using the param you specified, it returns 34 result in total..

xialin
  • 7,686
  • 9
  • 35
  • 66
  • still the same, the last 3 results are all the same, though the file names are different. – user225269 Sep 11 '13 at 03:35
  • 1
    doesn't that mean the owner uploaded duplicate photos? The API recognises them as two different files but you can *see* it's the same image ... You need to filter duplicate by adding other attributes, e.g. title , description etc. – xialin Sep 11 '13 at 05:14