Skip to content

Instantly share code, notes, and snippets.

View deanhume's full-sized avatar
🎮
Gaming

Dean deanhume

🎮
Gaming
View GitHub Profile
@deanhume
deanhume / gist:5037675
Created February 26, 2013 11:01
Data Uri - DrawImage()
public static MvcHtmlString DrawImage(this HtmlHelper helper, string imageUrl, string alt)
{
if (CanBrowserHandleDataUris() & IsFileSizeCorrect(imageUrl))
{
// Get the file type
string fileType = Path.GetExtension(imageUrl);
if (fileType != null)
{
fileType = fileType.Replace(".", "");
}
@deanhume
deanhume / gist:5038736
Created February 26, 2013 14:21
Convert the image to a base 64 string
/// <summary>
/// Converts the image to base64 string.
/// </summary>
/// <param name="imageUrl">The image URL.</param>
/// <returns></returns>
private static string ConvertImageToBase64String(string imageUrl)
{
string imagepath = HttpContext.Current.Server.MapPath(imageUrl);
using (Image image = Image.FromFile(imagepath))
@deanhume
deanhume / gist:5038748
Created February 26, 2013 14:22
Check if the browser is capable of handling data Uris
/// <summary>
/// Determines if the browser is able to handle Data URIs based on its version.
/// </summary>
/// <returns>
/// <c>true</c> if this instance [can browser handle data uris]; otherwise, <c>false</c>.
/// </returns>
private static bool CanBrowserHandleDataUris()
{
float browserVersion = -1;
@deanhume
deanhume / gist:5047078
Created February 27, 2013 10:52
Asynchronous Action to retrieve the PageSpeed score
public async Task<ActionResult> PageSpeed()
{
// Hit the API
var client = new HttpClient();
var homeResponseMessage = await client.GetAsync("https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=http://www.deanhume.com&key={The_API_Key}");
// Double check that it was successful
homeResponseMessage.EnsureSuccessStatusCode();
var homeValue = await homeResponseMessage.Content.ReadAsStringAsync();
@deanhume
deanhume / gist:5140647
Created March 12, 2013 05:56
Page Speed Object
public class PageSpeedEntity
{
public string kind { get; set; }
public string id { get; set; }
public int responseCode { get; set; }
public string title { get; set; }
public int score { get; set; }
public PageStats pageStats { get; set; }
}
@deanhume
deanhume / typeahead.html
Last active December 15, 2015 08:59
Twitter bootstrap typeahead - HTML
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Bootstrap Typeahead</title>
@deanhume
deanhume / typeahead.js
Last active December 15, 2015 08:59
Twitter bootstrap typeahead - JavaScript
$("#Search").typeahead({
source: function (query, process) {
var countries = [];
map = {};
// This is going to make an HTTP post request to the controller
return $.post('/Client/CountryLookup', { query: query }, function (data) {
// Loop through and push to the array
$.each(data, function (i, country) {
@deanhume
deanhume / CountryLookup.cs
Last active December 15, 2015 09:29
Bootstrap type ahead - Action method
public class ClientController : Controller
{
public ActionResult CountryLookup()
{
var countries = new List<SearchTypeAheadEntity>
{
new SearchTypeAheadEntity {ShortCode = "US", Name = "United States"},
new SearchTypeAheadEntity {ShortCode = "CA", Name = "Canada"},
new SearchTypeAheadEntity {ShortCode = "AF", Name = "Afghanistan"},
new SearchTypeAheadEntity {ShortCode = "AL", Name = "Albania"},
@deanhume
deanhume / DrawImage.cs
Created April 2, 2013 18:32
Build the Html WebP string
public static MvcHtmlString DrawImage(this HtmlHelper helper, string imageUrl, string alt)
{
if (CanBrowserHandleWebPImages())
{
// Get the file type
string fileType = Path.GetExtension(imageUrl);
if (fileType != null)
{
imageUrl = imageUrl.Replace(fileType, ".webp");
}
@deanhume
deanhume / mimeType.xml
Created April 2, 2013 19:17
MIME type Web.Config file
<system.webServer>
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" />
</staticContent>
</system.webServer>