Wednesday, March 29, 2017

15+ Animated PowerPoint Templates With Amazing Interactive Slides

How to Create an Auto Repair Shop Illustration in Adobe Illustrator

How to Quickly Start to Make Passive Income (Begin Now)

How to Use OmniAuth-Twitter in a Rails Application

Design in 60 Seconds: What Is Typography?

Create Light Painted Typography From Scratch in Photoshop

15 Top Adobe Lightroom Presets on Envato Elements

UX in 60 Seconds: Using Component Libraries

Tuesday, March 28, 2017

How to Make a Simple How-To Video

How to Create a New York Stamp Icon Set in Adobe Illustrator

How to Work With Views in Microsoft PowerPoint

How to Overcome Your Fear of Sales and Rejection

Docker From the Ground Up: Building Images

Docker From the Ground Up: Building Images

Docker containers are on the rise as a best practice for deploying and managing cloud-native distributed systems. Containers are instances of Docker images. It turns out that there is a lot to know and understand about images. 

In this two-part tutorial, I'm covering Docker images in depth. In part one I discussed the basic principles, design considerations, and inspecting image internals. In this part, I cover building your own images, troubleshooting, and working with image repositories. 

When you come out on the other side, you'll have a solid understanding of what Docker images are exactly and how to utilize them effectively in your own applications and systems.

Building Images

There are two ways to build images. You can modify and existing container and then commit it as a new image, or you can write a Dockerfile and build it to an image. We'll go over both and explain the pros and cons.

Manual Builds

With manual builds, you treat your container like a regular computer. You install packages, you write files, and when it's all said and done, you commit it and end up with a new image that you use as a template to create many more identical containers or even base other images on.

Let's start with the alpine image, which is a very small and spartan image based on Alpine Linux. We can run it in interactive mode to get into a shell. Our goal is to add a file called "yeah" that contains the text "it works!" to the root directory and then create a new image from it called "yeah-alpine". 

Here we go. Nice, we're already in the root dir. Let's see what's there.

What editor is available? No vim, no nano?

Oh, well. We just want to create a file:

I exited from the interactive shell, and I can see the container named "vibrant_spenc" with docker ps --all. The --all flag is important because the container is not running anymore.

Here, I create a new image from the "vibrate_spence" container. I added the commit message "mine, mine, mine" for good measure.

Let's check it out. Yep, there is a new image, and in its history you can see a new layer with the "mine, mine, mine" comment.

Now for the real test. Let's delete the container and create a new container from the image. The expected result is that the "yeah" file will be present in the new container.

What can I say? Yeah, it works!

Using a Dockerfile

Creating images out of modified containers is cool, but there is no accountability. It's hard to keep track of the changes and know what the specific modifications were. The disciplined way to create images is to build them using a Dockerfile.

The Dockerfile is a text file that is similar to a shell script, but it supports several commands. Every command that modifies the file system creates a new layer. In part one we discussed the importance of dividing your image into layers properly. The Dockerfile is a big topic in and of itself. 

Here, I'll just demonstrate a couple of commands to create another image, "oh-yeah-alpine", based on a Dockerfile. In addition to creating the infamous "yeah" file, let's also install vim. The alpine Linux distribution uses a package management system called "apk". Here is the Dockerfile:

The base image is alpine. It copies the "yeah" file from the same host directory where the Dockerfile is (the build context path). Then, it runs apk update and installs vim. Finally, it sets the command that is executed when the container runs. In this case it will print to the screen the content of the "yeah" file.

OK. Now that we know what we're getting into, let's build this thing. The "-t" option sets the repository. I didn't specify a tag, so it will be the default "latest".

Looks good. Let's verify the image was created:

Note how installing vim and its dependencies bloated the size of the container from the 4.8MB of the base alpine image to a massive 30.5MB!

It's all very nice. But does it work?

Oh yeah, it works!

In case you're still suspicious, let's go into the container and examine the "yeah" file with our freshly installed vim.

The Build Context and the .dockerignore file

I didn't tell you, but originally when I tried to build the oh-yeah-alpine image, it just hung for several minutes. The issue was that I just put the Dockerfile in my home directory. When Docker builds an image, it first packs the whole directory where the Dockerfile is (including sub-directories) and makes it available for COPY commands in the Dockerfile. 

Docker is not trying to be smart and analyze your COPY commands. It just packs the whole thing. Note that the build content will not end in your image, but it will slow down your build command if your build context is unnecessarily large.

In this case, I simply copied the Dockerfile and the "yeah" into a sub-directory and ran the docker build command in that sub-directory. But sometimes you have a complicated directory tree from which you want to copy specific sub-directories and files and ignore others. Enter the .dockerignore file. 

This file lets you control exactly what goes into the build context. My favorite trick is to first exclude everything and then start including the bits and pieces I need. For example, in this case I could create the following .dockerignore file and keep the Docker file  and the "yeah" in my home directory:

There is no need to include the "Dockerfile" itself or the ".dockerignore" file in the build context.

Copying vs. Mounting

Copying files into the image is sometimes what you need, but in other cases you may want your containers to be more dynamic and work with files on the host. This is where volumes and mounts come into play. 

Mounting host directories is a different ball game. The data is owned by the host and not by the container. The data can be modified when the container is stopped. The same container can be started with different host directories mounted.

Tagging Images

Tagging images is very important if you develop a microservices-based system and you generate a lot of images that must be sometimes associated with each other. You can add as many tags as you want to an image. 

You've already seen the default "latest" tag. Sometimes, it makes sense to add other tags, like "tested", "release-1.4", or the git commit that corresponds to the image.

You can tag an image during a build or later. Here's how to add a tag to an existing image. Note that while it's called a tag, you can also assign a new repository.

You can also untag by removing an image by its tag name. This is a little scary because if you remove the last tag by accident, you lose the image. But if you build images from a Dockerfile, you can just rebuild the image.

If I try to remove the last remaining tagged image, I get an error because it is used by a container.

But if I remove the container...

Yep. It's gone. But don't worry. We can rebuild it:

Yay, it's back. Dockerfile for the win!

Working With Image Registries

