Vue.js
Real-time image & video resizing, automatic optimization, and file uploading in Vue.js using ImageKit.io.
Vue.js
This quick start guide shows you how to integrate ImageKit in the Vue.js application. The code samples covered here are hosted on GitHub - https://github.com/imagekit-samples/quickstart/tree/master/vuejs.
This guide walks you through the following topics:
Setup ImageKit Vue.js SDK
Install Vue CLI
Create a project
Let's create a dummy project called vuejs using vue CLI.
It will prompt the vue version and a few other options, select default by pressing enter. It will create a dummy project like this:
Install imagekitio-vue
Initialize SDK
urlEndpoint
is the required parameter. You can get the value of URL-endpoint from your ImageKit dashboard - https://imagekit.io/dashboard/url-endpoints.publicKey
andauthenticator
parameters are optional and only needed if you want to use the SDK for client-side file upload.publicKey
can be obtained from the Developer section on your ImageKit dashboard.authenticator
expects an asynchronous function that resolves with an object containing the necessary security parameters i.e signature, token, and expire.
Let's modify src/components/HelloWorld.vue to import and initialize ImageKit as a plugin. Replace your_url_endpoint
etc, with actual values.
This SDK provides 4 components:
IKImage
for image resizing. The output is a<img>
tag.IKVideo
for video resizing. This renders a<video>
tag.IKUpload
for file uploading. The output is a<input type="file">
tag.IKContext
for definingurlEndpoint
,publicKey
andauthenticator
to all children elements.
You can import components individually.
Rendering images in Vue.js
Loading image using a relative path
Let's render an image at path /default-image.jpg
.
renders to:
The final result looks like this:
Loading image from an absolute path
If you have an absolute image path coming from the backend API e.g. https://www.custom-domain.com/default-image.jpg
then you can use src
prop to load the image.
For example:
The output looks like this:
Setting ImageKit context for the SDK
It is not necessary to specify the urlEndpoint
in every instance of IKImage
. This can be managed much more easily with the IKContext
component.
IKContext
is a wrapper that can be configured with your SDK initialization parameters. Pass your urlEndpoint
to it as a prop, and you're good to go!
Let's go ahead and import it within the file:
Now add the IKContext
component to the render function:
Let's nest our IKImage
components within it, so that those can access the urlEndpoint
from the context wrapper.
Common image manipulation in Vue.js
This section covers the basics:
The Vuejs SDK gives a name to each transformation parameter e.g. height
for h
and width
for w
parameter. It makes your code more readable. If the property does not match any of the available options, it is added as it is. See the full list of supported transformations in Vuejs SDK on Github.
👉 Note that you can also use h
and w
parameter instead of height
and width
. See the complete list of transformations supported in ImageKit here.
Resizing images in Vue.js
Let's resize the image to width 300 and height 300.
The output looks like:
Quality manipulation
You can use the quality parameter to change quality like this:
The output is:
Chained transformation
You can pass more than one object in transformation
prop to chain these transformations sequentially.
For example, the following values will first resize the image to width 300, height 300, and then rotate to 90 degrees.
So the following:
renders to:
Adding overlays
ImageKit.io enables you to apply overlays to images and videos using the raw parameter with the concept of layers. The raw parameter facilitates incorporating transformations directly in the URL. A layer is a distinct type of transformation that allows you to define an asset to serve as an overlay, along with its positioning and additional transformations.
Text as overlays
You can add any text string over a base video or image using a text layer (l-text).
For example:
Sample Result URL
Output Image:
Image as overlays
You can add an image over a base video or image using an image layer (l-image).
For example:
Sample Result URL
Output Image:
Solid color blocks as overlays
You can add solid color blocks over a base video or image using an image layer (l-image).
For example:
Sample Result URL
Output Image:
Lazy-loading images in Vue.js
You can lazy load images using the loading
prop in IKImage
component. When you use loading="lazy"
, all images that are immediately viewable without scrolling load normally. Those that are far below the device viewport are only fetched when the user scrolls near them.
The SDK uses a fixed threshold based on the effective connection type to ensure that images are loaded early enough to finish loading once the user scrolls near them.
On fast connections (e.g. 4G), the value of the threshold is 1250px
and on slower connections (e.g. 3G), it is 2500px
.
You should always set the height
and width
of image element to avoid layout shift when lazy-loading images.
Example usage:
Blurred image placeholder
To improve user experience, you can use a low-quality blurred variant of the original image as a placeholder while the original image is being loaded in the background. Once the loading of the original image is finished, the placeholder is replaced with the original image.
By default, the SDK uses the quality:20
and blur:6
. You can change this. For example:
Combining lazy loading with low-quality placeholders
You have the option to lazy-load the original image only when the user scrolls near them. Until then, only a low-quality placeholder is loaded. This saves a lot of network bandwidth if the user never scrolls further down.
Uploading files in Vue.js
Vuejs SDK provides an IKUpload
component, which can generate an input type="file"
tag that you can use to upload files to the ImageKit media library directly from the client-side.
For using upload functionality, we need to pass publicKey
and authenticator
in IKContext
. Replace your_url_endpoint
, your_public_key
, your_authentication_endpoint
with actual values.
Make sure that you have specified
authenticator
andpublicKey
in the parent IKContext component as a prop. The authenticator expects an asynchronous function that resolves with an object containing the necessary security parameters i.e signature, token, and expire. Endpoint forauthenticator
should be implemented in your backend. Learn how to implement endpoint for authenticator on your server.
It is advised to setup a backend server for the creation of these security parameters. In the frontend an HTTP GET request can be made to fetch them using the authenticator
function.
For this quickstart guide, we have provided a sample implementation of http://localhost:3001/auth
in Node.js.
Let's create a file index.js
inside server
folder in the project root. It should look like this:
We will use the ImageKit Node.js SDK to implement http://localhost:3001/auth
.
Let's first install all basic modules we need to run an HTTP server.
Let's modify index.js to implement http://localhost:3001/auth
which is our authenticationEndpoint
.
Let's run the backend server.
If you GET http://localhost:3001/auth, you should see a JSON response like this. Actual values will vary.
Let's include IKUpload
component in the HelloWorld.vue
.
The output looks like:
When you choose a file, the file is uploaded. You can pass optional onSuccess
, onUploadStart
, onUploadProgress
, validateFile
and onError
callback functions as props like we have.
After successful upload, you should see the upload API response in the console like this:
Abort upload
ref
can be passed to obtain access to the IKUpload component's instance. Calling the triggerAbortUpload
method will abort the upload if any is in progress.
Example Usage
Rendering videos
Rendering videos works similarly to rendering images in terms of usage of urlEndpoint
param (either directly or via IKContext
).
Loading video from relative path: Import IKVideo
from the SDK:
Now let's add it to our App. Along with the video path prop, it also needs the relevant urlEndpoint
(either directly or via IKContext
):
A more complex example:
What's next
The possibilities for image manipulation and optimization with ImageKit are endless. Learn more about it here:
Last updated