Cloudinary Blog

Vue.js Tutorial: Image Optimization for Web Apps

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.

Webinar
How to Optimize for Page Load Speed

Background

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.

Cloudinary the Media Full-Stack Platform

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.

Automatic Quality Adjustment and Encoding

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:

Ruby:
Copy to clipboard
cl_image_tag("woman.jpg", :quality=>"auto")
PHP:
Copy to clipboard
cl_image_tag("woman.jpg", array("quality"=>"auto"))
Python:
Copy to clipboard
CloudinaryImage("woman.jpg").image(quality="auto")
Node.js:
Copy to clipboard
cloudinary.image("woman.jpg", {quality: "auto"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().quality("auto")).imageTag("woman.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('woman.jpg', {quality: "auto"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("woman.jpg", {quality: "auto"})
React:
Copy to clipboard
<Image publicId="woman.jpg" >
  <Transformation quality="auto" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="woman.jpg" >
  <cld-transformation quality="auto" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="woman.jpg" >
  <cl-transformation quality="auto">
  </cl-transformation>
</cl-image>
.Net:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Quality("auto")).BuildImageTag("woman.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().quality("auto")).generate("woman.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setQuality("auto")).generate("woman.jpg")!, cloudinary: cloudinary)
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.

Automatic Formatting

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.

Resizing and Cropping

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:

