I’ve posted a code snippet I used to sideload images from Flickr into WordPress, specifically for http://foodirl.com/ I originally stored all of my images on Flickr because I started the blog on Tumblr, knowing that I would move it later (which I did, to Crowd Fusion and then to WordPress).


<?php
/**
* Retroactively sideload images referenced from Flickr.
*/
$flickr_sideload_media_id = null;
function flickr_sideload_get_media_id( $media_id ) {
global $flickr_sideload_media_id;
$flickr_sideload_media_id = $media_id;
}
function flickr_sideload_init() {
global $flickr_sideload_media_id;
if ( isset( $_GET['do_flickr_sideload'] ) ) {
add_action( 'add_attachment', 'flickr_sideload_get_media_id' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$offset = 0;
$limit = 10;
while ( $posts = get_posts( 'posts_per_page=' . $limit . '&offset=' . $offset ) ) {
$offset += $limit;
foreach ( $posts as $post ) {
if ( preg_match_all( '/(?<atag><a[^>]+href="(?<href>[^"]+)"[^>]*>)\s*(?<imgtag><img[^>]+src="(?<src>[^"]+)"[^>]+>)\s*(<\/a>)?/i', $post->post_content, $images, PREG_SET_ORDER ) ) {
$post_content = $post->post_content;
foreach ( $images as $image ) {
if ( strpos( $image['src'], '.flickr.com/' ) !== false ) {
$description_matches = array();
$description = '';
if ( ! empty( $image['atag'] ) ) {
preg_match_all( '/title="(?<title>[^"]+)"/i', $image['atag'], $description_matches, PREG_SET_ORDER );
}
if ( ! empty( $description_matches ) ) {
$description = $description_matches[0]['title'];
}
else {
preg_match_all( '/alt="(?<alt>[^"]+)"/i', $image['imgtag'], $description_matches, PREG_SET_ORDER );
if ( ! empty( $description_matches ) ) {
$description = $description_matches[0]['alt'];
}
}
$image_html = media_sideload_image( $image['src'], $post->ID, $description );
preg_match_all( '/width="(?<width>[0-9]+)"/', $image['imgtag'], $width_matches, PREG_SET_ORDER );
preg_match_all( '/height="(?<height>[0-9]+)"/', $image['imgtag'], $height_matches, PREG_SET_ORDER );
if ( ! empty( $width_matches ) && ! empty( $height_matches ) ) {
$size = array( $width_matches[0]['width'], $height_matches[0]['height'] );
}
else {
$size = 'full';
}
if ( ! empty( $image['atag'] ) ) {
$image_html = wp_get_attachment_link( $flickr_sideload_media_id, $size );
}
else if ( $size != 'full' ) {
$image_html = str_replace( '<img ', '<img width="' . $size[0] . '" height="' . $size[1] . '" ', $image_html );
}
$post_content = str_replace( $image[0], $image_html, $post_content );
}
}
if ( $post_content != $post->post_content ) {
wp_update_post( array( 'ID' => $post->ID, 'post_content' => $post_content ) );
}
}
}
}
die;
}
}
add_action( 'init', 'flickr_sideload_init' );

Stick it in mu-plugins and then load wp-admin/?do_flickr_sideload. It doesn’t have lots of error checking, but it worked flawlessly for FoodIRL’s 160+ posts.