<?php

require "../header.php";

$data = array();
$data["error"] = false;
$data["message"] = "";

$images = MTGImage::getList();

$removed = 0;
$newsize = 0;
foreach ( $images as $image ) {
    if ( !is_file("../images/" . $image->getFileName()) ) {
        $image->delete();
        $removed++;
    } else {
        list($width, $height) = getimagesize($destination);
        if ( ($width != $image->getWidth()) || ($height != $image->getHeight()) ) {
            $image->setWidth($width);
            $image->setHeight($height);
            $image->save();
            $newsize++;
        }
    }
}

$images = MTGImage::getList();
$existing_files = array();
foreach ( $images as $image ) {
    $existing_files[] = $image->getFileName();
}

$files = scandir("../images/");

$added = 0;
foreach ( $files as $file ) {
    if ( ($file == ".") || ($file == "..") ) continue;
    if ( in_array($file, $existing_files) ) continue;
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    if ( !in_array($ext, MTGImage::VALID_EXTENSIONS) ) continue;
    list($width, $height) = getimagesize($destination);
    $image = new MTGImage();
    $image->setFileName($file);
    $image->setWidth($width);
    $image->setHeight($height);
    $image->save();
    $added++;
}

$data["message"] = "{$removed} images removed from the database, {$added} images added to database, {$newsize} images with new dimensions";

if ( php_sapi_name() != "cli" ) {
    header('Content-Type: application/json');
    echo json_encode($data);
} else {
    var_dump($data);
}
exit();

// vim:ts=4 sw=4 et:
