Skip to content

Instantly share code, notes, and snippets.

Dave James Miller davejamesmiller

Block or report user

Report or block davejamesmiller

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@davejamesmiller
davejamesmiller / ask.sh
Last active Dec 4, 2019
Bash General-Purpose Yes/No Prompt Function ("ask")
View ask.sh
# This is a general-purpose function to ask Yes/No questions in Bash, either
# with or without a default answer. It keeps repeating the question until it
# gets a valid answer.
ask() {
# https://djm.me/ask
local prompt default reply
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
@davejamesmiller
davejamesmiller / Laravel-Container.md
Last active Nov 28, 2019
Laravel's Dependency Injection Container in Depth
View Laravel-Container.md

Laravel's Dependency Injection Container in Depth

Translations: Korean (by Yongwoo Lee)

Laravel has a powerful Inversion of Control (IoC) / Dependency Injection (DI) Container. Unfortunately the official documentation doesn't cover all of the available functionality, so I decided to experiment with it and document it for myself. The following is based on Laravel 5.4.26 - other versions may vary.

Introduction to Dependency Injection

I won't attempt to explain the principles behind DI / IoC here - if you're not familiar with them you might want to read What is Dependency Injection? by Fabien Potencier (creator of the Symfony framework).

@davejamesmiller
davejamesmiller / paste-as-plain-text.ahk
Last active Nov 26, 2019
Paste clipboard contents as plain text with AutoHotKey
View paste-as-plain-text.ahk
; This is part of my AutoHotkey [1] script that pastes the contents of the
; clipboard as plain text when I press Ctrl+Alt+Shift+V.
; [1]: http://www.autohotkey.com/
; Ctrl+Alt+Shift+V
^!+v::
Clip0 = %ClipBoardAll%
ClipBoard = %ClipBoard% ; Convert to plain text
@davejamesmiller
davejamesmiller / monitor-off.ahk
Last active Nov 22, 2019
Turn monitor off with a keyboard shortcut with AutoHotKey
View monitor-off.ahk
; This is part of my AutoHotkey [1] script that turns off my monitor when I
; press Win+\.
; It also turns the screensaver on at the same time, so that Windows Live
; Messenger (and any other programs) know I am away.
; I don't have a password on my screensaver, so there is a second version
; (Win+Shift+\) that locks the PC as well.
; Note: Sometimes the monitor comes back on after a second and I have to
@davejamesmiller
davejamesmiller / http-redirect-target.php
Last active Nov 20, 2019
Get HTTP redirect destination for a URL in PHP
View http-redirect-target.php
<?php
// FOLLOW A SINGLE REDIRECT:
// This makes a single request and reads the "Location" header to determine the
// destination. It doesn't check if that location is valid or not.
function get_redirect_target($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
@davejamesmiller
davejamesmiller / .htaccess
Last active Nov 19, 2019
Resize an image on the fly with PHP and cache it (like Drupal's ImageCache)
View .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/([^/]*)$ resize.php?size=$1&file=$2
@davejamesmiller
davejamesmiller / wp-config.php
Last active Nov 10, 2019
Set WordPress site URL in the config file instead of the database
View wp-config.php
<?php
// WordPress stores the site URL in the database by default (which I have never
// understood), and it's a pain to have to type out the UPDATE SQL or search in
// phpMyAdmin to change it. This is a simple way to put the URL into
// wp-config.php instead.
// Note that you will still need to update any URLs that appear in the content,
// especially when you copy a database from a development site to production:
// https://gist.github.com/davejamesmiller/a8733a3fbb17e0ff0fb5
@davejamesmiller
davejamesmiller / run-program.ahk
Last active Nov 7, 2019
Run a program with a keyboard shortcut with AutoHotKey
View run-program.ahk
; This is a simple and pretty generic example of an AutoHotkey script to run a
; program when you press a keyboard shortcut. Add as many of these as you want
; to a .ahk file, and set that to be run at startup.
; See the Hotkeys reference [1] for details of the modifiers and keys available.
; [1]: http://www.autohotkey.com/docs/Hotkeys.htm
; Win+Alt+G - Open Gmail in Chrome
@davejamesmiller
davejamesmiller / README.md
Last active Nov 6, 2019
HTML to PDF conversion in PHP using wkhtmltopdf (and optionally Smarty)
View README.md

Recently I was asked to generate PDF invoices for an online shop. I looked at various PHP PDF generators, but wasn't particularly impressed with any of them.

Then I found (via Stack Overflow) a command-line HTML-to-PDF convertor called wkhtmltopdf, which uses WebKit (the same layout engine as Safari and Google Chrome) and therefore is very accurate.

There is a class for PHP integration on the Wiki, but I found it overly complicated and it uses temp files which aren't necessary. This is the code I wrote instead.

I used Smarty for generating the HTML for the PDF, but you can use any template engine, or pure PHP if you prefer.

Note: I originally tried to install wkhtmltopdf from source, but it's much easier to use the static binary instead.

@davejamesmiller
davejamesmiller / blogs-menu.php
Last active Nov 4, 2019
List of all blogs in a WordPress Multisite network
View blogs-menu.php
<?php
// This code was used to generate a menu showing all blogs on a WordPress
// Multisite installation.
// projects_menu() was used in the template to show a dropdown menu on all the
// sites.
// The shortcode [bloglist] was used in the homepage of the main blog to link to
// all the other blogs.
You can’t perform that action at this time.