Skip to content

Vue.js Tutorial: Image Optimization for Web Apps

Launched in 2014 as an open-source JavaScript framework for building user interfaces for web apps, Vue.js has become a tool of choice for many developers. Currently, Vue.js is the most starred front-end JavaScript framework on GitHub with over 145,000 stars.

Optimizing images that reside in Vue.js apps with the Cloudinary Vue SDK is a cinch. This article explains why and describes the steps.

The smaller an image, the faster the browser can download it for display. Optimizing images means delivering them in the smallest-possible size without losing visual quality, leading to bandwidth savings and faster loading of websites.

That image optimization is mandatory for all websites is a case that can easily be made. That’s because faster image loading on websites invariably lifts audience conversion rates. In fact, studies have shown that half the online visitors expect a website to load within two seconds.

With Cloudinary, you can effectively and efficiently optimize digital media—regardless of the programming language in which you code—because Cloudinary automatically performs most of the related tasks by default. Besides, the platform’s delivery process through integrated content delivery networks (CDNs) ensures prompt transmission to both desktop and mobile audiences.

After uploading images to Cloudinary, you can leverage its major capabilities, as described in the subsections below. For details on other Cloudinary options for optimizing images, read the documentation section Image Transformations.

Adding the q_auto parameter to an image’s URL enables Cloudinary to select the optimal quality-compression level and encoding settings according to the image content, format, and viewing browser. The result is, gratifyingly, a smaller image that still retains its original visual quality. See this example:

woman

To further adjust the visual quality, edit q_auto to read q_auto:best, q_auto:low, q_auto:good, or q_auto:eco.

Adding the f_auto parameter to an image’s URL enables Cloudinary to analyze the image content and select the best format for delivery, such as WebP for Chrome browsers or JPEG-XR for Internet Explorer browsers. For all other browsers, Cloudinary delivers images in their original format.

Note:

With f_auto and q_auto set, Cloudinary would, as a rule, pick WebP or JPEG-XR for display on Chrome or IE, respectively. However, if the quality algorithm determines that PNG-8 or PNG-24 is optimal, Cloudinary might pick either of those formats for certain images.

To resize an image, add either or both of these two parameters to the URL: width (w) and height (h). Cloudinary leaves the aspect ratio as is while performing resizing tasks.

Here are three examples:

sample sample sample

To have Cloudinary crop an image, add the crop (c) parameter to the URL and specify one of these modes: scale, fit, mfit, fill, lfill, limit, pad, lpad, mpad, crop, thumb, imagga_crop, or imagga_scale.

This section describes how to optimize images in Vue.js apps with the Cloudinary SDK.

You can edit the quality level of images in Vue.js apps with JavaScript code, for example:

<cld-image publicId="cave.jpg" ><cld-transformation quality="50" /></cld-image>
Code language: HTML, XML (xml)

Here, under quality, 50 is your choice of quality level on a 0-100 scale. The lower the level, the smaller the image size. The higher the level, the larger the size and the better the quality.

But why do it manually like that? Instead, automate that compression task by taking advantage of Cloudinary with this code:

<cld-image publicId="prosper.jpg" > <cld-transformation quality="auto" /> </cld-image>
Code language: HTML, XML (xml)

Adding the quality=”auto” parameter is the best way to significantly reduce image size through automation.

To further narrow the size or to ensure higher-than-average quality, replace auto with auto:best , auto:good, auto:eco, or auto:low, for example:

<cld-image publicId="prosper.jpg" > <cld-transformation quality="auto:low" /> </cld-image>
Code language: HTML, XML (xml)

Adding the fetchFormat=”auto” parameter is the best way to ensure that Cloudinary delivers your images in the optimum format to the viewing browser in question. See this code example:

<cld-image publicId="prosper.jpg" > <cld-transformation width="500" fetchFormat="auto" crop="scale" /> </cld-image>
Code language: HTML, XML (xml)

Vue.js

By adding the relevant parameters to the <cld-image> and <cld-transformation> Vue.js components, you can resize and crop images, for example:

<cld-image publicId="family_bench.jpg" > <cld-transformation width="150" height="150" gravity="faces" crop="fill" /> </cld-image>
Code language: HTML, XML (xml)

The fill cropping method generates an image size of 150 x 150 pixels while retaining the original aspect ratio.

To change the size, assign new values to the width and height parameters. Note this neat Cloudinary capability: if you set only width or height, Cloudinary automatically updates the other parameter to maintain the aspect ratio. An example:

<cld-image publicId="prosper.jpg" > <cld-transformation width="0.5" crop="scale" /> </cld-image>
Code language: HTML, XML (xml)

Here, despite the width adjustment with the 0.5 value, the aspect ratio remains intact.

Another example:

<cld-image publicId="prosper.jpg" > <cld-transformation width="200" height="100" crop="scale" /> </cld-image>
Code language: HTML, XML (xml)

Here, too, the aspect ratio stays the same notwithstanding the resizing to a width of 200 and a height of 100 pixels.

This Vue.js tutorial for Image optimization is paramount for the performance of web apps. Do mount the effort to properly optimize and cache their images. For an excellent insight and suggestions on how to optimize JPEG images without compromising quality, read this Cloudinary article.


Back to top

Featured Post