Skip to content

Instantly share code, notes, and snippets.

View malenkiki's full-sized avatar

Michel Petit malenkiki

  • Freelance
  • Toulouse, France
View GitHub Profile
@malenkiki
malenkiki / compute_age.php
Created November 13, 2020 22:36
Compute age in year(s) using PHP
<?php
function compute_age(string $ymd): int
{
return (new \DateTime($ymd))->diff(new \DateTime())->y;
}
@malenkiki
malenkiki / byte_format.php
Created November 13, 2020 22:27
Format bytes as Ko, Go, and so on using PHP.
<?php
function byte_format($bytes, $precision = 2, $dec_point = '.')
{
// From https://stackoverflow.com/a/37523842
$units = ['o', 'Ko', 'Mo', 'Go', 'To'];
$bytes = max($bytes, 0);
$pow = min(
floor(($bytes ? log($bytes) : 0) / log(1024)),
count($units) - 1
@malenkiki
malenkiki / merge_pdf.sh
Created July 13, 2020 18:04
Merge PDF into one PDF file keeping hyperlinks using Ghostscript
gs \
-dNOPAUSE \
-sDEVICE=pdfwrite \
-sOUTPUTFILE=output.pdf \
-dBATCH \
-dPDFSETTINGS=/printer \
-dPrinted=false \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dFastWebView=true \
@malenkiki
malenkiki / change_rules.sh
Created August 14, 2019 13:03
Wordpress file access rules
#!/bin/sh
# Run like this:
#
# sh change_rules.sh /path/to/wordpress
#
chown -R www-data:www-data "$1"
find "$1" -type d -exec chmod 755 {} +
find "$1" -type f -exec chmod 644 {} +
@malenkiki
malenkiki / gist:5e15497176872fd47f6cbbc26a78d61e
Last active September 20, 2018 12:38
Memento de lignes de commandes pour transformer des images
# Optimisation JPEG basique
jpegoptim -s -o fichier.jpeg
# Création d'une vignette pour un fichier PDF avec Ghostscript (PNG avec canal alpha, carré de 400x400px)
gs \
-q \
-sDEVICE=pngalpha \
-dPDFFitPage=true \
-dLastPage=1 \
-dDEVICEWIDTHPOINTS=400 \
@malenkiki
malenkiki / gist:00da728722154364b094
Created July 28, 2014 09:32
Cake 2.x: Access to template vriable from view helper
<?php
$this->_View->viewVars['var'];
@malenkiki
malenkiki / gist:b7e0fe9133c4e2fd53eb
Created July 1, 2014 12:09
Git: Branch state into terminal
git log --oneline --decorate --all --graph
@malenkiki
malenkiki / gist:9071275
Last active August 29, 2015 13:56
Get information by pipe on PHP CLI script
$handle = fopen('php://stdin', 'rb');
$contents = stream_get_contents($handle);
fclose($handle);
var_dump($contents);
@malenkiki
malenkiki / Some assert for unit testing
Created February 7, 2014 21:59
Quick reminder for useful asserts used into PHPUnit
AssertTrue & AssertFalse
AssertEquals
AssertGreaterThan
LessThanOrEqual
AssertContains
AssertType
AssertNull
AssertFileExists
AssertRegExp
@malenkiki
malenkiki / gist:7584503
Last active December 29, 2015 00:09
Create inline HTML from strings provided by [PHP-Markdown](https://github.com/michelf/php-markdown), to have not htmlblock level element.
<?php
include('Michelf/Markdown.php');
use \Michelf\Markdown;
/**
* Custom Markdown class
*
* This class has just one method yet, to do inline transform without some