How can I change an image field programatically?
function top_node_update($node) {
      $top = views_get_view_result('top_3');
      $first = $top[0]-&gt;nid;
      $second = $top[1]-&gt;nid;
      $third = $top[2]-&gt;nid;

  if ($first == $node-&gt;nid) {
    $image = file_get_contents('public://images/badges/gouden rozet.png');
    $file  = file_save_data($image, 'public://images/gouden rozet.png',FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }

  if ($second == $node-&gt;nid) {
    $image = file_get_contents('public://images/badges/zilveren rozet.png');
    $file  = file_save_data($image, 'public://images/zilveren rozet.png',FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }

  if ($third == $node-&gt;nid) {
    $image = file_get_contents('public://images/badges/bronzen rozet.png');
    $file  = file_save_data($image, 'public://images/bronzen rozet.png', FILE_EXISTS_REPLACE);
    $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
    field_attach_update('node', $node);
  }
}


You should also try to prevent using functionality twice. So instead of the code above you can also write something like:

function top_node_update($node) {

  $top = views_get_view_result('top_3');
  $first = $top[0]-&gt;nid;
  $second = $top[1]-&gt;nid;
  $third = $top[2]-&gt;nid;

  if ($first == $node-&gt;nid) {
    $img_name = 'gouden rozet.png';
  }

  if ($second == $node-&gt;nid) {
    $img_name = 'zilveren rozet.png';
  }

  if ($third == $node-&gt;nid) {
    $img_name = 'bronzen rozet.png';
  }

  $image = file_get_contents('public://images/badges/' . $img_name);
  $file  = file_save_data($image, 'public://images/' . $img_name, FILE_EXISTS_REPLACE);
  $node-&gt;field_badge[LANGUAGE_NONE]['0']['fid'] = $file-&gt;fid;
  field_attach_update('node', $node);


}