Skip to content

Instantly share code, notes, and snippets.

View wmelton's full-sized avatar

Wes wmelton

View GitHub Profile
@wmelton
wmelton / WP-Optimize-Image-On-Upload.php
Last active March 31, 2025 17:24
Optimize Images on Upload for Wordpress
<?php
/**
* Wordpress function to optimize image in accordance with Google Pagespeed recommendations upon
* upload. Supports jpeg/jpg image types.
*
* Authored by: Wes Melton
* Source: https://gist.github.com/wmelton/bbf82ce43103fae7efe5946734e4de19
*
* This function compresses the image, converts it to a progressive JPEG, and strips ICC information.
* It also converts it to sRGB colorspace, compresses to 85% quality and then saves it back.
@wmelton
wmelton / Code-Loader.txt
Created January 3, 2018 21:32
Code Loader Animation
https://codepen.io/anon/pen/LejmPB
@wmelton
wmelton / point-in-polygon.mysql
Created December 22, 2017 19:23
MySQL Point In Polygon
SELECT latitude, longitude
from mytable
where ST_CONTAINS(
GeomFromText( 'LINESTRING(50.5606 -1.8131,51.5606 -0.8131)' ),
point(longitude, latitude)
)
@wmelton
wmelton / Circles-Intersect.js
Created November 24, 2017 23:53
Circles Intersect
//Two circles intersect if, and only if, the distance between their centers is between the sum and the difference of their radii. Given two circles (x0,y0,R0) and (x1,y1,R1), the formula is as follows:
ABS(R0-R1) <= SQRT((x0-x1)^2+(y0-y1)^2) <= (R0+R1)
//Squaring both sides lets you avoid the slow SQRT, and stay with ints if your inputs are integer:
(R0-R1)^2 <= (x0-x1)^2+(y0-y1)^2 <= (R0+R1)^2
//Since you need only a yes/no test, this check is faster than calculating the exact intersection points.
//Edit: corrected for the "one circle inside the other" case.
@wmelton
wmelton / Normalization.php
Created November 21, 2017 21:20
Normalize / Denormalize Numbers
//PHP implementation for normalisation:
function normalize($value, $min, $max) {
$normalized = ($value - $min) / ($max - $min);
return $normalized;
}
//Denormalization
function denormalize($normalized, $min, $max) {
$denormalized = ($normalized * ($max - $min) + $min);
@wmelton
wmelton / inverseSqrt.cpp
Created August 29, 2017 15:02
Inverse Sqrt
/**
Fast inverse square root is an algorithm that estimates 1/√x, the reciprocal (or multiplicative inverse) of the square root of a 32-bit floating bit number x. This operation is used in digital signal processing to normalize a vector, i.e., scale it to length 1. For example, computer graphics programs use inverse square roots to compute angles of incidence and reflection for lighting and shading The algorithm is best known for its implementation in 1999 in the source code of Quake III Arena, a first-person shooter video game that made heavy use of 3D graphics. The algorithm only started appearing on public forums such as Usenet in 2002 or 2003.
At the time, it was generally computationally expensive to compute the reciprocal of a floating-point number, especially on a large scale; the fast inverse square root bypassed this step.
**/
float InvSqrt(float x){
float xhalf = 0.5f * x;
int i = *(int*)&x; // store floating-point bits in integer
@wmelton
wmelton / Fotorama-click-to-instantiate.js
Created July 26, 2017 14:28
Fotorama click to instantiate
//Originally found here - https://stackoverflow.com/questions/19014907/call-fotorama-gallery-in-full-screen-mode-via-api
<script>
$(function () {
var fotorama = $('.fotorama')
.fotorama({allowfullscreen: true})
.data('fotorama');
fotorama.requestFullScreen();
});
@wmelton
wmelton / countdown-timer.js
Created June 29, 2017 01:16
Javascript Countdown Timer
var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
// Get todays date and time
var now = new Date().getTime();
// Find the distance between now an the count down date
var distance = countDownDate - now;