/** * 用户更新头像 * * 使用标准Form表单上传,头像文件名为file,只接受jpg格式的文件 * * @param string token , 必填 * @return user array * @author EasyChen */ public function user_update_avatar() { if ($_FILES['file']['error'] != 0) { return self::send_error(OP_API_UPLOAD_ERROR, 'UPLOAD ERROR ' . $_FILES['file']['error']); } $tmp_image_name = SAE_TMP_PATH . md5(time() . rand(1, 99999)) . '.tmp.jpg'; jpeg_up($_FILES['file']['tmp_name'], $tmp_image_name); include_once AROOT . 'lib/thumbnail.class.php'; $file_thumb_name = 'avatar-' . uid() . '.jpg'; $tmp_file = SAE_TMP_PATH . $file_thumb_name; include_once AROOT . 'lib/icon.class.php'; $icon = new Icon(); $icon->path = $tmp_image_name; $icon->size = 100; $icon->dest = $tmp_file; $icon->createIcon(); if (on_sae()) { $s = new SaeStorage(); if (!($thumb_url = $s->write('upload', $file_thumb_name, file_get_contents($tmp_file)))) { return self::send_error(OP_API_STORAGE_ERROR, 'SAVE ERROR ' . $s->errmsg()); } } else { $local_storage = AROOT . 'static' . DS . 'upload' . DS . 'avatar' . DS; $local_storage_url = c('site_url') . DS . 'static' . DS . 'upload' . DS . 'avatar' . DS; $thumb_path = $local_storage . $file_thumb_name; $thumb_url = $local_storage_url . $file_thumb_name; if (!copy($tmp_file, $thumb_path)) { return self::send_error(OP_API_STORAGE_ERROR, 'SAVE ERROR '); } } $sql = "UPDATE `user` SET `avatar_small` = '" . s($thumb_url) . "' WHERE `id` = '" . intval(uid()) . "' LIMIT 1"; run_sql($sql); if (mysql_errno() != 0) { return self::send_error(OP_API_DB_ERROR, __('API_MESSAGE_DATABASE_ERROR') . mysql_error()); } else { return self::send_result(get_user_info_by_id(intval(uid()))); } }