Skip to content

Instantly share code, notes, and snippets.

@aroesler-privat
aroesler-privat / get_favicon.php
Last active June 11, 2023 15:56
Tries several ways to fetch the URL of the favicon of $site. Returns <img>-tag on success.
/* ---[ fetch the favicon of a given $site ]----------------------------
Tries several ways to fetch the URL of the favicon of $site. Returns
<img>-tag on success.
--------------------------------------------------------------------- */
function get_favicon($site, $class = 'favicon')
{
$site = empty($parsed = parse_url($site, PHP_URL_HOST)) ? $site : 'https://' . $parsed;
$site = preg_match('/^http/', $site) ? $site : 'https://' . $site;
$class = empty($class) ? '' : ' class="' . $class . '"';
<?php
/* ---[ set default template for new posts ]--------------------------------
helper function using all kinds of hints, tipps and some hackish timeout
to let the old editor and Gutenberg startup with a defined template for
dedicated post_type's (default: page, post)
------------------------------------------------------------------------- */
function setDefaultTemplate($template, $post_type = array('page', 'post'))
{
if (!is_array($post_type) && is_string($post_type)) $post_type = array($post_type);
if (!is_array($post_type)) return false;
@aroesler-privat
aroesler-privat / canvasViewportNotBlurry.js
Created July 30, 2022 09:15
create canvas & take care on the pixel ratio, especially to prevent blurry canvas-images on mobile devices when using <meta name="viewport" content="width=device-width, initial-scale=1">
/* -----------------------------------------------------------------
create canvas & take care on the pixel ratio, especially to prevent
blurry canvas-images on mobile devices when using
<meta name="viewport" content="width=device-width, initial-scale=1">
----------------------------------------------------------------- */
function getCanvas(width, height)
{
let canvas = document.createElement('canvas');
let dpr = window.devicePixelRatio;
@aroesler-privat
aroesler-privat / estimateObjectSize.js
Created July 26, 2022 07:00
Small function to estimate the size of an array
/* -------------------------------------------------------------------------
https://stackoverflow.com/a/19354869
------------------------------------------------------------------------- */
function estimateObjectSize(obj)
{
let buffer = JSON.stringify(obj).replace(/[\[\]\,\"]/g,'');
return buffer.length;
}
@aroesler-privat
aroesler-privat / SVGvoodoo.js
Last active July 25, 2022 13:17
This function loads a SVG-image into a given DIV-container. Before it adds the SVG it does some cleanup: The width- and height-statements are removed from the <svg>-tag to allow resizing, the fill-statements are removed from the <path>- and <g>-tags, a fill-style is added to a given ID - element and all IDs are updated with a prefix to prevent d…
/* inspired by https://codepen.io/osublake/pen/OMRdKq */
function loadSvgIntoDiv(theDIV, pathToSvg, prefix = 'myPrefix-', fillColor = null, fillColorID = null)
{
if ( theDIV == null || typeof theDIV.appendChild != 'function' ) return null;
fetch(pathToSvg).then( (res) => {
// check the status
/* ***********************************************************************
* random = new Random(seed);
*
* Just a little class to create random numbers between 0 and 1 (including
* 0 and excluding 1 just as Math.random() is doing it). To reproduce
* results a seed can be retrieved and used.
*
* Algorithm: https://en.wikipedia.org/wiki/Lehmer_random_number_generator
* *********************************************************************** */

Keybase proof

I hereby claim:

  • I am aroesler-privat on github.
  • I am a_roesler (https://keybase.io/a_roesler) on keybase.
  • I have a public key ASDoSN6jyZVn3jEFbbkgw1dpfLHVRi3W_0efXfAV4PHn_Ao

To claim this, I am signing this object: