Effortless JavaScript Image Editing With Pintura

I’m a web developer for more than 10 years now, and I know how hard it would be to work on more serious projects without using third-party libraries.

This is why I love them so much, and in this article, I’m going to share one of my recent findings.

A gem that I deem very helpful for profile photos on a website such as a forum or LMS (learning management system), although this plugin is so much more.

It’s a paid JS plugin, but for a very good reason.

Let’s see how we can save a ton of time in the development process by using this neat image editor, compatible with most of the development stacks out there.

What is Pintura?

Pintura is an image editing tool, packed with so many great features that I don’t know where to start first.

In fact, the scope of this article simply can’t hold the number of possibilities that this plugin provides, so I’ll try focusing briefly on the main features and showing some quick examples.

In a nutshell, Pintura is a robust JS image editor, allowing you to crop, resize, rotate, apply filters, or fine-tune your images.

Pintura also supports annotation, stickers (including custom ones), removal of sensitive information from the photos (such as location), enforced crop ratio, and others.

It is responsive and works on both desktops and mobiles, very lightweight and fast to use.

And while you as a website owner can use it for your own website or project, this tool is perfect for developers.

Yes, this library is self-sufficient and can be used on your website as an image editor (check this website for example), but the real power comes in accessibility for developers and the support for a wide range of development platforms.

Should you try Pintura for your own project?

While I always encourage fellow devs to shake their heads and make the best of any tool they are trying to build, certain things are best when implemented.

I mean, if you want to build your own image editor tool, you can do that, but it can take months, especially if you need more than 1-2 features that Pintura provides.

Plus, you need to test on different devices and cover a wide range of use cases, which basically takes a solid team and a solid amount of time.

But what if you’re a solo rider?

Or if your team is working on a large-scale project, yet you want or need to make it better when it comes to image processing?

This is where Pintura comes in really handy.

The pool of use cases is rather deep, but you can think of it like this:

When you need a photo fitting certain criteria in your project, Pintura makes it effortless.

Profile photos in a certain size or ratio, filtered and styled featured images, equal images for slides, galleries, or carousels, and a website where you can strip all the privacy information from a photo… are just some of the many possibilities.

So, although someone with no coding experience can use it, Pintura shows real strength when used by developers.

It saves time and provides an elegant solution for image editing on the fly, whether automatically in the code or by allowing website users to fulfill a certain requirement or just make their photos more appealing!

We will cover the usage basics in the next section, but keep in mind that Pintura can be used in plain JS, jQuery, React, Vue, Angular, Svelte, and around 15 other frameworks.

Adding Pintura to your project

As mentioned a few times already, Pintura can be implemented in many frameworks, but to keep things simple I’m going to show you how to use the Pintura input field.

Pintura input field is a tiny wrapper over the <input type=”file”> field.

In other words, by using the Pintura input field, you will get a file upload button in HTML, but constrained to images and packed with a powerful set of options to process the image.

Once you have your account and license ready, simply log in to the dashboard and download the package.

Click on your license and download the latest release:

Unpack the Pintura archive and you will get a folder structure like this:

The locale folder contains translation files for various languages and is very useful if you need to use Pintura on a non-English project.

The packages folder contains packages for embedding Pintura in various development stacks, and the presets folder is a collection of examples on how to use Pintura in 20 different frameworks:

To locate the example we’re going to test, open the “pintura-input” folder:

As you can see, this example also contains a “pintura-input” folder, which consists of one JS and one CSS file, that power the Pintura input field.

We also have a favicon, one image for a test, and the index.html file that combines them all.

The demonstration should start working as soon as you start the index.html file:

What can we do with the Pintura input field?

As mentioned above, the Pintura input field can be used for any of your image processing needs, either predefined or based on visitor input.

Let’s say we have an LMS website where students can enroll in courses and get a certificate.

Such LMS needs profile photos of students, shown in various sections of the website, and also in the certificate.

To keep the design and layout consistent, we want each profile photo to be 256×256 pixels, but standard solutions do come with certain cons.

You can force the students to do it themselves on their PC, and then upload the proper photo.

Or use CSS to tweak photos on the fly or PHP to crop the photos as desired.

But instead of forcing students to do external work and research for the best tool, having to deal with the distortion that CSS brings or weirdly cropped photos in PHP, you can just use Pintura.

For this example, we are going to use a free stock photo from Unsplash:

The photo itself is beautiful, no questions about that, but for our needs, there are two big problems:

  • It’s not in the desired proportion or dimensions of 256×256
  • It’s actually 3744×5616 pixels, which is very big and gives a total size of 1.36MB.

Let’s go back to our example and see how Pintura can help:

<style>
	/* The empty state grey box */
	pintura-input.base [data-empty] {
		display: flex;
		align-items: center;
		justify-content: center;
	background-color: #eee;
	}

	/* To make each state equal size */
	pintura-input.base [data-empty], pintura-input.base img {
		width: 400px;
		height: 300px;
		object-fit: cover;
	}
</style>
<pintura-input class="base" src="image.jpeg" name="my_field" image-crop-aspect-ratio="4/3">
	<template>
		<div data-empty data-drop>
			<p>
				Drag &amp; Drop your image here or
		<button type="button" data-browse>Browse</button>
			</p>
		</div>
		<div data-load>
		<img src="image.jpeg" width="300" alt="" />
		<div>
			<button type="button" data-remove>Remove</button>
			<button type="button" data-edit>Edit</button>
		</div>
	</div>
		<output data-process>
			<img src="{url}" width="300" alt="" />
			<p role="status">"{filename}" saved</p>
			<div>
				<button type="button" data-remove>Remove</button>
				<button type="button" data-edit>Edit</button>
			</div>
		</output>
	</template>
</pintura-input>

As you can see in the code above, this part defines our Pintura input field, along with options to display, change or remove the image.

In order to tweak this example to our needs, we need to do just a few things:

Change the aspect ratio from:

image-crop-aspect-ratio="4/3"

to:

image-crop-aspect-ratio="4/4"

This will make sure that our profile photos are cropped as squares, without students needing to guess the right proportion.

In order to properly show the image on the front end, we also need to change the image HTML width attribute so it matches our needs of 256×256:

<img src="image.jpeg" width="300" alt="" />
<img src="{url}" width="300" alt="" />

to:

<img src="image.jpeg" width="256" alt="" />
<img src="{url}" width="256" alt="" />

We also want to tweak the CSS for the same reason:

/* To make each state equal size */
pintura-input.base [data-empty], pintura-input.base img {
	width: 256px;
	height: 256px;
	object-fit: cover;
}

and finally, we want to set the output image to be resized to 256×256 pixels, which will save us a ton of bandwidth on the server and also make it easy for web browsers to process the image in any template of our LMS:

<script>
	window.PinturaInput = {
		imageWriter: {
			targetSize: {
				width: 256,
				height: 256,
			},
		},
	};
</script>

In the end, the code should look like this:

<style>
	/* The empty state grey box */
	pintura-input.base [data-empty] {
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #eee;
	}

	/* To make each state equal size */
	pintura-input.base [data-empty], pintura-input.base img {
		width: 256px;
		height: 256px;
		object-fit: cover;
	}
</style>
<script>
	window.PinturaInput = {
		imageWriter: {
			targetSize: {
				width: 256,
				height: 256,
			},
		},
	};
</script>
<pintura-input class="base" src="image.jpeg" name="my_field" image-crop-aspect-ratio="4/4">
	<template>
		<div data-empty data-drop>
			<p>
			Drag &amp; Drop your image here or
			<button type="button" data-browse>Browse</button>
			</p>
		</div>
		<div data-load>
			<img src="image.jpeg" width="256" alt="" />
			<div>
				<button type="button" data-remove>Remove</button>
				<button type="button" data-edit>Edit</button>
			</div>
		</div>
		<output data-process>
			<img src="{url}" width="256" alt="" />
			<p role="status">"{filename}" saved</p>
			<div>
				<button type="button" data-remove>Remove</button>
				<button type="button" data-edit>Edit</button>
			</div>
				</output>
	</template>
</pintura-input>

That wasn’t hard, was it? Now let’s see if we assembled everything properly.

Let’s go to the Pintura Input Base Template section and remove the photo by clicking on the remove button:

This will remove the default image and allow us to upload any image that we want:

Simply click the browse button and choose a profile photo, or just drag and drop it.

This will trigger the Pintura interface, and allow the student to tweak their photo:

As you can see, the crop board is locked into a 4×4 proportion, so all we need to do is to move it around a bit to get the desired part of the image:

And just for a sake of showing some more features, let’s apply some filters to the photo:

