<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);	
$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen.1");

include_once '../../libs/phImaginator/phImaginator.class.php';

$handle=opendir ("../../web/intelectra_shop/images/article_big/");
echo "Verzeichnisinhalt:<br>";
 $counter = array();
 $z = 0;
while ($datei = readdir ($handle)) {
$z++;
//if ($z < 50) {  
    //echo "$datei<br>";
    $number = substr($datei,0,6);
    // get item id
    $item_id = 0;
    $sql = "SELECT * FROM items WHERE number='".$number."'";
    $query2 = mysql_query($sql) or die("Anfrage nicht erfolgreich");
    while($obj2 = mysql_fetch_object($query2)) {
            $item_id = $obj2->id;
    }
    
    if ($item_id) {
        if (substr($datei,6,1) != '.') {
            $counter['image_'.$number]++;
        }
        else {
            $counter['image_'.$number] = 1;
            // set default image
            $sql = "UPDATE items
                    SET default_image_file_name='".$datei."'
                    WHERE id=".$item_id;
            $query = mysql_query($sql);
            if (!$query) {
                echo $sql;
                echo "<br />";
            }
        }
        $sql = "INSERT INTO item_files
            SET
                rang='".$counter['image_'.$number]."',
                type=0,
                file_name='".$datei."',
                item_id=".$item_id;
        $query = mysql_query($sql);
        if (!$query) {
                echo $sql;
                echo "<br />";
        }
        else {
            // resize image
            $default_generatable_images = array(
                    'overview_' => 189,
                    'thumb_' => 90,
                    'details_' => 380,
                    'detaildefault_' => 245
            );
            $targetFile = "../../web/intelectra_shop/images/article/".$datei;
            echo $targetFile.'<br>';
            copy ("../../web/intelectra_shop/images/article_big/".$datei,$targetFile);
            foreach ($default_generatable_images as $key => $size) {
                    $image_name = $key;
                    $imaginator = new phImaginator();
                    $imaginator->add($targetFile);
                    $imaginator->Resize_fitBoth($size, $size);
                    $imaginator->nameAdd($image_name);
                    $imaginator->save();
            }
        }
    }
    else {
        echo "nicht gefunden <br>";
    }
}
//}
closedir($handle);


?>