og-image/src/page/Home.vue
Simon Vieille 88ea3c551a
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
add font selector
2023-01-31 21:09:18 +01:00

346 lines
15 KiB
Vue

<template>
<div class="grid grid-cols-12 col-span-12">
<div class="col-span-3 flex flex-col gap-4 lg:gap-6">
<div class="drop-shadow-md rounded-md border border-zinc-300 p-5">
<div>
<label class="block text-sm font-medium text-gray-700" for="heading">Heading</label>
<input type="text" class="mt-2 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-model="heading" id="heading">
<input type="range" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" min="1" max="10" step="0.1" v-model="headingSize" id="heading-size">
</div>
<div class="py-6">
<label class="block text-sm font-medium text-gray-700" for="subheading">Subheading</label>
<textarea class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-model="subheading" id="subheading"></textarea>
<input type="range" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" min="1" max="10" step="0.1" v-model="subheadingSize" id="subheading-size">
</div>
<div class="pb-6">
<label class="block text-sm font-medium text-gray-700" for="subheading">Font</label>
<select v-model="font" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
<option v-for="font in fonts" :key="font.value" :value="font.value" v-text="font.label"></option>
</select>
</div>
<div>
<div class="text-center stroke-gray-400">
<button v-on:click="headingAlign = 'left'">
<svg class="inline border-gray-400 rounded-md" :class="{border: headingAlign === 'left'}" height="40" viewBox="0 0 21 21" width="40" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"><path d="m4.5 6.5h12"/><path d="m4.498 10.5h5.997"/><path d="m4.5 14.5h9.995"/></g></svg>
</button>
<button v-on:click="headingAlign = 'center'">
<svg class="inline border-gray-400 rounded-md" :class="{border: headingAlign === 'center'}" height="40" viewBox="0 0 21 21" width="40" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"><path d="m4.5 6.5h12"/><path d="m7.498 10.5h5.997"/><path d="m5.5 14.5h9.995"/></g></svg>
</button>
<button v-on:click="headingAlign = 'right'">
<svg class="inline border-gray-400 rounded-md" :class="{border: headingAlign === 'right'}" height="40" viewBox="0 0 21 21" width="40" xmlns="http://www.w3.org/2000/svg"><g fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"><path d="m4.5 6.5h12"/><path d="m10.498 10.5h5.997"/><path d="m6.5 14.5h9.995"/></g></svg>
</button>
</div>
</div>
</div>
<div class="drop-shadow-md rounded-md border border-zinc-300 p-5">
<div>
<label class="block text-sm font-medium text-gray-700" for="author">Author</label>
<input type="text" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-model="author" id="author">
</div>
<div class="py-6">
<label class="block text-sm font-medium text-gray-700" for="avatar">Avatar</label>
<input type="file" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-on:change="updateAvatar" id="avatar">
</div>
<div>
<label class="block text-sm font-medium text-gray-700" for="logo">Logo</label>
<input type="file" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-on:change="updateLogo" id="logo">
</div>
</div>
</div>
<div class="col-span-3 px-10 flex flex-col gap-4 lg:gap-6">
<div class="drop-shadow-md rounded-md border border-zinc-300 p-5">
<div>
<label class="block text-sm font-medium text-gray-700" for="text-color">Text color</label>
<input type="color" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-model="textColor" id="text-color">
</div>
<div class="py-6">
<label class="block text-sm font-medium text-gray-700" for="background">Background</label>
<input type="file" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-on:change="updateBackground" id="background">
</div>
<div>
<label class="block text-sm font-medium text-gray-700" for="background-hover">Background hover</label>
<input type="color" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-model="backgroundHover" id="background-hover">
<input type="range" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" min="0" max="1" step="0.1" v-model="backgroundHoverOpacity" id="background-hover-opacity">
</div>
</div>
<div class="drop-shadow-md rounded-md border border-zinc-300 p-5">
<div>
<label class="block text-sm font-medium text-gray-700" for="width">Size and quality</label>
<div class="grid grid-cols-2 gap-x-4">
<input type="number" class="mt-1 inline rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-model="width" id="width">
<input type="number" class="mt-1 inline rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" v-model="height">
</div>
<input type="range" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" min="0" max="1" step="0.1" v-model="quality">
</div>
<div>
<label class="block text-sm font-medium text-gray-700" for="width">Box padding</label>
<input type="range" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50" min="2.5" max="10" step="0.1" v-model="contentPadding">
</div>
</div>
</div>
<div class="col-span-6 space-y-6" ref="ogwrapper">
<div class="og" ref="og" :style="ogStyle()">
<div class="og-image" :style="ogImageStyle()">
<div class="og-image-hover" :style="ogImageHoverStyle()">
<div class="h-full flex flex-col justify-between" :style="ogContentStyle()">
<div>
<h1 class="leading-none" v-text="heading" :style="ogHeadingStyle()"></h1>
<p class="mt-10 mb-16 leading-tight" v-text="subheading" :style="ogSubheadingStyle()"></p>
</div>
<div class="w-full flex flex-row items-center og-image--footer">
<img class="mr-4 h-10 w-10 rounded-full" v-if="avatar" :src="avatar">
<span class="mr-auto" v-text="author"></span>
<img v-if="logo" class="mr-4 h-10 w-10 rounded-full" :src="logo">
</div>
</div>
</div>
</div>
</div>
<div class="rounded-md text-center" role="group">
<button type="button" class="px-4 py-2 text-sm font-medium text-gray-900 bg-white border border-gray-200 rounded-l-lg hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-2 focus:ring-blue-700 focus:text-blue-700 dark:bg-gray-700 dark:border-gray-600 dark:text-white dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-blue-500 dark:focus:text-white" v-on:click="downloadAsPng">
PNG
</button>
<button type="button" class="px-4 py-2 text-sm font-medium text-gray-900 bg-white border border-gray-200 rounded-r-md hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-2 focus:ring-blue-700 focus:text-blue-700 dark:bg-gray-700 dark:border-gray-600 dark:text-white dark:hover:text-white dark:hover:bg-gray-600 dark:focus:ring-blue-500 dark:focus:text-white" v-on:click="downloadAsJpeg">
JPEG
</button>
</div>
</div>
</div>
</template>
<style>
.og {
background: #cecece;
max-width: 10px;
margin: auto;
}
.og-heading {
font-size: 5em;
}
.og-subheading {
font-size: 3em;
}
.og-image--footer {
font-size: 1.3em;
}
</style>
<script>
import { hexToRgb } from '../util/color'
import { toBase64 } from '../util/file'
import { toPng, toJpeg } from 'html-to-image'
export default {
data() {
return {
author: localStorage.getItem('author'),
heading: localStorage.getItem('heading'),
subheading: localStorage.getItem('subheading'),
headingAlign: localStorage.getItem('headingAlign') ?? 'left',
font: localStorage.getItem('font') ?? 'Trebuchet MS',
avatar: localStorage.getItem('avatar'),
logo: localStorage.getItem('logo'),
textColor: localStorage.getItem('textColor') ?? '#333333',
background: localStorage.getItem('background'),
backgroundHover: localStorage.getItem('backgroundHover') ?? '#000000',
backgroundHoverOpacity: localStorage.getItem('backgroundHoverOpacity') ?? 0,
headingSize: localStorage.getItem('headingSize') ?? 4,
subheadingSize: localStorage.getItem('subheadingSize') ?? 2,
width: localStorage.getItem('width') ?? 1920,
height: localStorage.getItem('height') ?? 1080,
quality: localStorage.getItem('quality') ?? 0.9,
contentPadding: localStorage.getItem('contentPadding') ?? 2.5,
ogMaxWidth: 100,
fonts: [
{label: 'Arial (sans-serif)', value : 'Arial'},
{label: 'Brush Script MT (cursive)', value : 'Brush Script MT'},
{label: 'Calibri (sans-serif)', value : 'Calibri'},
{label: 'Courier New (monospace)', value : 'Courier New'},
{label: 'Garamond (serif)', value : 'Garamond'},
{label: 'Georgia (serif)', value : 'Georgia'},
{label: 'Tahoma (sans-serif)', value : 'Tahoma'},
{label: 'Times New Roman (serif)', value : 'Times New Roman'},
{label: 'Trebuchet MS (sans-serif)', value : 'Trebuchet MS'},
{label: 'Verdana (sans-serif)', value : 'Verdana'},
],
}
},
methods: {
ogStyle() {
return {
color: this.textColor,
width: `${this.width}px`,
maxWidth: `${this.ogMaxWidth}px`,
fontFamily: this.font,
}
},
ogImageStyle() {
if (this.background) {
return {
background: `url(${this.background}) center`,
backgroundSize: 'cover',
}
}
},
ogHeadingStyle() {
return {
fontSize: `${this.headingSize}em`,
textAlign: this.headingAlign,
}
},
ogSubheadingStyle() {
return {
fontSize: `${this.subheadingSize}em`,
textAlign: this.headingAlign,
}
},
ogImageHoverStyle() {
let background = 'none'
if (this.backgroundHover) {
const colors = hexToRgb(this.backgroundHover)
background = `rgba(${colors.red}, ${colors.green}, ${colors.blue}, ${this.backgroundHoverOpacity})`
}
return {
background,
aspectRatio: `${this.width}/${this.height}`
}
},
ogContentStyle() {
return {
padding: `${this.contentPadding}em`,
}
},
async updateBackground(event) {
this.background = await this.convertFileToBase64(event.target.files)
},
async updateLogo(event) {
this.logo = await this.convertFileToBase64(event.target.files)
},
async updateAvatar(event) {
this.avatar = await this.convertFileToBase64(event.target.files)
},
downloadAsPng() {
return this.download(toPng, 'image.png')
},
downloadAsJpeg() {
return this.download(toJpeg, 'image.jpg')
},
download(callback, filename) {
const element = this.$refs.og
callback(this.$refs.og, {
canvasWidth: this.width,
canvasHeight: this.height,
quality: this.quality,
})
.then((dataUrl) => {
const link = document.createElement('a')
link.setAttribute('href', dataUrl)
link.setAttribute('download', filename)
link.click()
})
.catch((error) => {
element.classList.toggle('og--scale', false)
console.error('oops, something went wrong!', error);
})
},
async convertFileToBase64(files) {
if (files.length) {
return await toBase64(files[0])
}
return null
},
updateOgMaxWidth() {
this.$refs.og.style.display = 'none'
this.ogMaxWidth = this.$refs.ogwrapper.offsetWidth
this.$refs.og.style.display = 'block'
},
},
watch: {
author(value) {
localStorage.setItem('author', value)
},
heading(value) {
localStorage.setItem('heading', value)
},
subheading(value) {
localStorage.setItem('subheading', value)
},
avatar(value) {
localStorage.setItem('avatar', value)
},
logo(value) {
localStorage.setItem('logo', value)
},
textColor(value) {
localStorage.setItem('textColor', value)
},
background(value) {
localStorage.setItem('background', value)
},
backgroundHover(value) {
localStorage.setItem('backgroundHover', value)
},
backgroundHoverOpacity(value) {
localStorage.setItem('backgroundHoverOpacity', value)
},
headingSize(value) {
localStorage.setItem('headingSize', value)
},
subheadingSize(value) {
localStorage.setItem('subheadingSize', value)
},
headingAlign(value) {
localStorage.setItem('headingAlign', value)
},
width(value) {
localStorage.setItem('width', value)
},
height(value) {
localStorage.setItem('height', value)
},
quality(value) {
localStorage.setItem('quality', value)
},
contentPadding(value) {
localStorage.setItem('contentPadding', value)
},
font(value) {
localStorage.setItem('font', value)
},
},
mounted() {
this.updateOgMaxWidth()
window.addEventListener('resize', this.updateOgMaxWidth, false)
}
}
</script>