Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created March 17, 2012 06:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jboesch/2055837 to your computer and use it in GitHub Desktop.
Save jboesch/2055837 to your computer and use it in GitHub Desktop.
Import gravatar images to store on your local server
<?php
set_time_limit(120);
$domains = ClassRegistry::init('Domain')->find('all', array(
'conditions' => array(
'url' => $_GET['domain']
//'paid_recurring' => 1
),
'recursive' => -1
));
$User = ClassRegistry::init('User');
$gravatar_img_url = 'https://secure.gravatar.com/avatar/%s?s=37&d=http://7shifts.com/accounts/img/nophoto.png';
foreach($domains as $domain)
{
$domain_id = $domain['Domain']['id'];
$domain_folder = WWW_ROOT . 'img/group_avatars/' . md5($domain['Domain']['id']);
$users = $User->find('all', array(
'conditions' => array(
'User.domain_id' => $domain_id,
'User.email !=' => ''
),
'recursive' => -1
));
//$users = array($users[0], $users[1]);
if(!is_dir($domain_folder))
{
mkdir($domain_folder);
}
foreach($users as $user)
{
$email = md5(strtolower(trim($user['User']['email'])));
$img_file = md5($user['User']['id']) . '.jpg';
$grav_url = sprintf($gravatar_img_url, $email);
$dest = $domain_folder . DS . $img_file;
if(!is_file($dest))
{
copy($grav_url, $dest);
$copied_data = base64_encode(file_get_contents($dest));
// I checked before-hand to see what the bas64 encoded value of my 'nophoto.png' image was.
// If it's the nophoto image, then just remove it as I don't want to store that garbahhge.
if(substr($copied_data, 0, 2) == 'iV')
{
unlink($dest);
}
else
{
$User->save(array(
'id' => $user['User']['id'],
'photo' => $img_file
));
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment