Skip to content

Instantly share code, notes, and snippets.

View vijinho's full-sized avatar

Vijay vijinho

  • England
View GitHub Profile
@vijinho
vijinho / jpeg_squares.php
Created August 30, 2018 17:42
find square images in a folder and list number per pixel width size and also which files
<?php
// find square jpeg images
$cmd = 'find . -type f -iname "*.jp*" -print';
$files = cmd_execute($cmd);
$counts = [];
$by_size = [];
foreach ($files as $k => $path) {
$data = getimagesize($path);
@vijinho
vijinho / all_dirs.php
Last active August 26, 2018 18:30
get all files (and optionally dirs) in a dir path
#!/usr/bin/php
<?php
// get all files in a dir path,
function get_all_files($dir, $ignore_dirs = true) {
static $alldirs = array();
$dirs = glob($dir . '/*');
if (count($dirs) > 0) {
foreach ($dirs as $newdir) {
if (!array_key_exists($newdir, $alldirs)) {
@vijinho
vijinho / jpeg-rename.sh
Last active August 31, 2018 04:34
jpeg image - add comment (and/or rename based on exif date setting file time if exif exists)
# jpeg image - add comment (and/or rename based on exif date setting file time if exif exists) delete thumb
export JHEAD_FILE_ARGS="%Y-%m-%d-%a-%H%M%S"
export COMMENT="image use allowed only by permission of vijay mahrra <[email protected]> - all rights reserved"
# add comment, auto-rotate, rename files with exif tags, set file date
find . -type f -iname "*.jp*" -exec jhead -cl "${COMMENT}" -exonly -zt -di -dt -ft -autorot -n${JHEAD_FILE_ARGS} {} \;
# add comment, set file date to all files - will skip existing
find . -type f -iname "*.jp*" -exec jhead -cl "${COMMENT}" {} \;
@vijinho
vijinho / rsync-fast.sh
Last active January 6, 2025 10:56
fast rsync with an excludes file
time $(which rsync) \
--partial \
--delay-updates \
-aAHHSxXESy \
--stats \
--exclude-from=$HOME/.rsync-excludes
@vijinho
vijinho / wipe-files.sh
Created August 26, 2018 14:33
overwrite files with random data and then remove
#!/bin/sh
files=$@
for file in $files;
do
set `du $file`;
size=$1
for times in 0 1 2;
do
dd if=/dev/urandom of=$file count=$size conv=notrunc
done
@vijinho
vijinho / 0make_uae_configs.php
Last active August 16, 2018 16:06
PHP script to generate FS-UAE .ini config files from folder/subfolders of Commodore Amiga .adf/.afz files
<?php
// script to generate fs_uae configs for amiga floppies on mac os x or linux (.fs-uae files) as a zip archive
//
// first organise games with more than one disk into a subfolder containing the disks
// this will auto-load each extra floppy drive drive with each floppy (up to 4)
// e.g. make a folder 'alien_breed' containing abreed1.adz and abreed2.adz, config created is: Alien_Breed.fs-uae
// run 'php 0make_uae_configs.php' in Floppies folder to generate .fs-uae configs in folder 0configs to manually copy/move
//
// place games with multiple floppies under a subfolder per game
// author: [email protected]
@vijinho
vijinho / timer.php
Last active January 6, 2025 13:03
PHP Function Timer with results caching, memoization
<?php
// Implement a memoization function to cache results of expensive function calls.
$timer = function ($function) {
// Create an array to store cached results based on function arguments.
$cache = [];
// Return a closure that wraps the original function.
return function (...$args) use ($function, &$cache) {
// Generate a unique key for the cache based on function arguments.
@vijinho
vijinho / memoize.php
Created October 30, 2017 22:26
php memoize()
<?php
// https://www.youtube.com/watch?v=M3_xnTK6-pA?t=44m18s
// http://eddmann.com/posts/implementing-and-using-memoization-in-php/
//$memoize = function ($function) {
function memoize($function) {
return function () use ($function) {
static $cache = [];
$args = func_get_args();
$key = md5(serialize($args));
if (empty($cache[$key])) {
@vijinho
vijinho / shell_execute.php
Last active October 1, 2018 21:07
execute a command in php and return the captured output and streams stdin, stdout, stderr
<?php
define('VERBOSE', 1);
define('DEBUG', 1);
//-----------------------------------------------------------------------------
// required commands check
$commands = get_commands([
'find' => 'System command: find',
'curl' => 'https://curl.haxx.se',
'wget' => 'https://www.gnu.org/software/wget/'
@vijinho
vijinho / url_resolve.php
Last active September 29, 2018 11:57
unshorten a URL or find the target of a shortened URL and return it if success or status code if no success using PHP
#!/usr/bin/php
<?php
/**
* url_resolve.php - CLI script for resolving url
*
* @author Vijay Mahrra <[email protected]>
* @copyright (c) Copyright 2018 Vijay Mahrra
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
*/