この記事の内容
実際に
JavaScript開発者向け Gemini Nano Banana チートシート
Geminiも
テキストやgemini-2.5-flash-image)です。
この
JavaScriptでUint8Array形式で
さらに、
最後に、
それでは
1. AI SDKを使う
AI SDKは、
OpenAIや
統一されたgemini-2.5-flash-imageを
これに
以下は、
import { generateText } from 'ai'
import { createGoogleGenerativeAI } from '@ai-sdk/google'
const yodaImagePath = join(process.cwd(), 'data', 'yoda.jpg')
const yodaImageBuffer = await readFile(yodaImagePath)
const yodaImageUint8Array = new Uint8Array(yodaImageBuffer)
// Use AI SDK with generateText for Gemini 2.5 Flash Image (Nano Banana)
// This model supports image editing/generation with input images
const googleProvider = createGoogleGenerativeAI({ apiKey: apiKey })
const result = await generateText({
model: googleProvider('gemini-2.5-flash-image'),
prompt: [
{
role: 'user',
content: [
{
type: 'text',
text: 'Generate a realistic selfie photo of the person in the first image together with Yoda from Star Wars (shown in the second image). The person joins Yoda in the scene where he is at and hugs him in selfie style while they are both smiling and looking at the camera. The image should look natural and photorealistic, as if taken with a smartphone camera.'
},
{
type: 'image',
image: userImageUint8Array,
mediaType: imageFile.type || 'image/jpeg'
},
{
type: 'image',
image: yodaImageUint8Array,
mediaType: 'image/jpeg'
}
]
}
]
}) 2. Nano Bananaモデルの結果をData URLに変換する
Gemini Nano Banana(gemini-2.5-flash-image)のようなUint8Arrayバッファ形式です。
この
その<img>タグのsrc属性に
try {
// Call the model with await generateText({})
// ...
// Extract the generated image from result.files
const generatedImageFile = result.files?.find(file => file.mediaType?.startsWith('image/'))
if (!generatedImageFile) {
console.error('=== Error: No image file found ===')
console.error('Available files:', result.files)
console.error('Result text content:', result.text)
throw new Error('No image was generated in the response')
}
// Convert Uint8Array to base64 data URL for the frontend
const base64String = Buffer.from(generatedImageFile.uint8Array).toString('base64')
const dataUrl = `data:${generatedImageFile.mediaType};base64,${base64String}`
return {
success: true,
image: dataUrl
}
} catch (error: any) {
console.error('Error generating selfie:', error)
throw createError({
statusCode: error.statusCode || 500,
message: error.message || 'Failed to generate selfie'
})
}3. Webから画像データを取得する
ローカルファイルシステムからUint8Array形式に
最新の
通常Uint8Arrayなどのfetch APIでUint8Array形式に
// Fetch Yoda image
const yodaResponse = await fetch('https://example.com/image.jpg')
if (!yodaResponse.ok) {
throw new Error('Failed to fetch Yoda image')
}
const yodaArrayBuffer = await yodaResponse.arrayBuffer()
const yodaImageUint8Array = new Uint8Array(yodaArrayBuffer)この
4. 説明文を組み合わせて詳細なプロンプトを作成する
モデルAPIやgenerateText({})APIには、
この
モデルの
const result = await generateText({
model: googleProvider('gemini-2.5-flash-image'),
prompt: [
{
role: 'user',
content: [
{
type: 'text',
text: 'COMPOSITION INSTRUCTIONS: Generate a single, hyper-realistic, high-detail selfie photograph. The foundational scene and background must be derived exclusively from the Yoda background image (shown below); this setting serves as the environment for the final image.'
},
{
type: 'text',
text: '[Yoda Background Image]'
},
{
type: 'image',
image: yodaImageUint8Array,
mediaType: 'image/jpeg'
},
{
type: 'text',
text: 'Integrate the person from the user selfie image (shown below) into the Yoda background, placing them standing close to Yoda. The final image should have a selfie-style perspective (close-up, intimate framing) as if taken from a first-person viewpoint, but DO NOT include any visible hands, smartphones, or camera equipment in the frame. The image should appear as if the camera is invisible, capturing a natural moment without showing the device or hand holding it.'
},
{
type: 'text',
text: '[User Selfie Image]'
},
{
type: 'image',
image: userImageUint8Array,
mediaType: imageFile.type || 'image/jpeg'
},
{
type: 'text',
text: 'Both individuals must be smiling, looking directly into the lens, and their precise facial likenesses, clothing, and details from tyouheir respective input images must be accurately and seamlessly preserved. Maintain consistent, warm, soft, natural daylight and ensure the overall vibe is joyful and spontaneous. The final image must appear as a spontaneous, single-shot photograph taken in the original environment of the Yoda background image. IMPORTANT: Do not include any visible hands, smartphones, phones, or camera equipment in the final image - only show the people and background.'
}
]
}
]
})プロンプトのcontent配列内で
プロンプトを
5. AI駆動開発でモデルのルールを活用する
Gemini CLI、
この.cursor/rules/ai-sdk-google-provider-docs.mdc次に、
---
alwaysApply: false
description: AI SDK API documentation for the Google Gemini LLM provider
globs: server/api/generate-selfie.post.ts
---
# Google Generative AI Provider
The [Google Generative AI](https://ai.google.dev) provider contains language and embedding model support for
the [Google Generative AI](https://ai.google.dev/api/rest) APIs.
// ...
// REST OF THE CONTENT
// ...AI SDKの
6. Snyk Studioでセキュリティを確保しながらバイブコーディング
バイブコーディングや
実
Snyk Studioは、
Snyk Studioは

バイブコーディングに
オンデマンドワークショップ
バイブコーディングを 安全に :AI生成コードの セキュリティ課題に 対処する
Snykの