Skip to content

Instantly share code, notes, and snippets.

View erikyo's full-sized avatar

Erik Golinelli erikyo

View GitHub Profile
@erikyo
erikyo / imagemin.mjs
Created May 10, 2023 19:55
Image Compression and Minification using imagemin in Node.js.
import imagemin from 'imagemin';
import imageminAvif from 'imagemin-avif';
import imageminSvgo from 'imagemin-svgo';
import imageminMozjpeg from 'imagemin-mozjpeg';
import { promises as fsPromises } from 'node:fs';
import { promisify } from 'node:util';
import path from 'node:path';
import fs from 'graceful-fs';
@erikyo
erikyo / pdf_append.sh
Created May 9, 2023 19:11
PDF Appender - Append a page to multiple PDF files in a directory using pdftk
#!/bin/bash
# Check if pdftk is installed
if ! command -v pdftk &> /dev/null; then
echo "pdftk is not installed."
echo "Please install pdftk for your operating system."
echo ""
case "$(uname -s)" in
Linux*)
echo "To install on Ubuntu/Debian, run: sudo apt-get install pdftk"
@erikyo
erikyo / xkcd_colorname_dictionary.json
Created December 31, 2022 11:23
the XKCD color names survey result in json format
[
{
"name": "pig pink",
"color": "#e78ea5"
},
{
"name": "deep lilac",
"color": "#966ebd"
},
{
@erikyo
erikyo / woocommerce_layered_navigation_add_classes.php
Last active October 4, 2022 17:03
Adds some useful classes to the WooCommerce layered navigation widget. This makes it very easy to stylise attribute colours or anything that can be represented with an icon.
<?php
/**
* Adds some useful classes to the WooCommerce layered navigation widget.
* This makes it very easy to stylise attribute colours or anything that can be represented with an icon.
*
* @param string $term_html - the item html
* @param object $term - the item props
*
* @return string - the old html list content wrapped inside a div with as class term id, term name and taxonomy
@erikyo
erikyo / image-formats-comparison.ipynb
Last active August 27, 2022 23:23
image formats comparison.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@erikyo
erikyo / imagemin.mjs
Last active September 27, 2022 11:33
Optimizes the images stored into wordpress plugin/theme (encodes png to webp, jpg to mozjpeg and optimizes), add this alongside wp-scripts
/**
* Minifying images stored into ./assets/source-images saving them into the ./assets/dist
*
* Add to package.json:
* "imagemin": "^8.0.1",
* "imagemin-mozjpeg": "^10.0.0",
* "imagemin-svgo": "^10.0.1",
* "imagemin-webp": "^7.0.0",
* "graceful-fs": "^4.2.10"
*
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
const MINIMUM_CHUNK_HEADER_LENGTH = 18;
const VP8X_ALPHA = 16;
const VP8X_ANIM = 2;
const VP8X_EXIF = 8;
@erikyo
erikyo / wordpress_oembed_utils.php
Created February 16, 2022 10:15
A filter for Wordpress oembed Block. Will set Youtube url to "youtube-nocookie.com" and will disable misc related videos (rel=). Enables Vimeo subtitles automatically using the browser language.
<?php
function oembed_utils( $html ) {
// set the default youtube url to youtube-nocookie.com
$html = str_replace("www.youtube.com/embed","www.youtube-nocookie.com/embed", $html);
// remove youtube random related videos
$html = str_replace( "feature=oembed", "feature=oembed&rel=0", $html );
@erikyo
erikyo / scss_custom_prop.scss
Last active January 28, 2022 11:55
Generates HSL color schemes with SCSS/SASS dynamically
/*
* Erik Codekraft - 2022
*/
/// Output the value for the property
/// @param {number or string} $val - the base value
/// @param {number} $val2 - the value to sum
/// @output the result or the css string calc(val1 + val2)
@function set_value($val, $val2) {
@if (type-of($val) == string) {
@erikyo
erikyo / webp_uploads_filter_clone_image.php
Last active February 1, 2022 12:36
Save a copy webp copy of the original image then update post metadata
<?php
function webp_uploads_filter_image_copy( $image_meta, $attachment_id ) {
$file = wp_get_original_image_path( $attachment_id );
$image_mime = wp_getimagesize( $file )['mime'];
if ( 'image/jpeg' === $image_mime ) {