Images are very similar in some respects to git repositories. They are also built from an ordered set of commits. You can think of two images that use the same base images as branches (although there is no merging or rebasing in Docker). An image registry is the equivalent of a central git hosting service like GitHub. Guess what's the name of the official Docker image registry? That's right, Docker Hub

Pulling Images

When you run an image, if it doesn't exist, Docker will try to pull it from one of your configured image registries. By default it goes to Docker Hub, but you can control it in your "~/.docker/config.json" file. If you use a different registry, you can follow their instructions, which typically involve logging in using their credentials.

Let's delete the "hello-world" image and pull it again using the docker pull command.

It's gone. Let's pull now.

The latest hello-world was replaced with a newer version.

Pushing Images

Pushing images is a little more involved. First you need to create an account on Docker Hub (or other registry). Next, you log in. Then you need to tag the image you want to push according to your account name ("g1g1" in my case).

Now, I can push the g1g1/hello-world tagged image.

Conclusion

Docker images are the templates to your containers. They are designed to be efficient and offer maximum reuse by using a layering file system storage driver. 

Docker provides a lot of tools for listing, inspecting, building and tagging images. You can pull and push images to image registries like Docker Hub to easily manage and share your images.


Politics Through the Eyes of 12 Artists

How to Build a Shifting Underline Hover Effect With CSS and JavaScript

How to Plan a Drone Video Flight

How to Create Artistic Backgrounds From Photos With These 3 Photoshop Actions

How to Create a Table of Contents in Microsoft Word

Monday, March 27, 2017

New Course: Go Further With Swift

10 Best Weather App Templates

Leverage Assembla's Zapier Integration With Cloud Services

How to Save Your PowerPoint to PDF in 60 Seconds

PyQuery: Python's jQuery

PyQuery: Python's jQuery

In this tutorial, you'll have a look at PyQuery, a Python library which allows you to make jQuery queries on XML documents. Syntactically it's quite similar to jQuery, and if you are familiar with jQuery it should be easier to follow.

Getting Started With PyQuery

To get started with PyQuery, install the Python package using PIP.

Once you have installed PyQuery, import it into the Python program.

Let's start with a basic example of how it works. Consider the following HTML: 

Pass the input XML to the PyQuery object and you should be able to apply jQuery style queries to it.

Assume divMain as a jQuery object and print the HTML content of the div.

Save the changes and try running the program. You should have the following output:

To access the Hello World text from the inside the span, the code would be:

Save the changes and try running the code. You should have the following output:

Attributes Manipulation Using PyQuery

Now let's have a look at how to read the attributes using the PyQuery library. Assume that you have an HTML element as shown:

Use the PyQuery library to read the above HTML content.

Let's try to access the ID attribute of the ulMain ul.

Save the above changes and try running the Python program. You should have ID ulMain printed on the terminal.

You saw how to access the ID attribute of the ul ulMain. Let's try to set a class name for the same ulMain element. To specify a class name attribute, you need to specify the attribute name and its corresponding value as shown:

Print the ulMain object and you should have the element with the class attribute.

You can also add and remove classes directly without using the attr method. To add a class to the element, you can make use of the method addClass

To remove an existing class from the element, you can make use of the removeClass method.

Handling CSS Using PyQuery

Apart from attributes, the element would have some CSS properties. To add or modify the CSS properties, you can use the css method and specify the properties. Let's say that you want to specify the height in the style of the ul ulMain. The following code would add the required style properties to the element.

Save the above changes and try executing the Python program. You should have the ul ulMain printed along with the new style.

To add multiple styles to the element, you can specify them as shown:

Run the program and you should have the styles added to the ulMain.

Creating & Appending Elements

During dynamic element creation, you are required to create new elements and append them to the existing parent element where they'll be rendered. Let's have a look at how to create and append elements using PyQuery

Assume you have a main container div called divMain.

Let's create a new span element using PyQuery.

Add some CSS properties to the span.

PyQuery provides a method to add elements to existing elements. You can use the append method to append the span element to the div divMain. Here is the code:

Save the changes and run the program. You should be able to see the divMain printed with the newly created span appended to it.

You used the append method to append the span to the div. You have another alternative method called appendTo which would append the nodes to value. In the above case you can use the method like so:

Finding Elements Using PyQuery

PyQuery provides methods to find children, the next elements, the closest elements, etc. It also provides methods to filter and find elements inside a particular node.

Assume that you have a particular piece of HTML as shown:

Add the following HTML to the PyQuery object:

Let's use PyQuery to find the children of the div divMain. Add the following line of code to print the children of divMain.

On running the program, you should have the following output:

To find the closest element to an element, you can use the method closest. To find the closest div element to the span, the code would be:

The above command would return the following output:

A find method is also available to find the elements. For example, to find a span inside the divMain, you need to call the method as shown:

The above command would return the span.

Insert an Element Inside HTML

While append does the work of adding elements to the existing elements, sometimes you need to insert elements after or before certain elements. PyQuery provides a method to insert elements before or after other elements.

Let's define a new paragraph element to be inserted after the span inside the divMain div.

Call the insertAfter method on the paragraph p element to insert it after the span.

Save the above changes and run the Python program. You should have the following output:

Similarly, you have the insertBefore method, which inserts before the element. Modify the code as shown below to use the insertBefore method: 

Save the changes and run the program. You should be able to see the following output on the terminal:

Wrapping It Up

In this tutorial, you saw how to get started with PyQuery, a Python library which allows you to make jQuery queries on XML documents. You saw how to manipulate the attributes and CSS styles of the HTML elements. 

You learnt how to create and append elements to existing elements and insert new elements before and after elements. What you saw in this tutorial is just the tip of the iceberg, and there is a lot more that this library has to offer.

For more detailed information on using this library, I would recommend reading the official documentation. Do let us know your suggestions in the comments below.


How to Program With Yii2: Running Cron Services

10 Design Tips for Creating Amazing Wedding Invitations

What is UX Design?

Fly That Drone Inside the Lines: Technical Limits

How to Create a Boston Terrier Illustration in Adobe Illustrator

Saturday, March 25, 2017

How to Add Motion to Your Documentary With a Timelapse

