Skip to content

Instantly share code, notes, and snippets.

@Pierstoval
Last active January 9, 2020 15:24
Show Gist options
  • Save Pierstoval/eac8d182d2c51c93202f to your computer and use it in GitHub Desktop.
Save Pierstoval/eac8d182d2c51c93202f to your computer and use it in GitHub Desktop.
This enormous regexp matches any "Geometry" parameter for ImageMagick. See the docs about this: http://www.imagemagick.org/script/command-line-processing.php#geometry
<?php
$number = "\d*(?:\.\d+)?"; // It's a reference to use in other cases that matches any kind of number/float
$width = "(?<w>(?:$number)?%?)?"; // This is the first part, the width
$height = "(?:x(?<h>(?:$number)?%?))?"; // Here is the height, the same as "width" but starting with an "x"
$aspect = "[!><@^]"; // These are the different filters one can use to stretch, shrink, etc.
$size = "$width$height"; // To match any size we need width and height at least (aspect comes later)
$offset = "(?<x>[+-]$number)?(?<y>[+-]$number)?"; // This is the geometry offset
$regexp = "(?<size>$size)(?<aspect>$aspect)?(?<offset>$offset)"; // Here we have the full regexp
echo $regexp;
// Should echo this :
// (?<size>(?<w>(?:\d*(?:\.\d+)?)?%?)?(?:x(?<h>(?:\d*(?:\.\d+)?)?%?))?)(?<aspect>[!><@^])?(?<offset>(?<x>[+-]\d*(?:\.\d+)?)?(?<y>[+-]\d*(?:\.\d+)?)?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment