Snippets

Get current user name in a bash script

Using /usr/bin/whoami command

#!/bin/bash
echo "$(whoami)"

Using /usr/bin/id command

#!/bin/bash
echo "$(id -u -n)"

Using USER environment variable

#!/bin/bash
echo "$USER"

Image to Data URI with PHP

I often find myself converting images to data URIs in an attempt to reduce network requests. This script is based on the same concept as David Walsh’s article Image Data URIs with PHP but rather than using the depreciated mime_content_type, my script makes use of FileInfo, which “provides the same functionality in a cleaner way” as quoted from the PHP docs.

echo '<img src="'.getDataURI('path_to/your_image.png').'">';
function getDataURI($image,$mime='') {
	$finfo = new finfo(FILEINFO_MIME_TYPE);
	$mime = $finfo->buffer(file_get_contents($image));
	return 'data:'.$mime.';base64,'.base64_encode(file_get_contents($image));
}

Please keep in mind if you plan on using data URIs, they are not supported by ie7 and lower.

Enable WordPress Featured Image

If your wondering how to enable WordPress featured images, your in the right place. First paste the following code in your functions.php file.

add_theme_support('post-thumbnails');

Now in your theme where you’d like the featured image to appear paste the following code.

<?php if (has_post_thumbnail()) : ?>
   <figure class="post-image">
      <?php the_post_thumbnail();?>
   </figure>
<?php endif; ?>

Silverstripe Canonical URL

Canonical URLs can be useful for SEO. Silverstripe does not include these by default so I’m going to show you 2 ways to add a Silverstripe canonical URL to your site.

Method 1

The most logical approach in my opinion, simply pull the url into our SS template. Do this by copying the following function into your Page_Controller class.

function CanonicalURL() {
   return Director::protocolAndHost() . $this->Link();
}

Then copy the following into the head of your SS template.

<link rel="canonical" href="$CanonicalURL">

read more