Effective Chord Changes on Guitar—Part 2

Effective Chord Changes on Guitar—Part 2

A good magician never lets the mechanics of a trick obscure an illusion. As for guitar playing, an audience should never hear how you’re producing sounds. In this context that means working towards seamless chord changes.

This is the second in a two-part tutorial and here are some more concepts and ideas for you to consider.

  • Guitar
    Effective Chord Changes on Guitar—Part 1
    Philip Ockelford

Finger Trickery

Most people favour the index or middle finger, and with good reason, as they are stronger than the others. This is due to the arrangement and distribution of tendons in the hand.

Consequently, we tend to think our fingers operate best from index (one) to little (four), but this isn't the case.

To illustrate my point, hold your hand up in front of you, then slowly close your fingers into your palm. Rather than 1-2-3-4, as you might expect, they close 4-3-2-1. This is the natural order of your fingers. 

If you're unsure, try closing them 1-2-3-4; you'll experience resistance, and it’ll feel unnatural.

This leads onto my next point.

Direction of Play

Consider the number of basic open chord shapes that use your index finger for the lowest fretted note. I can think of two—A and Em.

If you're still starting a change with the index finger, therefore, the chances are the chord constructions are occurring against the order of the strings. This will inevitably slow you down, as you wait for the chord to be built before striking the strings.

You should lead with the finger that you need first, which isn't always necessarily your first (index) finger.

In an Instant

Whilst some shapes may become formed almost instantly upon arrival, others by definition take longer to construct. Even if you've overcome the issue discussed in the previous point you may still experience gaps in your own playing.

This is because beginners don't commit to playing until the entire shape has been built. More experienced players recognise a fundamental truth:

A string only needs to be fretted just before it’s struck

Knowing this means that you can play a chord on a rolling basis; as long as each note is fretted just ahead of the string being played, the desired sound will be achieved.

Furthermore, as you're no longer waiting for the whole shape before playing gaps become almost non-existent.

Don't Let Go

Another source of gaps can be taking too many steps when executing a change. The greatest of these is completely letting go of the strings.

This can sometimes be inevitable, dependent on the requirements of the change, but otherwise should be avoided. 

Taking the fingers away from the strings is easy, but having to rearrange all of them back down again takes time. Every time this occurs you risk creating extraneous string noise.

Correcting it not only speeds up the changes but cleans up the sound.

Shared Notes

Not all shapes need dismantling when a change is required. This is especially true when two chords share one or more fretted notes.

Here's an example: the chords of C major and A minor. On the face of it they’re different types of chord. Both, however, feature the same two notes: C and E.

When played in open positions, E is on the second fret of the D string, and C is on the first fret of the B string. These notes are fretted in the same places using the same fingers in both shapes. 

Consider why you would you lift the fingers from the strings when moving from one to the other. It makes no sense. Beginners, however, often do so considering it to be a new shape.

When looking at a chord progression, try to identify any shared fretted notes between chords. As well as improving the changes, it’ll increase your understanding of which notes your chords contain; this in turn enhances your knowledge of the fretboard, which is useful for all aspects of playing guitar.

Overcoming Differences

Shared fretted notes provide a pivot point. By this I mean you can lean on them to pivot the fingers, or indeed, entire hand, in order to move from one shape to the next.

Of course, not every chord change features such notes. You still don’t have to let go of the strings. If there aren't any pivot points you'll need to create one.

For example, the change from G major to C major in the open positions. Whilst both feature the note of G, it's only fretted during the G chord (being an open string for C major) and thus is of no use to us in this instance.

When changing, lift the third finger away from its position in the G chord, and place it on the third fret of the A string; you're now on the root note of C major. If you've done this correctly, you should feel the third finger butt up against the back of the second finger.

Press down on the third finger, bringing your weight to bear. Keeping it planted, you can now swivel your hand into position for the C chord. If you're unsure as to what I mean, look at the thumb when you fret the G chord; it’s on the bass side of the neck. 

When you fret the C chord it moves towards the treble side which involves swivelling the hand into a new orientation.

It takes a little getting used to, but it's cleaner and quicker than letting go, plus it ensures you fret the chord in the direction of play.

Conclusion

In this tutorial I've shown you the following:

  • The fingers work back-to-front
  • Make changes in the direction of play
  • A string needs to be fretted only just before it’s struck
  • Try not to let go
  • Notes shared between shapes speed up changes
  • If they aren’t shared notes, create a pivot point note

Implementing these ideas will take time but by practicing they’ll make you a better player.


Friday, March 24, 2017

Code Your First Ionic 2 App: A Photo Sharing App

How to Create a Spring-Themed Illustration in Adobe Illustrator

How to Ensure Diversity in Your Recruiting and Hiring Practices

Android Things: Understanding and Writing Drivers

Docker From the Ground Up: Understanding Images

Docker From the Ground Up: Understanding Images

Docker containers are on the rise as a best practice for deploying and managing cloud-native distributed systems. Containers are instances of Docker images. It turns out that there is a lot to know and understand about images. 

In this two-part tutorial I'll cover Docker images in depth. In this part, I'll start with the basic principles, and then I'll move on to design consideration and inspecting image internals. In part two, I'll cover building your own images, troubleshooting, and working with image repositories. 

When you come out on the other side, you'll have a solid understanding of what Docker images are exactly and how to utilize them effectively in your own applications and systems.

Understanding Layers

Docker manages images using a back-end storage driver. There are several supported drivers such as AUFS, BTRFS, and overlays. Images are made of ordered layers. You can think of a layer as a set of file system changes. When you take all the layers and stack them together, you get a new image that contains all the accumulated changes. 

The ordered part is important. If you add a file in one layer and remove it in another layer, you'd better do it in the right order. Docker keeps track of each layer. An image can be composed of dozens of layers (the current limit is 127). Each layer is very lightweight. The benefit of layers is that images can share layers. 

If you have lots of images based on similar layers, like base OS or common packages, then all these common layers will be stored just once, and the overhead per image will be just the unique layers of that image.

Copy on Write

When a new container is created from an image, all image layers are read-only and a thin read-write layer is added on top. All the changes made to the specific container are stored in that layer. 

