<?php

//ini_set("memory_limit","1024M");
//set_time_limit(60000);

include_once '../../libs/phImaginator/phImaginator.class.php';
// database
/*$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen.");
*/
$connection=mysql_connect('localhost', 'intelectra_shop_', 'Wwja71!3') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra_shop__', $connection) or die("Konnte die Datenbank nicht waehlen.1");


$query = mysql_query('SELECT id FROM structure WHERE id=357') or die("Anfrage nicht erfolgreich");
$data = array();
while($obj = mysql_fetch_object($query)) {
       
	$query2 = mysql_query('
            select s.id, s.name, isa.item_id, files.file_name from structure s
            left join item_structure_assign isa on isa.structure_id=s.id
            left join item_files files on files.item_id=isa.item_id
            where s.parent_id='.$obj->id.'
            group by s.id'
        ) or die("Anfrage nicht erfolgreich");
	$image = '';
        $image_path = '';
        $i = 0;
	while($obj2 = mysql_fetch_object($query2)) {
            $i++;
            $obj2->file_name = 'Warengruppe.jpg';
            if ($obj2->file_name) {
		$image_path = "../../web/intelectra_shop/images/structure/".$obj2->file_name;
                $targetFile = "../../web/intelectra_shop/images/structure/".$i.$obj2->file_name;
                //echo $targetFile.'<br>';
                copy ($image_path,$targetFile);
                $imaginator = new phImaginator();
                $imaginator->add($targetFile);
                $imaginator->Resize_fitBoth(180, 180);
                //$imaginator->Box_fit(212, 212);
                //$imaginator->nameAdd($targetFile);
                $imaginator->save();
                $sql = "
                        UPDATE structure
                        SET
                            image='".$i.$obj2->file_name."'
                        WHERE id=".$obj2->id."
                ";
                echo "<br>".$sql;
                mysql_query($sql);
            }
        }
}


?>