PHP script to generate numbered images

When I need small utilities or helping scripts I turn to a scripting language such as PHP not only because it is very easy to write quick code with it but also because it doesn’t need anything else besides a text file to run from command line. This was the case a few days ago when I suddenly needed a few hundred images to test an application but I also needed them to be numbered so I would know where each one of them is exactly.

Maybe someone else needs this so here it is:

<?php

    $width = 300;
    $height = 200;
    $fontSize = 100;
    $font = 'arial.ttf';

    $n = $argv[1];
    $path = $argv[2];

    for ($i = 1; $i <= $n; $i++) {
      $im = ImageCreate($width, $height);
      $white = imagecolorallocate($im, 255, 255, 255);
      $black = imagecolorallocate($im, 0, 0, 0);
      $grey = imagecolorallocate($im, 170, 170, 170);
      imagefill($im, 0, 0, $white);
      $bbox = imageftbbox($fontSize, 0, $font, $i);
      $x = ($width / 2) - ($bbox[4] / 2) - 5;
      $y = ($height / 2) - ($bbox[5] / 2) - 5;
      imagettftext($im, $fontSize, 0, $x, $y, $black, $font, $i);
      imagepng($im, $path.'\\'.str_pad($i, 4, '0', STR_PAD_LEFT).'.png');
      imagedestroy($im);
    }

?>