Download Flickr Gallery Photos

4Y6A4728.jpg

I took photos for my youngest’s soccer team this year. I publish them to Flickr because I like Flickr still and have been using it for 17 years (!?!). One of the parents put together a gallery for the seniors using Flickr. They then wanted to show it as a slideshow at the upcoming senior night. There’s no decent way to do that in galleries (albums, yes) and there’s no decent way to bulk download galleries (albums, yes).

I knew I could write some PHP to do it but it wasn’t an interesting challenge or anything I wanted to spend time on. It was also Saturday and I was tired from editing several thousand soccer photos. As someone with a decent knowledge of PHP and way too much AI experience of late, I decided I’d just ask chatGPT to do it using the 4o with Canvas model.

Create a php app to download the images in a Flickr gallery using the API. Provide areas for associating the api key and secret.

I had an answer in a few seconds. My prompt was at least mediocre in terms of specificity. It did fail to put the $ for the $galleryId variable. I knew where to get the API key, I also knew what to do with the code chatGPT created. I tweaked things a bit to get the larger format for the image with a revision prompt just to see how well it’d do. It did well.

It only took about 5 minutes total. I was pretty happy. This is the kind of simple thing where I can see LLMs speeding things up for people with certain levels of knowledge. I can also see these tools letting you push into publishing/hosting environments in the near future. That’d take a chunk of the complexity out of the mix and provide another way to make money.1

<?php

// Replace with your Flickr API Key and Secret
define('FLICKR_API_KEY', 'YOUR_API_KEY_HERE');
define('FLICKR_API_SECRET', 'YOUR_API_SECRET_HERE');

$galleryId = 'YOUR_GALLERY_ID_HERE'; // Replace with the actual gallery ID

downloadImagesFromGallery($galleryId);

function downloadImagesFromGallery($galleryId) {
    // Step 1: Get the photos in the gallery
    $url = "https://api.flickr.com/services/rest/?method=flickr.galleries.getPhotos&api_key=" . FLICKR_API_KEY . "&gallery_id=" . $galleryId . "&format=json&nojsoncallback=1";

    $response = file_get_contents($url);
    $data = json_decode($response, true);

    if ($data['stat'] == 'ok') {
        foreach ($data['photos']['photo'] as $photo) {
            // Step 2: Construct the URL for each photo
            $photoUrl = constructPhotoUrl($photo);

            // Step 3: Download the photo
            downloadPhoto($photoUrl, $photo['title']);
        }
    } else {
        echo "Failed to fetch gallery photos: " . $data['message'] . "\n";
    }
}

function constructPhotoUrl($photo) {
    $farmId = $photo['farm'];
    $serverId = $photo['server'];
    $id = $photo['id'];
    $secret = $photo['secret'];

    return "https://farm$farmId.staticflickr.com/$serverId/{$id}_{$secret}_b.jpg"; // Use the "_b" suffix for large image format
}

function downloadPhoto($url, $title) {
    $title = preg_replace('/[^a-zA-Z0-9_\-]/', '_', $title); // Sanitize title for a valid filename
    $filePath = "downloads/$title.jpg";

    if (!file_exists('downloads')) {
        mkdir('downloads', 0777, true);
    }

    $photoData = file_get_contents($url);
    if ($photoData !== false) {
        file_put_contents($filePath, $photoData);
        echo "Downloaded: $title\n";
    } else {
        echo "Failed to download: $title\n";
    }
}
?>


1 You’ll need those when you lose $5 billion dollars.