从 SilverStripe 中的路径创建 Image 的实例

Create an instance of Image from a path in SilverStripe

例如我们有路径 /assets/no-thumbnail.png,如何将其加载到 SilverStripe 中的 Image class?我有办法做到这一点吗?

我问的原因是因为我想要 Image class.

中可用的方法

是的,对于存在于 /assets 文件夹中的文件来说,这非常简单。虽然其他位置的文件可能也可以实现,但我将描述的方法将处理该文件与您通过 CMS 上传的任何其他文件没有区别。

首先,我们需要确定这张图片有DB记录。幸运的是,如果您需要以编程方式执行此操作,它可以像调用 Filesystem::sync() 一样简单。如果没有,CMS 的“文件”选项卡中有一个 "Sync" 按钮。

完成后,您可以简单地调用 File::get()->filter('Name', 'no-thumbnail.png')->first()(或者甚至只是 File::find('no-thumbnail.png')) to get your "proper" Image object, allowing you to call any of the of the Image class's instance methods