Tag Archives: scale

Archiving thumnails of FTP camera images

This php script will iterate over an array $cams that has an element file that is the filename.

It uses the PHP Imagick package to resize the image and overlay a timestamp. The file timestamp is used so the time of the last ftp image is used, which may differ from the current time if the ftp has stalled. The image is then copied to an Hour/Minute specific folder for a 24 hour rolling archive.

$H = date('H');
$i = date('i');
$arcpath = $ftppath . $H . '/';
mkdir($arcpath);
$arcpath = $ftppath . $H . '/' . $i . '/';

foreach ($cams as $cam)
{
  $pathfile = $ftppath . $cam['file'];
  if (file_exists($pathfile)) {
    $thumbname = $cam['file'] . '_320x240';

    $thumb = new Imagick($pathfile);
    $thumb->scaleImage(320,240);

    $draw = new ImagickDraw();
    $draw->setFontSize(12);
    $draw->setFillColor(new ImagickPixel("#ffffff"));

    $draw->setTextAlignment(LEFT);
    $draw->annotation(5, 12,  date ("F d Y H:i:s", filemtime($pathfile)));
    $thumb->drawImage($draw);

    $thumb->setImageCompression(imagick::COMPRESSION_JPEG);
    $thumb->setImageCompressionQuality(80);
    $thumb->stripImage();
    $thumb->writeImage($ftppath . $thumbname);
    $thumb->clear();
    $thumb->destroy();
    copy($ftppath . $thumbname, $arcpath . $thumbname);
  }
}