Skip to content

Instantly share code, notes, and snippets.

@savvot
savvot / latest-imagemagick-ubuntu18.04.sh
Created November 22, 2019 00:54
Builds latest ImageMagick on Ubuntu 18.04 with all dependencies
#!/bin/bash
# Before installing uncomment
# deb-src http://mirrors.online.net/ubuntu bionic main restricted
# in /etc/apt/sources.list
sudo apt update
sudo apt install build-essential
sudo apt build-dep imagemagick
@savvot
savvot / weighted_shuffle.php
Last active November 21, 2019 14:02
Fast php array weighted shuffle
<?php
/**
* Input can be specified as basic array or array of arrays:
* - array = [key1 => weight1, key2 => weight2, ...]
* - array = [[..., weightKey => weight], [..., weightKey2 => weight2], ...]
*
* @param array $array Array to shuffle
* @param string|null $weight_key Optional weight key if input is array of arrays
*/
@savvot
savvot / randuid.php
Last active January 18, 2017 08:00
Short random hash \ string for UIDs,URIs, etc.
<?php
function randuid($len = 8)
{
$uid = '';
do {
$uid .= base_convert(random_int(PHP_INT_MIN, PHP_INT_MAX), 10, 36);
$uid .= strtoupper(base_convert(random_int(PHP_INT_MIN, PHP_INT_MAX), 10, 36));
} while(strlen($uid) < $len * 2);
$uid = substr(str_shuffle($uid), 0, $len);
@savvot
savvot / weight_rand.php
Last active March 9, 2017 14:15
Weighted random function
<?php
/**
* @param array $data array of "key => weight" data to get random key from, all weights must be positive integers
* @return mixed key from $data array choosen by weighted random
*/
function weightRandom(array $data)
{
$c = count($data);
if (!$c) {
@savvot
savvot / popen-example.php
Created June 5, 2014 13:53
Run command\process from PHP in non blocking mode while dynamically reading its output
<?php
$fp = popen('ls 2>&1', 'r');
while(!feof($fp)) {
$str = fgets($fp);
}
pclose($fp);
@savvot
savvot / ffmpeg-extract-keyframes.sh
Last active February 3, 2022 06:40
Extract only keyframes (I-frames) from video to images with console ffmpeg
ffmpeg -ss <start_time> -i video.mp4 -t <duration> -q:v 2 -vf select="eq(pict_type\,PICT_TYPE_I)" -vsync 0 frame%03d.jpg