Keep in mind that there are many filters and tweaks that can be applied, but also disabled if you want to be very specific on what kind of changes are available for this profile photo.

And that’s it. Once done, click the yellow “Done” button in the top right corner, and your new photo will be ready:

How awesome is this!

In just a few tweaked lines of code, we managed to allow our students to upload their profile photos in a neat way.

Furthermore, we easily enforced the needed rules, making usage of this image a breeze in our system.

And we saved a ton of bandwidth and processing power while browsing!

While the original image was 3744×5616 pixels and 1.36MB, the resulting image is in the desired 256×256 resolution and just 20KB in size.

Final thoughts on Pintura

While the Pintura editor is very easy to use and implement, it comes packed with a powerful set of features.

Combine that with a beautiful GUI and support for most of the environments out there, and we can tell this is a big-time saver and a UI asset to make your projects shine.

The blend of possible constraints and uploader freedom makes the opportunities rather unlimited.

While some may fall into thinking that this tool is mostly targeted to projects that deal with galleries and similar media, this tool can actually vastly improve any project that requires a photo to be uploaded, and also make the image processing easier for developers.

I highly encourage you to test it out and believe that you will fall in love as I did.

Written by Stefan Ristic

How to Copy HTML and CSS Code From Websites Easily

If you worked as a front-end web developer in the 2000s, you must know how hard it was to deal with even the simplest bugs. You had an HTML page that used a CSS file, and that CSS file could grow quite large based on the website size and complexity. And once some bug popped up, it could take days to navigate through the CSS file and identify/solve the bug.

Then in 2006, Firebug was born.

It pretty much changed the front-end development (and especially the debugging part) overnight. In fact, it was so great and helpful that browsers of the time (like Chrome and Firefox) quickly incorporated it.

Nowadays, you probably know this as an “Inspect tool” available in most modern browsers such as Chrome, Firefox, Edge, and others. And with all its “little” problems, the “Inspect tool” worked quite well for developers, saving their time and making front-end debugging many times easier.

But evolution never stops, and today we’re going to show you the logical progress in front-end inspection: CSS Scan.

What is CSS Scan?

CSS Scan is a modern browser extension that works well in Chrome, Firefox, Safari, Edge, and possibly any Chromium-based browser.

The purpose of CSS Scan is to help you inspect, copy, and edit CSS in the fastest way, saving you tons of time.

The main problem with the “Inspect tool” is that it provides too much data, making it hard to navigate and copy only the CSS rules that are actually needed while omitting the redundant styles:

Furthermore, the “Inspect tool” considers HTML and CSS as separate entities, so copying even a simple button requires multiple steps in order to get the HTML structure and CSS styles that make it bright and shining.

The CSS Scan extension solves these problems rather well.

No right click and no inspection.

No tedious styles to go through.

Just left-click the element you like on a website, and you’ll have the CSS rules ready to make that exact button on your own website.

And as long as you want it, that same one click will copy the CSS selectors and HTML structure as well.

Sounds good? Let me show you how to start using CSS Scan today.

How to install the CSS Scan extension in your browser?

CSS Scan is a browser extension, so the first thing to do is to go to your browser’s extension store and look for CSS Scan.

If using Chrome, you can do so here. And if you’re using Firefox, Safari, or Edge, you’ll receive instructions when you purchase it.

Click the blue “Add to Chrome” button and the extension will be installed in your browser in no time:

It is a premium extension, so the first time you open it, you’ll need to add the license key (which you can obtain at the official CSS Scan website):

Simply enter your license key, click the green button and you’ll be ready for the easiest HTML/CSS inspection.

How to set up CSS Scan?

As soon as you validate your license key, the extension will start working:

The “Pause” option will pause the extension, so the toolbar is visible, you’ll no longer see the CSS window floating around whenever you hover over elements, and you can freely click throughout the website.

The “Move” option will move the extension toolbar from top to bottom, in case you prefer to keep it there or there’s some element behind it, like headers.

And in order to use it optimally and according to your preference, you should check the “Options” item first:

There are quite a few options and preferences to go through, and you can tweak them on the fly, depending on what you want to do at that moment.

Among other things, this extension offers you the ability to copy only CSS or both CSS and HTML, what to do with parent and child stylings, conversion of font-size units to pixels, whether to ignore box-sizing, and others.

And at the end of the options menu, you have a nice list of available keyboard shortcuts, as many devs prefer the keyboard over the mouse whenever possible:

