Skip to content

Instantly share code, notes, and snippets.

View lidocaine's full-sized avatar

天津甕星 lidocaine

View GitHub Profile
@lidocaine
lidocaine / base64_to_image.php
Last active February 16, 2023 16:13
Simple function to convert a base64 image string to its original format. Should only be used with trusted data!
<?php
function base64_to_image( string $base64_string, string $output_filename = '', string $output_dir = '_base64-out' ): string|false
{
$data = explode( ',', $base64_string );
$mime = substr( explode( ';', $data[0] )[0], 5 );
$ext = explode( '/', $mime )[1];
if ( ! str_starts_with( $mime, 'image' ) )
throw new InvalidArgumentException( 'Invalid image mime type data.' );
@lidocaine
lidocaine / giphy.php
Created July 19, 2019 16:23
A quick "middleman" function I wrote a while back while working on a discord bot in order to pull a random image from giphy based on a search term/s
<?php
$tag = urlencode( $_GET['t'] );
$giphyApiKey = 'dc6zaTOxFJmzC';
$giphyURL = 'http://api.giphy.com/v1/gifs/random?api_key='.$giphyApiKey.'&tag='.$tag;
$giphy = json_decode( file_get_contents( $giphyURL ) );
if ( $giphy->meta->status != 200 ) {
echo "**GiphyAPI Error: ".$giphy->meta->msg."**";
@lidocaine
lidocaine / ResponsiveImage.vue
Created April 11, 2019 19:03
Vue component to replace <img/> designed to be used when pulling images from Contentful but can be adapted as needed for various image sources.
<template>
<img
:id="id"
:src="src"
:srcset="srcSet"
:sizes="rSizes"
:alt="alt"
:class="classes"
/>
</template>
@lidocaine
lidocaine / REGEXs.md
Last active January 24, 2019 21:05
A place to keep regular expressions I wrote or found so I don't have to go hunting for them again because REGEX's hate me.

REGEXs.md

Form-related

Phone Number Validation (US)
^\d{3}[\-\s]{0,1}\d{3}[\-\s]{0,1}\d{4}$

@lidocaine
lidocaine / wp_human_readable_dateinterval.php
Created March 2, 2018 23:19
A PHP snippet for displaying a DateInterval object ($diff) as a human readable string in WordPress.
$readable_diff = [];
if ( $diff->y ) {
$readable_diff[] = sprintf( _n( '%s year', '%s years', $diff->y, 'textdomain' ), $diff->y );
}
if ( $diff->m ) {
$readable_diff[] = sprintf( _n( '%s month', '%s months', $diff->m, 'textdomain' ), $diff->m );
}
if ( $diff->d ) {
$readable_diff[] = sprintf( _n( '%s day', '%s days', $diff->d, 'textdomain' ), $diff->d );
@lidocaine
lidocaine / .gitigntore
Last active February 23, 2017 17:04
A "default" .gitignore designed to be used with minor customization on project init. This is an amalgamation of several templates with revisions/additions to suite my typical workflow/preferences.
.htaccess
#
# COMPOSER
#
composer.phar
/vendor/
#
@lidocaine
lidocaine / wordpress.php
Created October 30, 2014 17:45
Useful Wordpress snippets I've had to come up with and will probably need to use again.
/*
*
* Getting appropriate "X-Y of Z results" in searches
* with appropriate post numbering across pages
*
*/
// Setting up variables for math to display
// proper search result counts across all pages...
$posts_per_page = get_option('posts_per_page');
@lidocaine
lidocaine / easing.js
Created September 30, 2014 13:23
Easing Functions (js)
// These take t as (t/d)
// From: https://github.com/Prinzhorn/skrollr/issues/341
EasingFunctions = {
linear: function (t) { return t },
easeInQuad: function (t) { return t*t },
easeOutQuad: function (t) { return t*(2-t) },
easeInOutQuad: function (t) { return t<.5 ? 2*t*t : -1+(4-2*t)*t },
easeInCubic: function (t) { return t*t*t },
easeOutCubic: function (t) { return (--t)*t*t+1 },
easeInOutCubic: function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 },
@lidocaine
lidocaine / arrays.php
Created September 17, 2014 22:01
Things that no one should have to type over and over again...
$states = array(
'AL'=>'Alabama',
'AK'=>'Alaska',
'AZ'=>'Arizona',
'AR'=>'Arkansas',
'CA'=>'California',
'CO'=>'Colorado',
'CT'=>'Connecticut',
'DE'=>'Delaware',
'DC'=>'District of Columbia',
@lidocaine
lidocaine / ASOH-Mixins.scss
Last active August 29, 2015 14:05
My collection of SCSS (SASS) mixins that I find myself using more often than not.
// Adapted from - http://pivotallabs.com/bulletproof-font-face-syntax-with-sass/
@mixin declare-font-face($font-family, $font-filename, $font-path: '.', $font-weight: normal, $font-style: normal, $font-stretch: normal) {
@font-face {
font-family: '#{$font-family}';
src: url('#{$font-path}/#{$font-filename}.eot');
src: url('#{$font-path}/#{$font-filename}.eot?#iefix') format('embedded-opentype'),
url('#{$font-path}/#{$font-filename}.woff') format('woff'),
url('#{$font-path}/#{$font-filename}.ttf') format('truetype'),
url('#{$font-path}/#{$font-filename}.svg##{$font-family}') format('svg');
font-weight: $font-weight;