Now, it doesn't mean that the container can't modify files from its image layer. It definitely can. But it will create a copy in its top layer, and from that point on, anyone who tries to access the file will get the top layer copy. When files or directories are removed from lower layers, they become hidden. The original image layers are identified by a cryptographic content-based hash. The container's read-write layer is identified by a UUID.

This allows a copy-on-write strategy for both images and containers. Docker reuses the same items as much as possible. Only when an item is modified will Docker create a new copy.

Design Considerations for Docker Images

The unique organization in layers and the copy-on-write strategy promotes some best practices for creating and compositing Docker images.

Minimal Images: Less Is More

Docker images get enormous benefits from the point of view of stability, security and loading time the smaller they are. You can create really tiny images for production purposes. If you need to troubleshoot, you can always install tools in a container. 

If you write your data, logs and everything else only to mounted volumes then you can use your entire arsenal of debugging and troubleshooting tools on the host. We'll see soon how to control very carefully what files go into your Docker image.

Combine Layers

Layers are great, but there is a limit, and there is overhead associated with layers. Too many layers could hurt file system access inside the container (because every layer may have added or removed a file or directory), and they just clutter your own file system.

For example, if you install a bunch of packages, you can have a layer for each package, by installing each package in a separate RUN command in your Dockerfile:

Or you can combine them into one layer with a single RUN command.

Choosing a Base Image

Your base image (practically nobody builds images from scratch) is often a major decision. It may contain many layers and add a lot of capabilities, but also a lot of weight. The quality of the image and the author are also critical. You don't want to base your images on some flaky image where you're not sure exactly what's in there and if you can trust the author.

There are official images for many distributions, programming languages, databases, and runtime environments. Sometimes the options are overwhelming. Take your time and make a wise choice.

Inspecting Images

Let's look at some images. Here is a listing of the images currently available on my machine:

The repository and the tag identify the image for humans. If you just try to run or pull using a repository name without specifying the tag, then the "latest" tag is used by default. The image ID is a unique identifier.

Let's dive in and inspect the hello-world image:

It's interesting to see how much information is associated with each image. I will not go over each item. I'll just mention an interesting tidbit that the "container" and "containerConfig" entries are for a temporary container that Docker creates when it builds the image. Here, I want to focus on the last section of "RootFS". You can get just this part using the Go templating support of the inspect command:

It works, but we lost the nice formatting. I prefer to use jq:

You can see that the type is "Layers", and there is just one layer. 

Let's inspect the layers of the Python image:

Wow. Seven layers. But what are those layers? We can use the history command to figure that out:

OK. Don't be alarmed. Nothing is missing. This is just a terrible user interface. The layers used to have an image ID before Docker 1.10, but not anymore. The ID of the top layer is not really the ID of that layer. It is the ID of the Python image. The "CREATED BY" is truncated, but you can see the full command if you pass --no-trunc. I'll save you from the output here because of page-width limitations that will require extreme line wrapping.

How do you get images? There are three ways:

  • Pull/Run
  • Load
  • Build

When you run a container, you specify its image. If the image doesn't exist on your system, it is being pulled from a Docker registry (by default DockerHub). Alternatively, you can pull directly without running the container.

You can also load an image that someone sent you as a tar file. Docker supports it natively.

Finally, and most interestingly, you can build your own images, which is the topic of part two.

Conclusion

Docker images are based on a layered file system that offers many advantages and benefits for the use cases that containers are designed for, such as being lightweight and sharing common parts so many containers can be deployed and run on the same machine economically. 

But there are some gotchas, and you need to understand the principles and mechanisms to utilize Docker images effectively. Docker provides several commands to get a sense of what images are available and how they are structured.


International Artist Feature: Greece

How to Create a Quick and Dirty Sketch From a Photograph With a Photoshop Action

Thursday, March 23, 2017

How to Use OpenGL ES in Android Apps

How to Draw Metal

Productization: How to Turn Your Services Into Top Products

Uploading Files With Rails and Dragonfly

Uploading Files With Rails and Dragonfly

Some time ago I wrote an article Uploading Files With Rails and Shrine that explained how to introduce a file uploading feature into your Rails application with the help of the Shrine gem. There are, however, a bunch of similar solutions available, and one of my favorites is Dragonfly—an easy-to-use uploading solution for Rails and Rack created by Mark Evans. 

We covered this library early last year but, as with most software, it helps to take a look at libraries from time to time to see what's changed and how we can employ it in our application.

In this article I will guide you through the setup of Dragonfly and explain how to utilize its main features. You will learn how to:

  • Integrate Dragonfly into your application
  • Configure models to work with Dragonfly
  • Introduce a basic uploading mechanism
  • Introduce validations
  • Generate image thumbnails
  • Perform file processing
  • Store metadata for uploaded files
  • Prepare an application for deployment

To make things more interesting, we are going to create a small musical application. It will present albums and associated songs that can be managed and played back on the website.

The source code for this article is available at GitHub. You can also check out the working demo of the application.

Listing and Managing Albums

To start off, create a new Rails application without the default testing suite:

For this article I will be using Rails 5, but most of the described concepts apply to older versions as well.

Creating the Model, Controller, and Routes

Our small musical site is going to contain two models: Album and Song. For now, let's create the first one with the following fields:

  • title (string)—contains the album's title
  • singer (string)—album's performer
  • image_uid (string)—a special field to store the album's preview image. This field can be named anything you like, but it must contain the _uid suffix as instructed by the Dragonfly documentation.

Create and apply the corresponding migration:

Now let's create a very generic controller to manage albums with all the default actions:

albums_controller.rb

Lastly, add the routes:

config/routes.rb

Integrating Dragonfly

It's time for Dragonfly to step into the limelight. First, add the gem into the Gemfile:

Gemfile

Run:

The latter command will create an initializer named dragonfly.rb with the default configuration. We will put it aside for now, but you may read about various options at Dragonfly's official website.

The next important thing to do is equip our model with Dragonfly's methods. This is done by using the dragonfly_accessor:

models/album.rb

Note that here I am saying :image—it directly relates to the image_uid column that we created in the previous section. If you, for example, named your column photo_uid, then the dragonfly_accessor method would need to receive :photo as an argument.

