来自 JSON api 或直接的 Wagtail getting/generating 图片网址
Wagtail getting/generating image urls from JSON api or directly
我一直在使用 Wagtail 作为前端应用程序的无头 CMS,但是我注意到一些关于图像的限制。通常在您的 jinja 模板中,您会生成所需的正确图像大小,一切都很好,但是我无法在我的前端代码中访问这些助手。我一直在尝试几件事。例如,为了解决简单页面模型及其字段的这个问题,我可以像这样呈现一个自定义 api 字段:
api_fields = [
# Adds information about the source image (eg, title) into the API
APIField('feed_image'),
# Adds a URL to a rendered thumbnail of the image to the API
APIField('feed_image_thumbnail', serializer=ImageRenditionField('fill-100x100', source='feed_image')),
...
]
然而,这不适用于 streamfield,因为它们只会 return 图像 ID。所以我想我会使用 Wagtail 图像 API,但是这也不允许我访问直接 URL。
我找到一些参考此文档的 google 组答案:http://docs.wagtail.io/en/v1.9/advanced_topics/images/image_serve_view.html
然而,该页面似乎不存在于最新版本的文档中,并且似乎不允许我从前端的 url 生成图像。
有没有办法创建一个 url 允许我根据其 ID 获取图像?
例如:somehost:8000/images/1?width=200&height=200
或者我忽略了其他一些解决方案。
我喜欢 wagtail 但无法轻松访问图像 urls 确实限制了它的 API 使用,我希望有一个好的解决方案。
谢谢
编辑:
我设法在文档中找到了这个:
http://docs.wagtail.io/en/v1.11.1/advanced_topics/images/image_serve_view.html
但是他们说:
The view takes an image id, filter spec and security signature in the URL. If these parameters are valid, it serves an image file matching that criteria.
但是他们没有给出一个明确的例子来说明这样的请求会是什么样子或者我将如何生成该安全签名。
将图像再现作为 StreamField 数据结构的一部分的一种(有点老套)方法是重写 ImageChooserBlock
的 get_api_representation
方法:
from wagtail.wagtailimages.blocks import ImageChooserBlock as DefaultImageChooserBlock
class ImageChooserBlock(DefaultImageChooserBlock):
def get_api_representation(self, value, context=None):
if value:
return {
'id': value.id,
'title': value.title,
'large': value.get_rendition('width-1000').attrs_dict,
'thumbnail': value.get_rendition('fill-120x120').attrs_dict,
}
在您的 StreamField 定义中使用此版本的 ImageChooserBlock
然后会给您 'large' 和 'thumbnail' 再现作为 API 响应的一部分,而不仅仅是图像编号.
我一直在使用 Wagtail 作为前端应用程序的无头 CMS,但是我注意到一些关于图像的限制。通常在您的 jinja 模板中,您会生成所需的正确图像大小,一切都很好,但是我无法在我的前端代码中访问这些助手。我一直在尝试几件事。例如,为了解决简单页面模型及其字段的这个问题,我可以像这样呈现一个自定义 api 字段:
api_fields = [
# Adds information about the source image (eg, title) into the API
APIField('feed_image'),
# Adds a URL to a rendered thumbnail of the image to the API
APIField('feed_image_thumbnail', serializer=ImageRenditionField('fill-100x100', source='feed_image')),
...
]
然而,这不适用于 streamfield,因为它们只会 return 图像 ID。所以我想我会使用 Wagtail 图像 API,但是这也不允许我访问直接 URL。
我找到一些参考此文档的 google 组答案:http://docs.wagtail.io/en/v1.9/advanced_topics/images/image_serve_view.html
然而,该页面似乎不存在于最新版本的文档中,并且似乎不允许我从前端的 url 生成图像。
有没有办法创建一个 url 允许我根据其 ID 获取图像?
例如:somehost:8000/images/1?width=200&height=200
或者我忽略了其他一些解决方案。
我喜欢 wagtail 但无法轻松访问图像 urls 确实限制了它的 API 使用,我希望有一个好的解决方案。
谢谢
编辑: 我设法在文档中找到了这个: http://docs.wagtail.io/en/v1.11.1/advanced_topics/images/image_serve_view.html
但是他们说:
The view takes an image id, filter spec and security signature in the URL. If these parameters are valid, it serves an image file matching that criteria.
但是他们没有给出一个明确的例子来说明这样的请求会是什么样子或者我将如何生成该安全签名。
将图像再现作为 StreamField 数据结构的一部分的一种(有点老套)方法是重写 ImageChooserBlock
的 get_api_representation
方法:
from wagtail.wagtailimages.blocks import ImageChooserBlock as DefaultImageChooserBlock
class ImageChooserBlock(DefaultImageChooserBlock):
def get_api_representation(self, value, context=None):
if value:
return {
'id': value.id,
'title': value.title,
'large': value.get_rendition('width-1000').attrs_dict,
'thumbnail': value.get_rendition('fill-120x120').attrs_dict,
}
在您的 StreamField 定义中使用此版本的 ImageChooserBlock
然后会给您 'large' 和 'thumbnail' 再现作为 API 响应的一部分,而不仅仅是图像编号.