Ruby:
Copy to clipboard
cl_image_tag("sample.jpg", :width=>0.5, :crop=>"scale")
PHP:
Copy to clipboard
cl_image_tag("sample.jpg", array("width"=>0.5, "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("sample.jpg").image(width=0.5, crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("sample.jpg", {width: "0.5", crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().width(0.5).crop("scale")).imageTag("sample.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('sample.jpg', {width: "0.5", crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("sample.jpg", {width: "0.5", crop: "scale"})
React:
Copy to clipboard
<Image publicId="sample.jpg" >
  <Transformation width="0.5" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="sample.jpg" >
  <cld-transformation width="0.5" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="sample.jpg" >
  <cl-transformation width="0.5" crop="scale">
  </cl-transformation>
</cl-image>
.Net:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(0.5).Crop("scale")).BuildImageTag("sample.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().width(0.5).crop("scale")).generate("sample.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setWidth(0.5).setCrop("scale")).generate("sample.jpg")!, cloudinary: cloudinary)
sample
Ruby:
Copy to clipboard
cl_image_tag("sample.jpg", :height=>200, :crop=>"scale")
PHP:
Copy to clipboard
cl_image_tag("sample.jpg", array("height"=>200, "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("sample.jpg").image(height=200, crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("sample.jpg", {height: 200, crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().height(200).crop("scale")).imageTag("sample.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('sample.jpg', {height: 200, crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("sample.jpg", {height: 200, crop: "scale"})
React:
Copy to clipboard
<Image publicId="sample.jpg" >
  <Transformation height="200" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="sample.jpg" >
  <cld-transformation height="200" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="sample.jpg" >
  <cl-transformation height="200" crop="scale">
  </cl-transformation>
</cl-image>
.Net:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Height(200).Crop("scale")).BuildImageTag("sample.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().height(200).crop("scale")).generate("sample.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setHeight(200).setCrop("scale")).generate("sample.jpg")!, cloudinary: cloudinary)
sample
Ruby:
Copy to clipboard
cl_image_tag("sample.jpg", :width=>200, :height=>100, :crop=>"scale")
PHP:
Copy to clipboard
cl_image_tag("sample.jpg", array("width"=>200, "height"=>100, "crop"=>"scale"))
Python:
Copy to clipboard
CloudinaryImage("sample.jpg").image(width=200, height=100, crop="scale")
Node.js:
Copy to clipboard
cloudinary.image("sample.jpg", {width: 200, height: 100, crop: "scale"})
Java:
Copy to clipboard
cloudinary.url().transformation(new Transformation().width(200).height(100).crop("scale")).imageTag("sample.jpg");
JS:
Copy to clipboard
cloudinary.imageTag('sample.jpg', {width: 200, height: 100, crop: "scale"}).toHtml();
jQuery:
Copy to clipboard
$.cloudinary.image("sample.jpg", {width: 200, height: 100, crop: "scale"})
React:
Copy to clipboard
<Image publicId="sample.jpg" >
  <Transformation width="200" height="100" crop="scale" />
</Image>
Vue.js:
Copy to clipboard
<cld-image publicId="sample.jpg" >
  <cld-transformation width="200" height="100" crop="scale" />
</cld-image>
Angular:
Copy to clipboard
<cl-image public-id="sample.jpg" >
  <cl-transformation width="200" height="100" crop="scale">
  </cl-transformation>
</cl-image>
.Net:
Copy to clipboard
cloudinary.Api.UrlImgUp.Transform(new Transformation().Width(200).Height(100).Crop("scale")).BuildImageTag("sample.jpg")
Android:
Copy to clipboard
MediaManager.get().url().transformation(new Transformation().width(200).height(100).crop("scale")).generate("sample.jpg");
iOS:
Copy to clipboard
imageView.cldSetImage(cloudinary.createUrl().setTransformation(CLDTransformation().setWidth(200).setHeight(100).setCrop("scale")).generate("sample.jpg")!, cloudinary: cloudinary)
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.

Vue.js Tutorial: Image Optimization in Web Apps

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

Automatic Quality and Encoding

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

Copy to clipboard
<cld-image publicId="cave.jpg" ><cld-transformation quality="50" /></cld-image>

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:

Copy to clipboard
<cld-image publicId="prosper.jpg" >
  <cld-transformation quality="auto" />
</cld-image>

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:

Copy to clipboard
<cld-image publicId="prosper.jpg" >
  <cld-transformation quality="auto:low" />
</cld-image>

Automatic Formatting

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:

Copy to clipboard
<cld-image publicId="prosper.jpg" >
  <cld-transformation width="500" fetchFormat="auto" crop="scale" />
</cld-image>

Vue.js

Image Resizing and Cropping

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

Copy to clipboard
<cld-image publicId="family_bench.jpg" >
  <cld-transformation width="150" height="150" gravity="faces" crop="fill" />
</cld-image>

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:

Copy to clipboard
<cld-image publicId="prosper.jpg" >
  <cld-transformation width="0.5" crop="scale" />
</cld-image>

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

Another example:

Copy to clipboard
<cld-image publicId="prosper.jpg" >
  <cld-transformation width="200" height="100" crop="scale" />
</cld-image>

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

Conclusion

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.

About Cloudinary

Cloudinary provides easy-to-use, cloud-based media management solutions for the world’s top brands. With offices in the US, UK and Israel, Cloudinary has quickly become the de facto solution used by developers and marketers at major companies around the world to streamline rich media management and deliver optimal end-user experiences.

For more information, visit www.cloudinary.com or follow us on Twitter


Further Reading on Image Optimization

Recent Blog Posts

New Learning Pathways From the Cloudinary Academy

In December 2019, Cloudinary launched its customer education platform, the Cloudinary Academy, replete with courses taught by the company’s experts on developer-oriented products and digital asset management (DAM) solution. The courses comprise interactive lessons and hands-on assignments, a proven way of familiarizing the audience with the course material and illustrating it with live examples.

Read more
Maya Shavin: How I Built My Website

Besides working as a senior front-end developer at Cloudinary, I'm also a content creator, a blogger, and an open-source developer. Follow me at @mayashavin and on mayashavin.com.

In the beginning, my website, mayashavin.com, was mainly for showcasing the status of my development projects and keeping me organized with my speaking schedule. Initially, I built it with Vue.js, later on switching to Nuxt.js (aka Nuxt) for a higher SEO score, and deployed it with Netlify. After some time, I added a blog section with Netlify CMS as the content management system (CMS). Everything was fine until I added more content and features, which led to a significant decline in the site’s performance. Also, the site design needed a modern look. So, I gave the site a makeover.

Read more
Automation Frees Up PetRescue’s Staff to Help Pets Find Their Forever Homes

As we spend more time at home, many of us are adopting pets for the joy, companionship and a surprising range of health benefits. In Australia, where our nonprofit customer PetRescue is located, there’s a shortage of pets to adopt. Last August, the Guardian reported that dog shelters in Australia emptied and adoption fees for puppies were running as high as $AUS1800.

Read more
Cloudinary and Contentful Make Modern Content Management Easier

I am pleased to share that Cloudinary and Contentful have joined forces to further streamline the creation, processing, and delivery of online content through Cloudinary’s digital asset management (DAM) solution and advanced transformation and delivery capabilities for images and video. What’s more, the partnership delivers a headless approach to DAM. By leveraging APIs for media management tasks, marketers and developers alike benefit from an integrated stack of optimized assets for optimization and automation. As a result, page loads are fast and beautiful, and at scale—with less overhead and effort.

Read more
Introducing Cloudinary's Nuxt Module

Since its initial release in October 2016 by the Chopin brothers as a server-side framework that runs on top of Vue.js, Nuxt (aka Nuxt.js) has gained prominence in both intuitiveness and performance. The framework offers numerous built-in features based on a modular architecture, bringing ease and simplicity to web development. Not surprisingly, Nuxt.js has seen remarkable growth in adoption by the developer community along with accolades galore. At this writing, Nuxt has earned over 30K stars on GitHub and 96 active modules with over a million downloads per month. And the upward trend is ongoing.

Read more