If you are using Rails 4 or 5, another important step is to mark the :image field (not :image_uid!) as permitted in the controller:

albums_controller.rb

This is pretty much it—we are ready to create views and start uploading our files!

Creating Views

Start off with the index view:

views/albums/index.html.erb

Now the partial:

views/albums/_album.html.erb

There are two Dragonfly methods to note here:

  • album.image.url returns the path to the image.
  • album.image_stored? says whether the record has an uploaded file in place.

Now add the new and edit pages:

views/albums/new.html.erb

views/albums/edit.html.erb

views/albums/_form.html.erb

The form is nothing fancy, but once again note that we are saying :image, not :image_uid, when rendering the file input.

Now you may boot the server and test the uploading feature!

Removing Images

So the users are able to create and edit albums, but there is a problem: they have no way to remove an image, only to replace it with another one. Luckily, this is very easy to fix by introducing a "remove image" checkbox: 

views/albums/_form.html.erb

If the album has an associated image, we display it and render a checkbox. If this checkbox is set, the image will be removed. Note that if your field is named photo_uid, then the corresponding method to remove attachment will be remove_photo. Simple, isn't it?

The only other thing to do is permit the remove_image attribute in your controller:

albums_controller.rb

Adding Validations

At this stage, everything is working fine, but we're not checking the user's input at all, which is not particularly great. Therefore, let's add validations for the Album model:

models/album.rb

validates_property is the Dragonfly method that may check various aspects of your attachment: you may validate a file's extension, MIME type, size, etc.

Now let's create a generic partial to render the errors that were found:

views/shared/_errors.html.erb

Employ this partial inside the form:

views/albums/_form.html.erb

Style the fields with errors a bit to visually depict them:

stylesheets/application.scss

Retaining an Image Between Requests

Having introduced validations, we run into yet another problem (quite a typical scenario, eh?): if the user has made mistakes while filling in the form, he or she will need to choose the file again after clicking the Submit button.

Dragonfly can help you solve this problem as well by using a retained_* hidden field:

views/albums/_form.html.erb

Don't forget to permit this field as well:

albums_controller.rb

Now the image will be persisted between requests! The only small problem, however, is that the file upload input will still display the "choose a file" message, but this can be fixed with some styling and a dash of JavaScript.

Processing Images

Generating Thumbnails

The images uploaded by our users can have very different dimensions, which can (and probably will) cause a negative impact on the website's design. You probably would like to scale images down to some fixed dimensions, and of course this is possible by utilizing the width and height styles. This is, however, not an optimal approach: the browser will still need to download full-size images and then shrink them.

Another option (which is usually much better) is to generate image thumbnails with some predefined dimensions on the server. This is really simple to achieve with Dragonfly:

views/albums/_album.html.erb

250x250 is, of course, the dimensions, whereas # is the geometry that means "resize and crop if necessary to maintain the aspect ratio with center gravity". You may find information about other geometries on Dragonfly's website.

The thumb method is powered by ImageMagick—a great solution for creating and manipulating images. Therefore, in order to see the working demo locally, you'll need to install ImageMagick (all major platforms are supported). 

Support for ImageMagick is enabled by default inside Dragonfly's initializer:

config/initializers/dragonfly.rb

Now thumbnails are being generated, but they are not stored anywhere. This means each time a user visits the albums page, thumbnails will be regenerated. There are two ways to overcome this problem: by generating them after the record is saved or by performing generation on the fly.

The first option involves introducing a new column to store the thumbnail and tweaking the dragonfly_accessor method. Create and apply a new migration:

Now modify the model:

models/album.rb

Note that now the first call to dragonfly_accessor sends a block that actually generates the thumbnail for us and copies it into the image_thumb. Now just use the image_thumb method in your views:

views/albums/_album.html.erb

This solution is the simplest, but it's not recommended by the official docs and, what's worse, at the time of writing it does not work with the retained_* fields.

Therefore, let me show you another option: generating thumbnails on the fly. It involves creating a new model and tweaking Dragonfly's config file. First, the model:

The thumbs table will host your thumbnails, but they will be generated on demand. To make this happen, we need to redefine the url method inside the Dragonfly initializer:

config/initializers/dragonfly.rb

Now add a new album and visit the root page. The first time you do it, the following output will be printed into the logs:

This effectively means that the thumbnail is being generated for us by ImageMagick. If you reload the page, however, this line won't appear anymore, meaning that the thumbnail was cached! You may read a bit more about this feature on Dragonfly's website.

More Processing

You may perform virtually any manipulation to your images after they were uploaded. This can be done inside the after_assign callback. Let's, for example, convert all our images to JPEG format with 90% quality: 

There are many more actions you can perform: rotate and crop the images, encode with a different format, write text on them, mix with other images (for example, to place a watermark), etc. To see some other examples, refer to the ImageMagick section on the Dragonfly website.

Uploading and Managing Songs

Of course, the main part of our musical site is songs, so let's add them now. Each song has a title and a musical file, and it belongs to an album:

Hook up the Dragonfly methods, as we did for the Album model:

models/song.rb

Don't forget to establish a has_many relation:

models/album.rb

Add new routes. A song always exists in the scope of an album, so I'll make these routes nested:

config/routes.rb

