Skip to content

Instantly share code, notes, and snippets.

View jjxtra's full-sized avatar

Jeff Johnson jjxtra

View GitHub Profile
@jjxtra
jjxtra / imageRotated.m
Last active August 11, 2016 19:37
Rotating UIImage from AVCaptureSession [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:]
// this method rotates the UIImage captured by the capture session manager based on the device orientation when the image was captured ([AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];)
- (UIImage*) imageRotated:(UIImage*)image position:(AVCaptureDevicePosition)position orientation:(AVCaptureVideoOrientation)orientation
{
CGAffineTransform transform = CGAffineTransformIdentity;
CGFloat w = image.size.width * image.scale;
CGFloat h = image.size.height * image.scale;
CGFloat dw = w;
CGFloat dh = h;
image = [UIImage imageWithCGImage:image.CGImage scale:1.0f orientation:UIImageOrientationUp];
@jjxtra
jjxtra / HttpContext_RemoteIPAddress.cs
Last active October 26, 2024 05:42
C# / .NET core: Get Remote IP Address with Proxy / CDN Support
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace HttpContext_RemoteIPAddress
@jjxtra
jjxtra / ComputeOcclusionFromDepth.shader
Created December 12, 2018 19:07 — forked from aras-p/ComputeOcclusionFromDepth.shader
Unity command buffer that modifies screenspace shadow mask
Shader "Hidden/ComputeOcclusion"
{
Properties
{
_MainTex ("", 2D) = "white" {}
}
SubShader
{
Pass
{
@jjxtra
jjxtra / BetterEmbeddedFileProvider.cs
Last active July 11, 2019 15:51
The Microsoft embedded file provider is buggy and difficult to use, requires arcane project settings and fails in unit tests, especially with class libraries and multiple projects. This embedded file provider has no such problems.
// MIT license, https://github.com/jjxtra
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.Extensions.FileProviders;
@jjxtra
jjxtra / CloudflareForwardHeaders.cs
Created September 10, 2019 00:17
Cloudflare IApplicationBuilder forward header and proxy security
private static string[] GetStrings(string url)
{
return new WebClient().DownloadString(url).Split('\n').Select(s => s.Trim()).ToArray();
}
private static string[] GetCloudflareIP()
{
try
{
return GetStrings("https://www.cloudflare.com/ips-v4").Union(GetStrings("https://www.cloudflare.com/ips-v6")).ToArray();
@jjxtra
jjxtra / IDistributedCache.cs
Created December 23, 2019 14:41
New and improved C# IDistributedCacheInterface
/// <summary>
/// Simplified and improved IDistributedCache interface. No more stupid byte arrays. Removal events
/// can also be hooked into allowing removal from a local memory cache if desired.
/// </summary>
public interface IDistributedCache
{
/// <summary>
/// Fires when an item is removed by another machine, parameters are the key and the machine name
/// </summary>
event Action<DistributedCacheRemoveEventArgs>? Removed;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.Utilities
{
/// <summary>
/// Defines operations for locks used by Polly policies.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Wfp
{
/// PInvoke wrappers for Windows Filtering Platform
public class WfpNativeInterop
{
@jjxtra
jjxtra / OrderPreservingDictionary.cs
Last active March 10, 2021 17:34
The .NET Dictionary class will mostly preserve the insertion order of items when enumerated, until you start removing stuff - then there are no guarantees. Regardless, the documentation says order is undefined. This custom dictionary class preserves insertion order during all enumeration calls.
#nullable disable
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace System.Collections.Generic
{
/// <summary>
/// Dictionary that preserves inserted order of items during iteration.
@jjxtra
jjxtra / image_fade.js
Last active February 4, 2023 23:04
Don't pay for Cloudflare Pro just to get Mirage, use this script instead to cross fade and lazy load your images. No external js required! https://dailytechnews.io
//
// code used on https://dailytechnews.io to fade images
// MIT license, no support provided :)
//
// Setup:
// - you must put your img tag inside a span tag and set the img tag css to display: none. Make sure the css for the span and img tag has a height set. Width can be 100%.
// - query on all cross-fade spans in a script at the end of your body tag and call DailyTechNewsIoFade(eachSpan) for each one
// - i.e. _loop('.cross-fade', DailyTechNewsIoFade);
// - required html markup: <span class='cross-fade'><img class='your-image-class' src='/path/lowres/image.webp' title='title' alt='alt' data-src='/path/highres/image.webp' data-dim='1.5' /></span>
// - the src is a very low res image (32x32 max dimensions, with full res image aspect ratio as close as possible)