Home
Blog
Showcase
Community
Introduction
Overview
Introduction To TinaCMS
Getting Started
Using the Tina Editor
FAQ
Core Concepts
Content Modeling
Data Fetching
Visual Editing
Querying Content
Overview
Writing custom queries
Editing
Overview
Markdown & MDX
Block-based editing
Single Document Collections
Customizing Tina
Overview
Validation
Custom Field Components
Custom List Rendering
Format and Parse Input
Filename Customization
Before Submit function
Going To Production
Overview
Tina Cloud
Self-Hosted
Drafts
Overview
Draft Fields
Editorial Workflow
Guides
Overview
Framework Guides
Separate Content Repo
Querying Tina Content at Runtime
Internationalization
Migrating From Forestry
Further Reference
Overview
Config
Schema
The "tina" folder
The TinaCMS CLI
Media
Search
Content API
Tina's edit state
The "tinaField" helper
Self-Hosted Components

Image Field


This is an advanced-use feature, and likely not something you'll need to configure. What you probably want is the content types reference!

The image field is used for content values that point to an image used on the page. This field allows you to upload new images by via dragging or selection in Finder. Note this field does not handle any images included in the Markdown body, those would be handled by the markdown component.

tinacms-image-field

Definition

interface ImageConfig {
component: 'image'
name: string
label?: string
description?: string
clearable?: boolean
parse(media: Media): string
previewSrc(formValues: any)?: string
uploadDir(formValues: any)?: string
}

KeyDescription
componentThe name of the plugin component. Always 'image'.
nameThe path to some value in the data being edited.
labelA human readable label for the field. Defaults to the name. (Optional)
descriptionDescription that expands on the purpose of the field or prompts a specific action. (Optional)
clearableWhen true, editors can 'clear' the image field to an empty state via a trash icon. (Optional)
parseDefines how the actual front matter or data value gets populated. The media object gets passed as an argument, and one can set the path this image as defined by the uploadDir property.
previewSrcDefines the path for the src attribute on the image preview. If using gatsby-image, the path to the childImageSharp.fluid.src needs to be provided. (Optional)
uploadDirDefines the upload directory for the image. All of the form data is passed in, fileRelativePath is most useful in defining the upload directory, but you can also statically define the upload directory. (Optional)

This interfaces only shows the keys unique to the image field. Visit the Field Config docs for a complete list of options.

Examples

Next.js

Below is an example of an image field defined in a Next.js project.

const formOptions = {
fields: [
{
label: 'Hero Image',
name: 'frontmatter.hero_image',
component: 'image',
// Generate the frontmatter value based on the filename
parse: (media) => `/static/${media.filename}`,
// Decide the file upload directory for the post
uploadDir: () => '/public/static/',
// Generate the src attribute for the preview image.
previewSrc: (fullSrc) => fullSrc.replace('/public', ''),
},
],
//...
}

Gatsby

Below is an example of how a image field could be defined in a Gatsby Remark form.

const BlogPostForm = {
fields: [
{
label: 'Hero Image',
name: 'rawFrontmatter.hero.image',
component: 'image',
parse: (media) => {
if (!media) return ''
return `../images/${media.filename}`
},
uploadDir: () => `/content/images/`,
previewSrc: (src, path, formValues) => {
if (!formValues.frontmatter.hero || !formValues.frontmatter.hero.image)
return ''
return formValues.frontmatter.hero.image.childImageSharp.fluid.src
},
},
// ...
],
}

Proper Image Paths in Gatsby

In order for image paths to be properly sourced into GraphQL, it's best if a relative path to the image is saved to the content file's front matter. Constructing this relative path will depend on where the image is uploaded to as well as the location of the content file. The following example uses a co-location strategy, where a blog post is stored in content/blog/$slug/index.md and its images will be uploaded to content/blog/$slug/$image.png:

import get from 'lodash.get'
const path = require('path').posix
const BlogPostForm = {
fields: [
{
name: 'rawFrontmatter.thumbnail',
label: 'Thumbnail',
component: 'image',
previewSrc: (src, path, formValues) => {
if (!formValues.frontmatter.thumbnail) return ''
return formValues.frontmatter.thumbnail.childImageSharp.fluid.src
},
// upload images to same directory as content file
uploadDir: (formValues) => path.dirname(formValues.fileRelativePath),
// image file is sibling of content file
parse: (filename) => `./${filename}`,
},
// ...
],
}

Product

Showcase
TinaCloud
Introduction
How Tina Works
Roadmap

Resources

Blog
Examples
Support
Media

Whats New
TinaCMS
TinaCloud
Use Cases
Agencies
Documentation
Teams
Jamstack CMS
Benefits
MDX
Markdown
Git
Editorial Workflow
Customization
SEO
Comparisons
TinaCMS vs Storyblok
TinaCMS vs Sanity
TinaCMS vs DecapCMS
TinaCMS vs Contentful
TinaCMS vs Builder.io
TinaCMS vs Strapi
Integrations
Astro
Hugo
NextJS
Jekyll