Giter Club home page Giter Club logo

get-share-image's Introduction

Generate Social Media Images Using Cloudinary

This is a utility function that builds social media images by overlaying a title and tagline over an image using Cloudinary’s APIs.

NOTE: a Cloudinary account is required to use this package. The free tier should be more than enough for most small to medium websites using this package. Sign up for an account here!

This was created as part of an article series:

Installation

# install using npm
npm install --save @jlengstorf/get-share-image

# install using yarn
yarn add @jlengstorf/get-share-image

See how this is used in a production site in the learnwithjason.dev source code.

Example Usage

import getShareImage from '@jlengstorf/get-share-image';

const socialImage = getShareImage({
  title: 'Deploy a Node.js App to DigitalOcean with SSL',
  tagline: '#devops #nodejs #ssl',
  cloudName: 'jlengstorf',
  imagePublicID: 'lwj/blog-post-card',
  titleFont: 'futura',
  taglineFont: 'futura',
  textColor: '232129',
});

This generates an image URL:

https://res.cloudinary.com/jlengstorf/image/upload/w_1280,h_669,c_fill,q_auto,f_auto/w_760,c_fit,co_rgb:232129,g_south_west,x_480,y_254,l_text:futura_64:Deploy%20a%20Node.js%20App%20to%20DigitalOcean%20with%20SSL/w_760,c_fit,co_rgb:232129,g_north_west,x_480,y_445,l_text:futura_48:%23devops%20%23nodejs%20%23ssl/lwj/blog-post-card

Which looks like this:

Deploy a Node.js App to DigitalOcean with SSL, from learnwithjason.dev

Options

This utility function accepts a config object. Available options are as follows:

name required description
title true (string) title text to be placed on the card
tagline (string) tagline text to be placed on the card
cloudName true (string) your Cloudinary cloud name (i.e. your username)
imagePublicID true (string) the public ID of your social image template
cloudinaryUrlBase (string, default https://res.cloudinary.com) Cloudinary asset URL
titleFont (string, default arial) font to use for rendering title
titleExtraConfig (string) optional additional text overlay config
taglineExtraConfig (string) optional additional text overlay config
taglineFont (string, default arial) font to use for rendering tagline
imageWidth (number, default 1280) SEO image width (defaults to Twitter ratio)
imageHeight (number, default 669) SEO image height (defaults to Twitter ratio)
textAreaWidth (number, default 760) width of title and tagline text areas
textLeftOffset (number, default 480) distance from left edge to start text boxes
titleGravity (string, default south_west) location the title is anchored from
taglineGravity (string, default north_west) location the tagline is anchored from
titleLeftOffset (number, null) distance from left edge to start text boxes
taglineLeftOffset (number, default null) distance from left edge to start text boxes
titleBottomOffset (number, default 254) distance from bottom to start title text
taglineTopOffset (number, default 445) distance from top to start tagline text
textColor (string, default 000000) hex value for text color
titleColor (string) hex value specific for title color. If this is not set, the color will be the one set to textColor
taglineColor (string) hex value specific for tagline color. If this is not set, the color will be the one set to textColor
titleFontSize (number, default 64) font size to use for the title
taglineFontSize (number, default 48) font size to use for the tagline
version (string) optional version string for caching

Setting config options

const socialImage = getShareImage({
  title: 'My Post Title',
  tagline: 'A tagline for the post',
  cloudName: 'myusername',
  imagePublicID: 'my-template-image.jpg',
  titleExtraConfig: '_bold', // optional - set title font weight to bold
  textColor: '663399', // optional — set the color to purple
});

Who is using this?

get-share-image's People

Contributors

chrisberry-io avatar codebender828 avatar colbyfayock avatar dijonmusters avatar horacioh avatar jlengstorf avatar jsjoeio avatar khaledgarbaya avatar nhoizey avatar ryancharris avatar sebastiangelotte avatar tanseersaji avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

get-share-image's Issues

Double-encode special characters

Cloudinary apparently processes URL-encoded slashes, commas, etc. (!!!) so we need to double-encode them

https://support.cloudinary.com/hc/en-us/articles/202521512-How-to-add-a-slash-character-or-any-other-special-characters-in-text-overlays-

  • comma ,
  • question mark ?
  • forward slash /
  • backslash \
  • hash #

it looks like we could just encodeUriComponent(encodeUriComponent(str)), but that feels... gross? might be cleaner to just look for %2C and replace with %252C, etc.

export valid ESM so this works better with modern builds

right now we have to do this:

import getShareImageModule from '@jlengstorf/get-share-image';

const getShareImage = getShareImageModule.default;

that's fine, but not great

to fix it we need to update the TS export to target ESM properly (or drop TS support if it's too much work)

Double encoding of "#"

I’m using the tagline field to hold tags for my posts as hashtags, but the script is double-encoding them as "%2523" rather than "%23" ("%25" is "%").

Is it possible to overlay images

image

I have this social card, which I partially generated with this project, and I'm curious if it's possible to overlay the images of the people in the firstname lastnamey section.

Allow arbitrary number of text overlays

On one site, I have only a title, no tagline, hence #4.

On another site, I will need more than 2 texts.

On any site, I will need to be able to set the gravity for each text.

So maybe we could accept an array of text settings instead of just title and tagline.

Where / how to use the figma templates

Hello!

You're guide is awesome and I am trying to follow it, however its not covered/clear what/how/where to use the image template in cloudinary?

Could you enhance the article to provide more detailed instructions on this? For someone with 0 cloudinary background it gets you quite lost at this step.

different color for title and tagline

I'm working on implementing this for my website, and I was wondering, maybe is a good idea to enable users to set different colors for title and tagline.

I'm working with my fork now and when is working, I will submit a PR if you think this is interesting to enable too.

thanks for this package Jason!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.