How to Create a Thumbnail Image with Ionic


When working on apps with image upload functionality, many times you either need to resize the image or to create something like a preview image for later requests. It’s not a super straight forward thing to to, so here’s how we can create a Thumbnail Image with Ionic.

This Quick Win is about choosing a picture from the library (you can of course also capture a new one) and calculate the size of the image. Then we will create a preview image as a thumbnail from that image and see what size we have with the new image. Spoiler: You can see it inside the little animation below!


Starting our Ionic Thumbnail Image App

We start with a blank Ionic app and install the Camera plugin so we have something to work with. Go ahead and run:

Now also make sure to add the Camera to your providers inside the src/app/app.module.ts like this:

We don’t need anything else as we will resize the image with the canvas so there’s no need for another external package!

Loading Images and creating a Preview Image

First of all we need our very basic view with buttons to load and image and to create the thumbnail. Additional we have the 2 images plus the size for both of them which we will calculate in the actual class. The view really is very dumb here, so change your src/pages/home/home.html to:

Now the actually hard part of this Quick Win begins.

The first step is simple: Inside our loadImage() function we use the Camera plugin to load an image from the camera. We specify the quality to be 100% so we can see the difference to the thumbnail later.

Of course changing the quality here will also result in a smaller image and most of the time it makes sense to drop at least some percent here. But if you want the best resolution of your users image and also a thumbnail which can be delivered faster, this approach here will work better.

Anyway, after the picture data is there we call our getImageSize() function to get the size of that image. I didn’t came up with that solution but can’t find the exact solution on SO again. Overall we need to multiplay it with 6 because of how base64 works and divide it by 8 to convert bits into bytes. Of course this means we can also use it like * 3 / 4!

The last additions are to convert that number into MB, so just some more math going on here. But this is not critical to creating our thumbnail.

To create a thumbnail image we call the generateFromImage() function with a few parameters for the size and quality and get the result inside the callback. This is the function you can copy directly into your code of you need the logic for a thumbnail!

At this point you might also add the functionality for an Ionic Gallery Zoom to show the thumbnail first and then a bigger image later.

The function takes a few steps to create a canvas element with an image. It will draw the image on that canvas with the specific size and afterwards convert the canvas back to a base64 image string but with the specified quality.

In our app we again assign that new image to a variable and calculate the size of it so our view will be updated.

Now go ahead and change your src/pages/home/home.ts to:

That’s all ne weed, and most of it lives in that one function. Make sure to run your app on a device or simulator now as the Cordova plugin won’t work on a browser!

Hopefully this will help you to dramatically reduce the size of your images especially if you work with lists of images where a preview image renders way faster than some 5 MB pictures.

You can also find a video version of this Quick Win below.

Read Next..

Ionic Instagram Image Share If your app works with images, chances are high your users want to post their output to their Instagram account. With Ionic, we can easily add this sh...
Accordion List with Ionic An Accordion list is a very classic UI pattern, for which you can find about 100 jQuery solutions. But we want to develop an accordion list with Ionic...
Building an Ionic Image Gallery With Zoom The Ionic image gallery is a classic pattern used in many applications which need to display any kind of grid with pictures or photos to the user. If ...