最近我加入了GitHub。我在那里主持了一些项目。

我需要在README文件中包含一些图像。我不知道该怎么做。

我搜索了一下,但得到的只是一些链接,告诉我“在web上托管图像并在README.md文件中指定图像路径”。

是否有任何方法可以在不在任何第三方网络托管服务上托管图像的情况下做到这一点?


当前回答

此答案也可在以下位置找到:https://github.com/YourUserAccount/YourProject/blob/master/DirectoryPath/ReadMe.md

使用以下方式显示回购中的图像:

前置域:https://raw.githubusercontent.com/

附加标志:?消毒=真&原始=真

使用<img/>标记

Eample url works for svg, png, and jpg using:

域名:raw.githubusercontent.com/用户名:YourUserAccount/回购:YourProject/分行:YourBranch/路径:DirectoryPath/文件名:example.png

适用于SVG、PNG和JPEG

 - `raw.githubusercontent.com/YourUserAccount/YourProject/YourBranch/DirectoryPath/svgdemo1.svg?sanitize=true&raw=true`

使用后显示的工作示例代码如下:

**raw.githubusercontent.com**:
<img src="https://raw.githubusercontent.com/YourUserAccount/YourProject/master/DirectoryPath/Example.png?raw=true" />

<img src="https://raw.githubusercontent.com/YourUserAccount/YourProject/master/DirectoryPath/svgdemo1.svg?sanitize=true&raw=true" />

raw.githubusercontent.com:

谢谢:- https://stackoverflow.com/a/48723190/1815624- https://github.com/potherca-blog/StackOverflow/edit/master/question.13808020.include-an-svg-hosted-on-github-in-markdown/readme.md

其他回答

如果您需要上传一些图片用于文档,一个不错的方法是使用git-lfs。假设您已安装git-lfs,请执行以下步骤:

初始化每个图像类型的git-lfs:数字lfs*.png数字lfs*.svg数字lfs*.gifgit-lfs*.jpggit-lfs*.jpeg创建一个将用作图像位置的文件夹,例如doc。在基于GNU/Linux和Unix的系统上,这可以通过以下方式完成:cd项目文件夹mkdir文档git添加文档复制粘贴任何图像到文档文件夹。然后通过gitadd命令添加它们。提交并推送。图片可在以下url中公开获取:https://media.githubusercontent.com/media/^github_username^/^repo^/^branch^/^image_location在repo中^

哪里:*^github_username^是github中的用户名(您可以在配置文件页面中找到)*^repo_name^是存储库名称*^branch^是上载图像的存储库分支*^image_location in the repo ^是包含存储图像的文件夹的位置。

此外,您还可以先上传图像,然后访问项目github页面中的位置并浏览,直到找到图像,然后按下下载按钮,然后从浏览器的地址栏复制粘贴url。

将我的项目作为参考。

然后,您可以使用上面提到的markdown语法使用url来包含它们:

![some alternate text that describes the image](^github generated url from git lfs^)

让我们假设我们使用这张照片,然后你可以使用markdown语法:

![In what order to compile the files](https://media.githubusercontent.com/media/pc-magas/myFirstEnclave/master/doc/SGX%20Compile%20workflow.png)

您可以创建新问题上传(拖放)图像到它复制图像URL并将其粘贴到README.md文件中。

下面是一个详细的youTube视频,详细解释了这一点:

https://www.youtube.com/watch?v=nvPOUdz5PL4

只需在README.md中添加一个<img>标记,并将相对src添加到存储库。如果您没有使用相对src,请确保服务器支持CORS。

它之所以有效,是因为GitHub支持内联html

<img src="/docs/logo.png" alt="My cool logo"/>
# My cool project and above is the logo of it

在此处观察

在最新的Github中,我尝试了以上所有方法,但未能在自述文件页面上显示图像。如果是公共回购,我发现最好的方法是对master或main分支使用githubpages,然后引用自述文件中的链接。

步骤:

在您的存储库中,转到“代码和自动化”下的设置->选择页选择回购的来源和分支,然后单击保存

现在您的repo应该托管在(https://*username*.github.io/*repone*/)上

现在,您可以通过

![Preview Image](https://*username*.github.io/*reponame*/images/logo.PNG)

您还可以添加带有简单HTML标记的图像:

<p align="center">
  <img src="your_relative_path_here" width="350" title="hover text">
  <img src="your_relative_path_here_number_2_large_name" width="350" alt="accessibility text">
</p>