Create a very simple controller (once again, don't forget to permit the track field):

songs_controller.rb

Display the songs and a link to add a new one:

views/albums/show.html.erb

Code the form:

views/songs/new.html.erb

Lastly, add the _song partial:

views/songs/_song.html.erb

Here I am using the HTML5 audio tag, which will not work for older browsers. So, if you're aiming to support such browsers, use a polyfill.

As you see, the whole process is very straightforward. Dragonfly does not really care what type of file you wish to upload; all in you need to do is provide a dragonfly_accessor method, add a proper field, permit it, and render a file input tag.

Storing Metadata

When I open a playlist, I expect to see some additional information about each song, like its duration or bitrate. Of course, by default this info is not stored anywhere, but we can fix that quite easily. Dragonfly allows us to provide additional data about each uploaded file and fetch it later by using the meta method.

Things, however, are a bit more complex when we are working with audio or video, because to fetch their metadata, a special gem streamio-ffmpeg is needed. This gem, in turn, relies on FFmpeg, so in order to proceed you will need to install it on your PC.

Add streamio-ffmpeg into the Gemfile:

Gemfile

Install it:

Now we can employ the same after_assign callback already seen in the previous sections:

models/song.rb

Note that here I am using a path method, not url, because at this point we are working with a tempfile. Next we just extract the song's duration (converting it to minutes and seconds with leading zeros) and its bitrate (converting it to kilobytes per second).

Lastly, display metadata in the view:

views/songs/_song.html.erb

If you check the contents on the public/system/dragonfly folder (the default location to host the uploads), you'll note some .yml files—they are storing all meta information in YAML format.

Deploying to Heroku

The last topic we'll cover today is how to prepare your application before deploying to the Heroku cloud platform. The main problem is that Heroku does not allow you to store custom files (like uploads), so we must rely on a cloud storage service like Amazon S3. Luckily, Dragonfly can be integrated with it easily.

All you need to do is register a new account at AWS (if you don't have it already), create a user with permission to access S3 buckets, and write down the user's key pair in a safe location. You might use a root key pair, but this is really not recommended. Lastly, create an S3 bucket.

Going back to our Rails application, drop in a new gem:  

Gemfile 

Install it:

Then tweak Dragonfly's configuration to use S3 in a production environment:

config/initializers/dragonfly.rb

To provide ENV variables on Heroku, use this command:

If you wish to test integration with S3 locally, you may use a gem like dotenv-rails to manage environment variables. Remember, however, that your AWS key pair must not be publicly exposed!

Another small issue I've run into while deploying to Heroku was the absence of FFmpeg. The thing is that when a new Heroku application is being created, it has a set of services that are commonly being used (for example, ImageMagick is available by default). Other services can be installed as Heroku addons or in the form of buildpacks. To add an FFmpeg buildpack, run the following command:

Now everything is ready, and you can share your musical application with the world!

Conclusion

This was a long journey, wasn't it? Today we have discussed Dragonfly—a solution for file uploading in Rails. We have seen its basic setup, some configuration options, thumbnail generation, processing, and metadata storing. Also, we've integrated Dragonfly with the Amazon S3 service and prepared our application for deployment on production.

Of course, we have not discussed all aspects of Dragonfly in this article, so make sure to browse its official website to find extensive documentation and useful examples. If you have any other questions or are stuck with some code examples, don't hesitate to contact me.

Thank you for staying with me, and see you soon! 


How to Sort Pictures Automatically With Automator for Mac

How to Create a Grunge Retro Text Effect in Adobe Photoshop

Wednesday, March 22, 2017

Learn to Create a Voice-Controlled Android App in Our New Course

How to Create a Simple Magazine Template in Adobe InDesign

Managing Cron Jobs Using Python

Managing Cron Jobs Using Python

In this tutorial, you'll learn the importance of cron jobs and why you need them. You'll have a look at python-crontab, a Python module to interact with the crontab. You'll learn how to manipulate cron jobs from a Python program using the python-crontab module.

What Is Cron?

During system administration, it's necessary to run background jobs on a server to execute routine tasks. Cron is a system process which is used to execute background tasks on a routine basis. Cron requires a file called crontab which contains the list of tasks to be executed at a particular time. All these jobs are executed in the background at the specified time.

To view cron jobs running on your system, navigate to your terminal and type in:

The above command displays the list of jobs in the crontab file. To add a new cron job to the crontab, type in:

The above command will display the crontab file where you can schedule a job. Let's say you have a file called hello.py which looks like:

Now, to schedule a cron job to execute the above script to output to another file, you need to add the following line of code:

The above line of code schedules the execution of the file with output to a file called a.txt. The numbers before the command to execute define the time of execution of the job. The timing syntax has five parts:

  1. minute
  2. hour
  3. day of month
  4. month
  5. day of week

Asterisks(*) in the timing syntax indicate it will run every time. 

Introducing Python-Crontab 

python-crontab is a Python module which provides access to cron jobs and enables us to manipulate the crontab file from the Python program. It automates the process of modifying the crontab file manually. To get started with python-crontab, you need to install the module using pip:

Once you have python-crontab installed, import it into the python program.

Writing Your First Cron Job

Let's use the python-crontab module to write our first cron job. Create a Python program called writeDate.py. Inside writeDate.py, add the code to print the current date and time to a file. Here is how writeDate.py would look:

Save the above changes.

Let's create another Python program which will schedule the writeDate.py Python program to run at every minute. Create a file called scheduleCron.py.

Import the CronTab module into the scheduleCron.py program.

Using the CronTab module, let's access the system crontab.

The above command creates an access to the system crontab of the user. Let's iterate through the cron jobs and you should be able to see any manually created cron jobs for the particular username.

Save the changes and try executing the scheduleCron.py and you should have the list of cron jobs, if any, for the particular user. You should be able to see something similar on execution of the above program:

Let's move on with creating a new cron job using the CronTab module. You can create a new cron by using the new method and specifying the command to be executed.

As you can see in the above line of code, I have specified the command to be executed when the cron job is executed. Once you have the new cron job, you need to schedule the cron job. 

Let's schedule the cron job to run every minute. So, in an interval of one minute, the current date and time would be appended to the dateInfo.txt file. To schedule the job for every minute, add the following line of code:

Once you have scheduled the job, you need to write the job to the cron tab.

Here is the scheduleCron.py file:

Save the above changes and execute the Python program.

Once it gets executed, check the crontab file using the following command:

The above command should display the newly added cron job.

Wait for a minute and check your home directory and you should be able to see the dateInfo.txt file with the current date and time. This file will get updated each minute, and the current date and time will get appended to the existing content.

Updating an Existing Cron Job

To update an existing cron job, you need to find the cron job using the command or by using an Id. You can have an Id set to a cron job in the form of a comment when creating a cron job using python-crontab. Here is how you can create a cron job with a comment:

As seen in the above line of code, a new cron job has been created using the comment as dateinfo. The above comment can be used to find the cron job.

What you need to do is iterate through all the jobs in crontab and check for the job with the comment dateinfo. Here is the code:

Check for each job's comment using the job.comment attribute.

Once you have the job, reschedule the cron job and write to the cron. Here is the complete code:

Save the above changes and execute the scheduleCron.py file. List the items in the crontab file using the following command:

You should be able to see the cron job with updated schedule time.

Clearing Jobs From Crontab

python-crontab provides methods to clear or remove jobs from crontab. You can remove a cron job from the crontab based on the schedule, comment, or command.

Let's say you want to clear the job with comment dateinfo from the crontab. The code would be:

Similarly, to remove a job based on a comment, you can directly call the remove method on the my_cron without any iteration. Here is the code:

To remove all the jobs from the crontab, you can call the remove_all method.

Once done with the changes, write it back to the cron using the following command:

Calculating Job Frequency

To check how many times your job gets executed using python-crontab, you can use the frequency method. Once you have the job, you can call the method called frequency, which will return the number of times the job gets executed in a year.

To check the number of times the job gets executed in an hour, you can use the method frequency_per_hour.

To check the job frequency in a day, you can use the method frequency_per_day

Checking the Job Schedule

python-crontab provides the functionality to check the schedule of a particular job. For this to work, you'll need the croniter module to be installed on your system. Install croniter using pip:

Once you have croniter installed, call the schedule method on the job to get the job schedule.

Now you can get the next job schedule by using the get_next method.

Here is the complete code:

You can even get the previous schedule by using the get_prev method.

Wrapping It Up

In this tutorial, you saw how to get started with using python-crontab for accessing system crontab from a Python program. Using python-crontab, you can automate the manual process of creating, updating, and scheduling cron jobs. 

Have you used python-crontab or any other libraries for accessing system crontab? I would love to hear your thoughts. Do let us know your suggestions in the comments below.


How to Add Slide Transitions In PowerPoint in 60 Seconds

Watch and Compile Sass in Five Quick Steps

New Course: 8 CSS Tricks You Never Use

Code Your First Ionic 2 App: Getting Set Up

How to Make a Beautiful Mesh Rose From Scratch in Adobe Illustrator

Visual Vocabulary: How to Add Motion to Your Documentary Shots

Automating the iPhone With Workflow II

Swift From Scratch: An Introduction to Classes and Structures

Tuesday, March 21, 2017

25 Premium Abstract Logo Designs

How to Make a Survey With Google Docs Forms

How to Create a Resume

20+ Best Personal WordPress Blog Themes for 2017

How to Quickly Make a One Page Website: From a Responsive Template

Passive Income: How to Stop Trading Your Time For Money

Procedural Generation for Simple Puzzles

Create a Space Shooter With PlayCanvas: Part 2

How to Create a Rubber Stamp Effect in Adobe Photoshop

Photoshop in 60 Seconds: How to Create a Vignette in 3 Ways

Monday, March 20, 2017

How to Create a Decorative Spring Floral Lettering Card in Adobe Illustrator

Learn CSS by Example With CSS Reference

25+ Awesome PowerPoint Templates With Cool PPT Designs

Making a Sliding Side Navigation Menu for Responsive Designs

How to Use Conditional Formatting in Microsoft Excel

Building With the Twitter API: Creating Friends to Follow

How to Create a Detailed Isometric Building in Adobe Illustrator

Design in 60 Seconds: Serif vs. Sans Serif Fonts

30 Top Items From the VideoHive Front Page, Winter 2017

Friday, March 17, 2017

New Course: Introduction to Sequence.js

Envato Tuts+ Community Challenge: Created by You, March 2017 Edition

How to Use AutoSum in Excel in 60 Seconds

Crafty Beyond the Basics: Collisions

Crafty Beyond the Basics: Collisions

It is very important in a game that you detect collisions properly. Nobody is going to play a game where things start exploding even when they are many pixels apart. Besides the graphics and sounds, this is one more thing that you should try to keep as accurate as possible.

In this tutorial, you will learn about detecting and debugging collisions in detail.

Detecting and Ignoring Hits

Before you can detect any collisions, you need to add the Collision component to an entity. This component will successfully detect a collision between any two convex polygons. This component has two events: HitOn and HitOff. The HitOn event is triggered when a collision occurs. It will not be triggered again unless collisions of that specific type cease. The HitOff event is triggered when a collision ceases.

If you are checking for collision with multiple components and all these collisions occur simultaneously, each collision will fire its own HitOn event. The data received from a collision event is only valid as long as the collision is still occurring.

You can use the .checkHits() method to perform collision checks against all entities with a specified component. Calling this method multiple times will not result in multiple redundant checks. 

Keep in mind that hit checks are performed upon entering each new frame. Let's say there are two objects which have not yet collided when the hit check is performed. Now, if one of the objects moves to a new location and overlaps with the second object later in the same frame, the hit events will not be fired until a collision check is performed again in the next frame.

If you have to detect only the first collision between different entities, you can use the .ignoreHits(String componentList) method. The componentList variable is a comma-separated list of components with which you no longer want to detect collisions. When no arguments are provided, it will stop collision detection with all entities. Here is an example:

You can see that Crafty not only starts detecting the HitOn event but also the HitOff event. That's why the color of the big box does not change back to black.

Another similar method called .resetHitChecks(String componentList) can be used to re-check for collision between specific components. This method will keep firing the HitOn event again and again as long as the collision is still happening.

Debugging Collisions

When entities are moving continuously, it is very hard to see if the collisions are being fired at the right time. In the above demo, it looks as if the HitOn event is firing slightly before the actual event. Crafty gives you the option of drawing hit boxes around entities so that you can actually see what's going on. 

There are two components that you can use for debugging purposes. These are WiredHitBox and SoldHitBox

The first component will allow you to use the .debugStroke([String strokeColor]) method, which will draw an outline around the entity with a given color. When no color is provided, the color red is used to draw the outline. 

Similarly, the second component is used to fill the entities with a given color using the .debugFill([String fillStyle]) method. When no color is provided, the color red is used. Here is a demo with the .debugStroke() method.

Creating a Custom Hit Box

You can also create a custom hit box for collision detection. This is helpful when you are using image sprites in your game that are not rectangular. The .collision() method that you can use to create a custom hit area accepts a single parameter that is used to set the coordinates of the new hit box. 

These coordinates can be supplied in the form of a polygon object, an array of x,y coordinate pairs, or a list of x,y coordinate pairs. The points of the polygon are marked in a clockwise manner, and they are positioned relative to the unrotated state of our entity. The custom hit area will automatically rotate itself when the entity rotates. 

There are a few things that you should keep in mind when using custom hit areas. The hit area that you define should form a convex polygon for the collision detection to work properly. For those who are unfamiliar with the term, a convex polygon is a polygon with all of the interior angles less than 180°. You might also see slight performance degradation when the hit area that you defined lies outside the entity itself.

The custom hit area that you defined won't have any effect unless you add the Collision component to every entity with which your hit area needs to detect a collision.

In the above demo, we have defined a custom hit box that lies outside the orange box. As you can see, the big block turns blue only when it collides with the triangle. The position of the orange box doesn't matter.

Let's take a look at another example which uses a spaceship by Gumichan01. The default hit box for the spaceship is the boundary of the sprite itself. In the current scenario, the top right corner of the spaceship touches the block first, but that space is actually empty. For users who are playing your game, this is a case of bad collision detection.

What you can do here is define your own hit area using a triangular shape like the following code. The custom hit box polygon that you define can have as many sides as you want. Just make sure that it is still a convex polygon.

Conclusion

After completing all these tutorials, you should now be able to create your own little games with great graphics, nice sound effects, scenes, and collision detection. I should remind you that I have used Crafty version 0.7.1 in this tutorial, and the demos might not work with other versions of the library. 

JavaScript has become one of the de-facto languages of working on the web. It’s not without it’s learning curves, and there are plenty of frameworks and libraries to keep you busy, as well. If you’re looking for additional resources to study or to use in your work, check out what we have available in the Envato marketplace.

If you have any questions, let me know in the comments.


How to Create a Western Text Effect in Adobe Illustrator

How to Apply a Halftone Effect to Your Images With a Photoshop Action

Thursday, March 16, 2017

RSVP for Events With These WordPress Plugins

Photoshop in 60 Seconds: Create a Pearl Necklace With the Mixer Brush in Photoshop

How to Use IFTTT With Google Sheets

Crafty Beyond the Basics: Sounds and Scenes

Crafty Beyond the Basics: Sounds and Scenes

Just like images or sprites, sound also plays a vital role in games. The right background music can set the mood for the game. Similarly, including sound effects for things like a crash or gunfire will make the game much more interesting.

You can also add scenes to your game to make it more organised. For example, instead of directly showing the game screen to users, you can first show them the home screen where they can choose a difficulty level for the game or increase/decrease the volume of background music. 

In this tutorial, you will learn how to add sounds and scenes to your games using Crafty.

Adding Sounds

The process for adding sounds to a game is similar to adding sprite sheets. You need to create an asset object and then supply an array of audio files for different sound effects. Crafty will then load the first file that is supported by the browser. Here is an example:

Once you have added the audio files, you can play them using Crafty.audio.play(String id, Number repeatCount, Number volume). The first parameter is the Id of the file we want to play. To play the background music, you can pass "back_music" as Id

You can control how many times an audio file is played using the second parameter. When this parameter is not specified, the file is played only once. You would probably want to continuously keep playing some sounds. One such example is the background music of a game. This can be achieved by setting the second parameter to -1. 

The third parameter controls the volume of the given audio file. It can have any value between 0.0 and 1.0. This is the code to play background music:

You can also play audio files based on some events like collision between entities or a key press.

Keep in mind that you need to add the Keyboard component to your hero before it can detect the KeyDown event. The above code binds the KeyDown event to the hero and checks if the key was pressed using evt.key. If the Up Arrow key is pressed, a jumping animation is played for the hero once. A gunshot sound is played as well.

Try pressing the Up Arrow key in the following demo and you will see it all in action. I have commented out the line that plays the background music, but you can just uncomment it while playing with the demo. 

The background music in the demo has been created by Rosalila, and the gunshot sound is by Luke.RUSTLTD.

There are a lot of other methods that you can use to manipulate the sounds played by Crafty. You can mute and unmute all the audio files that are being played in the game by using .mute() and .unmute() respectively. You can also pause and resume audio files based on their Id by using the .pause(Id) and .unpause(Id) method.

There is a limit on the maximum number of sounds that can be played simultaneously in Crafty. The default limit for this value is 7. Each of the different sounds playing simultaneously is a channel. However, you can set your own value by using Crafty.audio.setChannels(Number n). You can also check if a given audio file is currently playing on at least one channel using the .isPlaying(string ID) method. It will return a Boolean indicating if the audio is playing or not.

Scenes in Crafty

The game screen is generally not the first thing that you see in a game. Usually, the first screen that you see is the loading screen or the main menu screen. Then, once you have set different options like audio or difficulty level, you can click the play button to move on to the actual game screen. Finally, when the game is over, you can show users a game over screen.

These different game screens or scenes make your game more organized. A scene in Crafty can be created by calling Crafty.defineScene(String sceneName, Function init[, Function uninit])

The first parameter is the name of the scene. The second parameter is the initialization function, which sets things up when the scene is played. The third parameter is an optional function which is executed before the next scene is played and after all the entities with 2D component in the current scene have been destroyed.

Here is the code for defining the loading screen:

In the above code, I have defined a "loading_screen" scene. The initialization function set the background color to orange and shows some text to give the user some information about what's coming next. You can include a logo and some menu options in an actual game here. Pressing any key while the canvas is in focus will take you to the actual game screen. The .enterScene(String sceneName) method has been used here to load the "game_screen"

In the following demo, you can press the UP key 10 times to go to the final screen.

Conclusion

After completing this tutorial, you should be able to add a variety of sound effects to your game and be able to control the audio output. You can now also show different screens to a user in different situations. I should remind you that I have used Crafty version 0.7.1 in this tutorial, and the demos might not work with other versions of the library. 

In the next and final tutorial of this series, you will learn how to improve collision detection in Crafty.