And once you choose the options that work best for you, it’s time to actually start copying the element styles in the easiest way possible.

The pain of using the Inspect Tool

For this example, I’m going to copy some styles and HTML from the BBC homepage.

But you can use the same easy way to copy any element and CSS from any website that is publicly available.

The easy way will follow in the next section since in contrast, let’s see how hard this is with the Inspect Tool.

On the homepage of the BBC website, let’s have a look at that latest business news element:

It has a nice white header with a red background, then a set of list items that are numbered in a stylish way. And as a final touch, every immediate list item has a different shade of the background.

“Normally”, we can use the Chrome Inspect tool to first copy the HTML:

And that doesn’t look that difficult.

But getting the CSS for the entire list, while keeping the redundant stuff out of the way, is indeed difficult.

As you can see, we have the “most-popular” div, then the “top-list” div, an <h2>, and then the <ul> list that contains multiple <li> elements.

Each <li> contains an <a> tag, and within each <a> tag, we have a <span> and an <h3>, plus the ::after pseudo element.

While we don’t have to copy each repetitive element (like <li> and <a>), we still need to separately copy the CSS rules of 9 distinctive elements within this list design!

And you may think okay, we will copy 9 CSS rules for each distinct selector.

But do you know which CSS to copy?

As you can see from the above screenshot, just the CSS for <h2> is so big that it can’t fit on the screen.

And we have 9 elements to go through, plus rule out a TON of CSS that actually isn’t needed, yet Chrome is showing any related styles there, regardless of whether they are actually used.

This is where the CSS Scan extension comes really handy.

How to easily copy HTML and CSS from any website with CSS Scan

Let’s go to the same list on the BBC homepage again.

And then open the CSS Scan options and make sure that we are copying the child element’s CSS and HTML:

Also, to easily target the entire section, while hovering an element, you can press your arrow keyboards (up and down) to navigate through the DOM tree so that you can target parents, siblings, and child elements.

Now click “Continue” and the extension will be ready to use.

Hovering over any element will instantly provide you codes needed to recreate that element:

And this is really helpful when you want to recreate a single element or two.

But we want to recreate the entire section, and we can do that as well with a single hover and click:

As you can see above, we have hovered the entire <div class=”most-popular”> element, so all child elements and associated CSS will be included.

Now just left-click on it and all rules will be copied to your clipboard:

That’s it!

Now that the code is copied, you could paste it right into your website source code, but just to see if it worked, create a new file called “index.html” on your desktop, then open your code editor of choice and paste the code from your clipboard:

Save the file, then open it in your browser:

How easy and awesome is that!

You can also export it to Codepen and play with it or save it on the cloud by pinning the CSS window first (pressing the space bar):

Further considerations when using CSS Scan

As demonstrated above, the CSS Scan extension is probably the easiest way to gather HTML and CSS from any website.

While it is a paid extension, it will save you a ton of time and pay off within days if not minutes.

But this is not all!

While the extension is dead simple to use, it is packed with powerful options that simply couldn’t fit in this article.

Starting with CSS Scan 3.0, you can export the HTML/CSS code directly to Codepen, for easy testing and sharing with your peers and co-workers.

And unlike the “Inspect Tool”, with CSS Scan you can automatically copy not just the standard selectors, but also the pseudo ones.

Selectors such as :hover, :before and :after will be incorporated, so not only your copied button will look great, but it will also be as interactive as it was on the original website!

Edit CSS on the fly with CSS Scan

If you like the possibility to change CSS code on the fly in the “Inspect Tool”, you’ll be happy to know that this is possible in CSS Scan as well.

All you need to do is press the space bar on your keyboard while hovering over the element you want to copy. This will lock the style window into place, and allow you to make live changes.

Let’s say you want to change the header background and the color of the numbers in the list from above.

Hover over the heading element and press the space bar, then hover over the number element and press the space bar again.

This will lock code editors for both elements in place. Now simply change the color from the editor:

Then you simply copy the code with one click and place the element(s) into your project.

Like all this isn’t enough, CSS Scan doesn’t only copy the styles for the desktop design. It will copy the entire rule set for that element (or elements), including rules for every browser and resolution available.

All in all, it is a real pleasure using the CSS Scan extension to quickly grab styles and markup. It’s so easy to use and packed with powerful features, you need to try it to believe it!

And then you’ll know the easiest way to copy clean and optimized HTML/CSS code from any website!