Created
March 17, 2012 06:42
-
-
Save jboesch/2055837 to your computer and use it in GitHub Desktop.
Import gravatar images to store on your local server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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