Programmatically add avatars

Hi,
I need to programmatically add avatars. Is that possible?

Also, the avatars will reside off-site. Is that possible?

Thanks

  • emorling

    Nevermind I solved it myself.

    For future reference incase anyone needs it.

    Simply bass the blog id and remote url like this:
    avatars_create_avatar(1, "http://mytabletennis.net/forum/uploads/New/avatars/20080506_141433_spongebob_1.jpg");

    It will automatically download and resize the images to work with the Avatar plugin.

    function avatars_create_avatar($blog_id, $remoteImage) {
    // download avatar
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $remoteImage);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $fileContents = curl_exec($ch);
    curl_close($ch);
    $newImg = imagecreatefromstring($fileContents);
    // create avatar directory
    $avatar_path = './wp-content/avatars/blog/'.substr(md5($blog_id), 0, 3);
    if (!is_dir($avatar_path)){
    mkdir($avatar_path, 0777);
    }
    // create avatar in sizes
    avatars_create_avatar_size($blog_id, $avatar_path, $newImg, 128);
    avatars_create_avatar_size($blog_id, $avatar_path, $newImg, 96);
    avatars_create_avatar_size($blog_id, $avatar_path, $newImg, 48);
    avatars_create_avatar_size($blog_id, $avatar_path, $newImg, 32);
    avatars_create_avatar_size($blog_id, $avatar_path, $newImg, 16);
    //return imagejpeg($newImg, "./wp-content/avatars/user/{$isbn}.jpg",100);
    return $image;
    }

    function avatars_create_avatar_size($blog_id, $avatar_path, $newImg, $size) {
    $width = imagesx($newImg);
    $height = imagesy($newImg);
    $im_dest = imagecreatetruecolor ($size, $size);
    imagecopyresampled($im_dest, $newImg, 0, 0, 0, 0, $size, $size, $width, $height);
    if ($image_type == 'png'){
    imagesavealpha($im_dest, true);
    }
    $image = imagepng($im_dest, $avatar_path.'/blog-' . $blog_id . "-$size.png");
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.