Query 库使用 Stable Diffusion 和 TinyPNG 生成优化的文章特色图片
使用 Stable Diffusion 和 TinyPNG 生成优化的文章特色图片
此 Persisted GraphQL query 使用 Stable Diffusion 生成式 AI,为没有特色图片的文章生成图片。
首先,它会检查文章是否已有特色图片。如果没有,则通过调用 Stable Diffusion 的生成式 AI 服务来创建一张。使用前需要提供 API 密钥。
由于生成式 AI 产生的图片未针对 Web 进行优化,该 query 还可以将新生成的图片发送至 TinyPNG 进行压缩。使用前同样需要提供 API 密钥。
最后,该 query 会使用该图片创建一个新的媒体项,并将其设置为文章的特色图片。
query InitializeVariables(
$tinyPngAPIKey: String
)
@configureWarningsOnExportingDuplicateVariable(enabled: false)
{
isFeaturedImageMissing: _echo(value: false)
@export(as: "isFeaturedImageMissing")
@remove
isPostTitleNotEmpty: _echo(value: false)
@export(as: "isPostTitleNotEmpty")
@remove
generatedImageURL: _echo(value: null)
@export(as: "generatedImageURL")
@remove
isImageGenerated: _echo(value: false)
@export(as: "isImageGenerated")
@remove
mimeType: _echo(value: null)
@export(as: "mimeType")
@remove
isMediaItemCreated: _echo(value: false)
@export(as: "isMediaItemCreated")
@remove
useTinyPng: _notEmpty(value: $tinyPngAPIKey)
@export(as: "useTinyPng")
@remove
}
query ExportPostData(
$postID: ID!
)
@depends(on: "InitializeVariables")
{
post(by: { id: $postID }, status: any) {
hasFeaturedImage
isFeaturedImageMissing: hasFeaturedImage
@boolOpposite
@export(as: "isFeaturedImageMissing")
title
@export(as: "postTitle")
rawTitle
@remove
isPostTitleNotEmpty: _notEmpty(value: $__rawTitle)
@export(as: "isPostTitleNotEmpty")
mediaItemFilename: rawTitle
@default(value: "untitled", condition: IS_EMPTY)
@strLowerCase
@strSubstr(offset: 0, length: 20)
@export(as: "filename")
@remove
}
}
query GenerateImageUsingStableDiffusion(
$stableDiffusionAPIKey: String!
$width: Int! = 1024
$height: Int! = 1024
)
@depends(on: "ExportPostData")
@include(if: $isFeaturedImageMissing)
@include(if: $isPostTitleNotEmpty)
{
stableDiffusionResponse: _sendJSONObjectItemHTTPRequest(input: {
url: "https://stablediffusionapi.com/api/v3/text2img",
method: POST,
options: {
json: {
key: $stableDiffusionAPIKey
prompt: $postTitle,
width: $width
height: $height
samples: 1
}
}
})
@underJSONObjectProperty(by: { key: "output" })
@underArrayItem(index: 0)
@export(as: "generatedImageURL")
stableDiffusionImageCaption: _sprintf(
string: "Image created by Stable Diffusion using prompt: '%s'",
values: [$postTitle]
)
@export(as: "imageCaption")
stableDiffusionMediaItemFilename: _sprintf(
string: "%s.png",
values: [$filename]
)
@export(as: "filename")
}
query CheckIsImageGenerated
@depends(on: "GenerateImageUsingStableDiffusion")
@include(if: $isFeaturedImageMissing)
@include(if: $isPostTitleNotEmpty)
{
isImageGenerated: _notEmpty(value: $generatedImageURL)
@export(as: "isImageGenerated")
}
query MaybeCompressGeneratedImage(
$tinyPngAPIKey: String
)
@depends(on: "CheckIsImageGenerated")
@include(if: $isImageGenerated)
@include(if: $useTinyPng)
{
compressedImageResponse: _sendHTTPRequest(input: {
url: "https://api.tinify.com/shrink",
method: POST,
options: {
auth: {
password: $tinyPngAPIKey
},
headers: [
{
name: "Content-Type",
value: "application/json"
}
],
json: {
source: {
url: $generatedImageURL
}
}
}
}) {
body
@remove
bodyJSONObject: _strDecodeJSONObject(string: $__body)
mimeType: _objectProperty(
object: $__bodyJSONObject
by: { path: "output.type" }
)
@export(as: "mimeType")
generatedImageURL: header(name: "Location")
@export(as: "generatedImageURL")
}
}
mutation CreateMediaItemFromGeneratedImage
@depends(on: "MaybeCompressGeneratedImage")
@include(if: $isImageGenerated)
{
createMediaItem(input: {
from: {
url: {
source: $generatedImageURL
filename: $filename
}
}
title: $postTitle
caption: $imageCaption
altText: $postTitle
mimeType: $mimeType
}) {
mediaItemID
@export(as: "mediaItemID")
isMediaItemCreated: _notNull(value: $__mediaItemID)
@export(as: "isMediaItemCreated")
@remove
status
errors {
__typename
...on ErrorPayload {
message
}
}
mediaItem {
altText
caption
mimeType
slug
src
title
}
}
}
mutation GenerateOptimizedPostFeaturedImageUsingStableDiffusion(
$postID: ID!
)
@depends(on: "CreateMediaItemFromGeneratedImage")
@include(if: $isMediaItemCreated)
{
setFeaturedImageOnCustomPost(input: {
customPostID: $postID
mediaItemBy: { id: $mediaItemID }
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPost {
__typename
...on CustomPost {
featuredImage {
id
altText
caption
mimeType
slug
src
title
}
}
}
}
}